README.md in yaoc-0.0.5 vs README.md in yaoc-0.0.6

- old
+ new

@@ -1,6 +1,6 @@ -# Yaoc +# Yaoc [![Code Climate](https://codeclimate.com/github/slowjack2k/yaoc.png)](https://codeclimate.com/github/slowjack2k/yaoc) [![Build Status](https://travis-ci.org/slowjack2k/yaoc.png?branch=master)](https://travis-ci.org/slowjack2k/yaoc) [![Coverage Status](https://coveralls.io/repos/slowjack2k/yaoc/badge.png?branch=master)](https://coveralls.io/r/slowjack2k/yaoc?branch=master) [![Gem Version](https://badge.fury.io/rb/yaoc.png)](http://badge.fury.io/rb/yaoc) Indentation of this gem is to learn and train a little ruby. ## Installation @@ -24,30 +24,16 @@ ```ruby require 'yaoc' -User = Struct.new(:id, :firstname, :lastname, :role) do - def initialize(params={}) - super() +include Yaoc::Helper - params.each do |attr, value| - self.public_send("#{attr}=", value) - end if params - end -end +User = StructHE(:id, :firstname, :lastname, :role) -OldUser = Struct.new(:id, :fullname, :r_role) do - def initialize(params={}) - super() +OldUser = StructHE(:id, :fullname, :r_role) - params.each do |attr, value| - self.public_send("#{attr}=", value) - end if params - end -end - mapper = Yaoc::ObjectMapper.new(User, OldUser).tap do |mapper| mapper.add_mapping do fetcher :public_send rule to: :role, from: :r_role @@ -85,10 +71,14 @@ ### The resulting classes have no hash enabled constructor? ```ruby +require 'yaoc' + +include Yaoc::Helper + OldUser2 = Struct.new(:id, :fullname, :r_role) User2 = Struct.new(:id, :firstname, :lastname, :role) reverse_source = ->(attrs){ @@ -125,29 +115,37 @@ rule to: :id end end old_user2 = OldUser2.new(1, "myfirst mysecond", "admin" ) -new_user2 = mapper.load(old_user) +new_user2 = mapper.load(old_user2) puts old_user2 puts new_user2 new_user2.firstname = "no" new_user2.lastname = "name" puts mapper.dump(new_user2) + #<struct OldUser2 id=1, fullname="myfirst mysecond", r_role="admin"> #<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 + +require 'yaoc' + +include Yaoc::Helper + +puts "\n" * 5 + OldUser3 = Struct.new(:id, :fullname, :r_role) User3 = Struct.new(:id, :firstname, :lastname, :role) mapper = Yaoc::ObjectMapper.new(User3, OldUser3).tap do |mapper| @@ -177,11 +175,11 @@ end end old_user3 = OldUser3.new(1, "myfirst mysecond", "admin" ) -new_user3 = mapper.load(old_user) +new_user3 = mapper.load(old_user3) puts old_user3 puts new_user3 new_user3.firstname = "no" @@ -197,52 +195,28 @@ ### And how to use it with compositions? ```ruby -User4 = Struct.new(:id, :firstname, :lastname, :roles) do - def initialize(params={}) - super() +require 'yaoc' - params.each do |attr, value| - self.public_send("#{attr}=", value) - end if params - end -end +include Yaoc::Helper -OldUser4 = Struct.new(:o_id, :o_firstname, :o_lastname, :o_roles) do - def initialize(params={}) - super() - params.each do |attr, value| - self.public_send("#{attr}=", value) - end if params - end -end +puts "\n" * 5 -Role = Struct.new(:id, :name) do - def initialize(params={}) - super() +User4 = StructHE(:id, :firstname, :lastname, :roles) - params.each do |attr, value| - self.public_send("#{attr}=", value) - end if params - end -end +OldUser4 = StructHE(:o_id, :o_firstname, :o_lastname, :o_roles) -OldRole = Struct.new(:o_id, :o_name) do - def initialize(params={}) - super() - params.each do |attr, value| - self.public_send("#{attr}=", value) - end if params - end -end +Role = StructHE(:id, :name) +OldRole = StructHE(:o_id, :o_name) + role_mapper = Yaoc::ObjectMapper.new(Role, OldRole).tap do |mapper| mapper.add_mapping do fetcher :public_send rule to: :id, from: :o_id @@ -282,9 +256,55 @@ # o_roles=[#<struct OldRole o_id=1, o_name="admin">, #<struct OldRole o_id=2, o_name="guest">]> #<struct User4 id=1, firstname="firstname", lastname="lastname", # roles=[#<struct Role id=1, name="admin">, #<struct Role id=2, name="guest">]> #<struct OldUser4 o_id=1, o_firstname="firstname", o_lastname="lastname", # o_roles=[#<struct OldRole o_id=1, o_name="admin">, #<struct OldRole o_id=2, o_name="guest">]> + +``` + +### And how can I add values to existing objects? + +```ruby +require 'yaoc' + +include Yaoc::Helper + +puts "\n" * 5 + +OldUser5 = StructHE(:id, :name) + +RoleThing = StructHE(:id, :role) + +User5 = StructHE(:id, :name, :role) + + +user_mapper = Yaoc::ObjectMapper.new(User5, OldUser5).tap do |mapper| + mapper.add_mapping do + fetcher :public_send + rule to: [:id, :name] + end +end + +role_mapper = Yaoc::ObjectMapper.new(User5, RoleThing).tap do |mapper| + mapper.add_mapping do + fetcher :public_send + rule to: [:role] + end +end + +old_role = RoleThing.new(id: 1, role: "my_role") +old_user5 = OldUser5.new(id: 1, name: "my fullname") +new_user5 = user_mapper.load(old_user5) + +role_mapper.load(old_role, new_user5) + +puts old_user5 +puts old_role +puts new_user5 + +#<struct OldUser5 id=1, name="my fullname"> +#<struct RoleThing id=1, role="my_role"> +#<struct User5 id=1, name="my fullname", role="my_role"> ``` ## Contributing