NestedStruct

Build nested structs from hash/array structures.

Installation

Add this line to your application's Gemfile:

gem 'nested_struct'

And then execute:

$ bundle

Or install it yourself as:

$ gem install nested_struct

Usage

class AddressForm
  include NestedStruct::Interface

  field :street
end

class SkillForm
  include NestedStruct::Interface

  field :name
end

class UserForm
  include NestedStruct::Interface

  field :name
  field :address, AddressForm
  field :skills, [SkillForm]
end

attributes = {
  name: 'Tom',
  address: { street: 'Long' },
  skills: [{ name: 'Jumping' }, { name: 'Kicking' }, {}]
}

form = UserForm.new(attributes)
#=> #<UserForm:0x0055cceb5c9438 @name="Tom", @address=#<AddressForm:0x0055cceb5c9320 @street="Long">, @skills=[#<SkillForm:0x0055cceb5c91e0 @name="Jumping">, #<SkillForm:0x0055cceb5c9118 @name="Kicking">, #<SkillForm:0x0055cceb5c90a0 @name=nil>]>

Contributing

Bug reports and pull requests are welcome.

License

The gem is available as open source under the terms of the MIT License.