Class: ClientDataAdapter::Wrapper
- Inherits:
-
Object
- Object
- ClientDataAdapter::Wrapper
- Defined in:
- lib/client-data-adapter/wrapper.rb
Instance Attribute Summary collapse
-
#target ⇒ Object
readonly
Instance of original class.
Class Method Summary collapse
-
.adapter(&block) ⇒ Hash
Main adapter method, should return
Hash
. -
.link_many(*associations) ⇒ Object
Used in one-to-many relationship.
-
.link_one(*associations) ⇒ Object
Used in one-to-one relationship.
-
.with(method_name, &block) ⇒ Object
Define a method of
Wrapper
.
Instance Method Summary collapse
-
#initialize(target, &block) ⇒ Wrapper
constructor
A new instance of Wrapper.
Constructor Details
#initialize(target, &block) ⇒ Wrapper
Returns a new instance of Wrapper.
12 13 14 15 16 |
# File 'lib/client-data-adapter/wrapper.rb', line 12 def initialize(target, &block) @target = target self.class.module_eval(&block) end |
Instance Attribute Details
#target ⇒ Object (readonly)
Instance of original class.
10 11 12 |
# File 'lib/client-data-adapter/wrapper.rb', line 10 def target @target end |
Class Method Details
.adapter(&block) ⇒ Hash
Main adapter method, should return Hash
.
Syntactic sugar of with
.
36 37 38 |
# File 'lib/client-data-adapter/wrapper.rb', line 36 def self.adapter(&block) with('__adapter__', &block) end |
.link_many(*associations) ⇒ Object
Used in one-to-many relationship.
Syntactic sugar of with
.
78 79 80 81 82 83 84 85 86 |
# File 'lib/client-data-adapter/wrapper.rb', line 78 def self.link_many(*associations) associations.each do |assoc| with(assoc) do |*args| (public_send(assoc.to_sym) || []).map do |elem| elem.adapter(*args) end end end end |
.link_one(*associations) ⇒ Object
Used in one-to-one relationship.
Syntactic sugar of with
.
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/client-data-adapter/wrapper.rb', line 53 def self.link_one(*associations) associations.each do |assoc| with(assoc) do |*args| obj = public_send(assoc.to_sym) if obj.respond_to?(:adapter) obj.adapter(*args) end end end end |
.with(method_name, &block) ⇒ Object
Define a method of Wrapper
.
Merged to the result of adapter
method.
102 103 104 105 106 107 108 109 |
# File 'lib/client-data-adapter/wrapper.rb', line 102 def self.with(method_name, &block) define_method(method_name.to_sym) do |*args| target.instance_exec( *args, &Util.to_lambda(block) ) end end |