README.md in yaoc-0.0.3 vs README.md in yaoc-0.0.4

- old
+ new

@@ -18,10 +18,12 @@ ## Usage Uptodate doc's look into the specs. +### The resulting classes have hash enabled constructors? + ```ruby require 'yaoc' User = Struct.new(:id, :firstname, :lastname, :role) do @@ -82,14 +84,17 @@ #<struct OldUser id=1, fullname="myfirst mysecond", r_role="admin"> #<struct User id=1, firstname="myfirst", lastname="mysecond", role="admin"> #<struct OldUser id=1, fullname="no name", r_role="admin"> -# the resulting class has no hash enabled constructor? -puts "\n" * 5 +``` +### The resulting classes have no hash enabled constructor? + +```ruby + OldUser2 = Struct.new(:id, :fullname, :r_role) User2 = Struct.new(:id, :firstname, :lastname, :role) reverse_source = ->(attrs){ @@ -142,12 +147,63 @@ #<struct User2 id=1, firstname="myfirst", lastname="mysecond", role="admin"> #<struct OldUser2 id=1, fullname="no name", r_role="admin"> ``` +### But my classes have positional constructor, what now? + +```ruby +OldUser3 = Struct.new(:id, :fullname, :r_role) +User3 = Struct.new(:id, :firstname, :lastname, :role) + + +mapper = Yaoc::ObjectMapper.new(User3, OldUser3).tap do |mapper| + mapper.add_mapping do + fetcher :public_send + + strategy :to_array_mapping + reverse_strategy :to_array_mapping + + rule to: 0, from: :id, + reverse_to: 0, reverse_from: :id + + rule to: 1, + from: :fullname, + + converter: ->(source, result){ result[1] = source.fullname.split().first }, + reverse_converter: ->(source, result){ result[1] = "#{source.firstname} #{source.lastname}" } + + rule to: 2, + from: :fullname, + + converter: ->(source, result){ result[2] = source.fullname.split().last }, + reverse_converter: ->(source, result){ result } + + rule to: 3, from: :r_role, + reverse_to: 2, reverse_from: :role + end +end + +old_user3 = OldUser3.new(1, "myfirst mysecond", "admin" ) +new_user3 = mapper.load(old_user) + +puts old_user3 +puts new_user3 + +new_user3.firstname = "no" +new_user3.lastname = "name" + +puts mapper.dump(new_user3) + +#<struct OldUser3 id=1, fullname="myfirst mysecond", r_role="admin"> +#<struct User3 id=1, firstname="myfirst", lastname="mysecond", role="admin"> +#<struct OldUser3 id=1, fullname="no name", r_role="admin"> + +``` + ## Contributing -1. Fork it ( http://github.com/<my-github-username>/yaoc/fork ) +1. Fork it ( http://github.com/slowjack2k/yaoc/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. Create new Pull Request