!!!tree_struct
gem is merged to form_obj
gem and is not maintained separately anymore
TreeStruct
class is available now as FormObj::Struct
class - part of form_obj
gem.
Installation
Add this line to your application's Gemfile:
gem 'tree_struct'
And then execute:
$ bundle
Or install it yourself as:
$ gem install tree_struct
Usage
It is used as the parent class for FormObj::Form
Table of Contents
- Definition
- Serialize to Hash
- Reference Guide
1. Definition
Inherit your class from TreeStruct
and define its attributes.
class SimpleTreeStruct < TreeStruct
attribute :name
attribute :year
end
Access attributes via dot notation.
simple_tree_struct = SimpleTreeStruct.new # => #<SimpleTreeStruct name: nil, year: nil>
simple_tree_struct.name = 'Ferrari' # => "Ferrari"
simple_tree_struct.year = 1950 # => 1950
1.1. Nested TreeStruct Objects
Use blocks to define nested TreeStruct.
class NestedTreeStruct < TreeStruct
attribute :name
attribute :year
attribute :car do
attribute :body
attribute :driver
attribute :engine do
attribute :power
attribute :volume
end
end
end
Or explicitly define nested TreeStruct classes.
class EngineTreeStruct < TreeStruct
attribute :power
attribute :volume
end
class CarTreeStruct < TreeStruct
attribute :body
attribute :driver
attribute :engine, class: EngineTreeStruct
end
class NestedTreeStruct < TreeStruct
attribute :name
attribute :year
attribute :car, class: CarTreeStruct
end
Access attributes via dot notation.
nested_tree_struct = NestedTreeStruct.new # => #<NestedTreeStruct name: nil, year: nil, car: #<CarTreeStruct body: nil, driver: nil, engine: #<EngineTreeStruct power: nil, volume: nil, power: nil, volume: nil>, body: nil, driver: nil, engine: #<EngineTreeStruct power: nil, volume: nil, power: nil, volume: nil>>>
nested_tree_struct.name = 'Ferrari' # => "Ferrari"
nested_tree_struct.year = 1950 # => 1950
nested_tree_struct.car.body = '340 F1' # => "340 F1"
nested_tree_struct.car.driver = 'Ascari' # => "Ascari"
nested_tree_struct.car.engine.power = 335 # => 335
nested_tree_struct.car.engine.volume = 4.1 # => 4.1
1.2. Array of TreeStruct Objects
Specify attribute parameter array: true
in order to define an array of TreeStruct objects
class ArrayTreeStruct < TreeStruct
attribute :name
attribute :year
attribute :cars, array: true do
attribute :body
attribute :driver
attribute :engine do
attribute :power
attribute :volume
end
end
end
or
class EngineTreeStruct < TreeStruct
attribute :power
attribute :volume
end
class CarTreeStruct < TreeStruct
attribute :body
attribute :driver
attribute :engine, class: EngineTreeStruct
end
class ArrayTreeStruct < TreeStruct
attribute :name
attribute :year
attribute :cars, array: true, class: CarTreeStruct
end
Add new elements in the array by using method :create.
array_tree_struct = ArrayTreeStruct.new # => #<ArrayTreeStruct name: nil, year: nil, cars: []>
array_tree_struct.name = 'Ferrari' # => "Ferrari"
array_tree_struct.year = 1950 # => 1950
array_tree_struct.cars.size # => 0
array_tree_struct.cars.create # => #<CarTreeStruct body: nil, driver: nil, engine: #<EngineTreeStruct power: nil, volume: nil>>
array_tree_struct.cars.size # => 1
array_tree_struct.cars[0].body = '340 F1' # => "340 F1"
array_tree_struct.cars[0].driver = 'Ascari' # => "Ascari"
array_tree_struct.cars[0].engine.power = 335 # => 335
array_tree_struct.cars[0].engine.volume = 4.1 # => 4.1
2. Serialize to Hash
Call to_hash
method in order to get hash representation of the TreeStruct object
simple_tree_struct.to_hash # => {
# => :name => "Ferrari",
# => :year => 1950
# => }
2.1. Nested TreeStruct Objects
nested_tree_struct.to_hash # => {
# => :name => "Ferrari",
# => :year => 1950,
# => :car => {
# => :body => "340 F1",
# => :driver => "Ascari",
# => :engine => {
# => :power => 335,
# => :volume => 4.1
# => }
# => }
# => }
3.2. Array of TreeStruct Objects
array_tree_struct.to_hash # => {
# => :name => "Ferrari",
# => :year => 1950,
# => :cars => [{
# => :body => "340 F1",
# => :driver => "Ascari",
# => :engine => {
# => :power => 335,
# => :volume => 4.1
# => }
# => }]
# => }
3. Reference Guide: attribute
parameters
Parameter | Condition | Default value | Description |
---|---|---|---|
array | block* or :class ** |
false |
This attribute is an array of TreeStruct objects. The structure of array element is described either in the block or in the separate class referenced by :class parameter |
class | - | - | This attribute is either nested TreeStruct object or an array of TreeStruct objects. The value of this parameter is a class or a name of a class of this TreeStruct object. |
* block - means that there is block definition for the attribute |
** :class
- means that this attribute has :class
parameter specified
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/akoltun/tree_struct.
License
The gem is available as open source under the terms of the MIT License.