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.
12 13 14 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 12 def initialize(adapter_map) @map = adapter_map end |
Instance Attribute Details
#map ⇒ Object
Returns the value of attribute map.
11 12 13 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 11 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.
47 48 49 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 47 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.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 94 def map_belongs_to(model_field, public_field, = {}) model_class = [:model_class] || model_field.to_s.classify.constantize sub_method = [:sub_method] || :id unless [:only] == :to map_from_public public_field do |value| record = model_class.send(:"find_by_#{sub_method}", value) { model_field => record ? record : model_class.new(sub_method => value) } end end unless [:only] == :from map_to_public model_field do |record| { public_field => record.send(sub_method) } end end end |
#map_bool(model_field, public_field) ⇒ Object
Register a one-to-one mapping of a boolean field
73 74 75 76 77 78 79 80 81 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 73 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. When converting from the public representation, if the non-string values are returned as-is. Strings that cannot be parsed with the given date_format string are returned as nil.
If no timezone is provided, utc is assumed.
60 61 62 63 64 65 66 67 68 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 60 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, date_format) } 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.
41 42 43 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 41 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.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 122 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.
19 20 21 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 19 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.
29 30 31 32 33 34 |
# File 'lib/rails_view_adapters/definition_proxy.rb', line 29 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 |