GithubHelp home page GithubHelp logo

isabella232 / value_class Goto Github PK

View Code? Open in Web Editor NEW

This project forked from invoca/value_class-archived

0.0 0.0 0.0 522 KB

a lightweight way to define configuration DSLs

Groovy 4.52% Ruby 95.48%

value_class's Introduction

ValueClass

Provides a simple mechanism for declaring a class with complex attributes. Instances of the class can be progressively constructed in a using a block. The class is immutable outside of the block. This is useful for building simple configuration DSLs.

Installation

Add this line to your application's Gemfile:

gem 'value_class'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install value_class

Usage

The following class declaration allow a bicycle to be declared:

  class BikeTire
    include ValueClass::Constructable

    value_attr :diameter, description: "The diameter in inches"
    value_attr :tred,     description: "The tred on the tire"
  end

  class BikeSeat
    include ValueClass::Constructable

    value_attr :size, description: "The size of the bike seat in inches"
    value_attr :color
  end

  class Bicycle
    include ValueClass::Constructable

    value_description "For riding around town"
    value_attr :speeds
    value_attr :color, default: :orange
    value_attr :seat, class_name: 'BikeSeat'

    value_list_attr :riders, insert_method: :add_rider
    value_list_attr :tires, insert_method: :tire, class_name: 'BikeTire'
  end

Given the above declarations, you can then configure a bicycle with the following code.

  bike = Bicycle.config do |bicycle|
    bicycle.speeds 10
    bicycle.color  :blue
    bicycle.seat do |seat|
      seat.color :green
      seat.size  :large
    end
  end

You can also directly declare the class:

  bike = Bicycle.new(speeds: 10, color: :gold, tires: [{ diameter: 40, tred: :mountain }, { diameter: 50, tred: :slicks }])

If you have a simple class, ValueClass provides a replacement for ruby Struct that allows for a quick class declaration.

        
  Gears = ValueClass.struct(:first_gear, :second_gear, :third_gear, default: 200)
  gear = Gears.new(first_gear: 20) 

Once an instance of a class is returned. It is immutable: it is frozen and all of its attributes are frozen.

Running Tests

Tests in this gem are written in Rspec and can be executed through the main rake task for the repo

bundle exec rake

If there is a subset of tests you would like to run, you can add the focus: true tag to the test or context to only run the subset of tests.

Contributing

  1. Fork it ( https://github.com/invoca/value_class/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Make sure the tests pass: rspec spec
  6. Create a new Pull Request

value_class's People

Contributors

alecjacobs5401 avatar byendan avatar caitlinrhd avatar chrisatanasian avatar colindkelley avatar dcaddell avatar declanvk avatar deltaroe avatar dependabot[bot] avatar gabe-kent avatar infamelia avatar jebentier avatar naellis89 avatar nburwell avatar omeedrj avatar primiti avatar rjkearns avatar ttstarck avatar victorborda avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.