Class: RailsViewAdapters::DefinitionProxy
- Inherits:
-
Object
- Object
- RailsViewAdapters::DefinitionProxy
- Defined in:
- lib/rails_view_adapters/definition_proxy.rb
Overview
Defines the DSL methods that are used to modify the underlying map. This class is only used to evaluate the DSL calls, thereby modifying the Map.
Instance Attribute Summary collapse
-
#map ⇒ Object
Returns the value of attribute map.
Instance Method Summary collapse
-
#hidden_field(model_field) ⇒ Object
Register a hidden field, i.e.
-
#initialize(adapter_map) ⇒ DefinitionProxy
constructor
A new instance of DefinitionProxy.
-
#map_belongs_to(model_field, public_field, options = {}) ⇒ Object
Register a mapping of a belongs_to association.
-
#map_bool(model_field, public_field) ⇒ Object
Register a one-to-one mapping of a boolean field.
-
#map_date(model_field, public_field, date_format) ⇒ Object
Register a one-to-one mapping of a date field.
-
#map_from_public(public_field) {|public_value| ... } ⇒ Object
Register a mapping from a public field to the model representation.
-
#map_has_many(model_field, public_field, options = {}) ⇒ Object
Register a mapping of a has_many association.
-
#map_simple(model_field, public_field) ⇒ Object
Register a simple one-to-one mapping.
-
#map_to_public(model_field, extra_public_fields = []) {|model_value| ... } ⇒ Object
Register a mapping from a model field to the public representation.
Constructor Details
#initialize(adapter_map) ⇒ DefinitionProxy
Returns a new instance of DefinitionProxy.
9 10 11 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 9 def initialize(adapter_map) @map = adapter_map end |
Instance Attribute Details
#map ⇒ Object
Returns the value of attribute map.
8 9 10 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 8 def map @map end |
Instance Method Details
#hidden_field(model_field) ⇒ Object
Register a hidden field, i.e. a field not present in public representations.
44 45 46 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 44 def hidden_field(model_field) map.add_model_field(model_field) end |
#map_belongs_to(model_field, public_field, options = {}) ⇒ Object
Register a mapping of a belongs_to association.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 86 def map_belongs_to(model_field, public_field, = {}) model_class = [:model_class] || model_field.to_s.classify.constantize sub_method = [:sub_method] || :id model_field_id = :"#{model_field.to_s.sub(/(_id|)\Z/, "_id")}" unless [:only] == :to map_from_public public_field do |value| record = model_class.send(:"find_by_#{sub_method}", value) { model_field_id => record ? record.id : nil } end end unless [:only] == :from map_to_public model_field_id do |id| { public_field => model_class.find_by(id: id).send(sub_method) } end end end |
#map_bool(model_field, public_field) ⇒ Object
Register a one-to-one mapping of a boolean field
65 66 67 68 69 70 71 72 73 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 65 def map_bool(model_field, public_field) map_from_public public_field do |value| { model_field => to_bool(value) } end map_to_public model_field do |value| { public_field => value } end end |
#map_date(model_field, public_field, date_format) ⇒ Object
Register a one-to-one mapping of a date field.
52 53 54 55 56 57 58 59 60 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 52 def map_date(model_field, public_field, date_format) raise ArgumentError if date_format.nil? map_from_public public_field do |value| { model_field => time_from_public(value) } end map_to_public model_field do |value| { public_field => value.utc.strftime(date_format) } end end |
#map_from_public(public_field) {|public_value| ... } ⇒ Object
Register a mapping from a public field to the model representation.
38 39 40 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 38 def map_from_public(public_field, &block) map.add_from_map(public_field, &block) end |
#map_has_many(model_field, public_field, options = {}) ⇒ Object
Register a mapping of a has_many association.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 115 def map_has_many(model_field, public_field, = {}) model_class = [:model_class] || model_field.to_s.classify.constantize sub_method = [:sub_method] || public_field unless [:only] == :to map_from_public public_field do |value| result = { model_field => model_class.where(sub_method => value) } public_field_size = value.respond_to?(:size) ? value.size : 0 result[model_field] = result[model_field] .to_a .fill(nil, result[model_field].size, public_field_size - result[model_field].size) result end end unless [:only] == :from map_to_public model_field do |records| { public_field => records.map(&sub_method.to_sym) } end end end |
#map_simple(model_field, public_field) ⇒ Object
Register a simple one-to-one mapping.
16 17 18 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 16 def map_simple(model_field, public_field) map.add_simple_map(model_field, public_field) end |
#map_to_public(model_field, extra_public_fields = []) {|model_value| ... } ⇒ Object
Register a mapping from a model field to the public representation.
26 27 28 29 30 31 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 26 def map_to_public(model_field, extra_public_fields = [], &block) map.add_to_map(model_field, &block) extra_public_fields.each do |public_field| map.add_public_field(public_field) end end |