Class: RailsViewAdapters::DefinitionProxy

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#mapObject

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.

Parameters:

  • model_field (Symbol)


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.

Parameters:

  • model_field (Symbol)

    The field on the model that holds the association, usually the association’s name.

  • public_field (Symbol)

    The public field.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :model_class (Class)

    The class of the associated model, if it cannot be inferred from the model_field.

  • :sub_method (Symbol)

    The method of the association model that holds the desired data. Default is :id.

  • :only (Symbol)

    Only create the to_map or the from_map, as directed by setting this to :to or :from, respectively.



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, options = {})
  model_class = options[:model_class] || model_field.to_s.classify.constantize
  sub_method = options[:sub_method] || :id

  unless options[: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 options[: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

Parameters:

  • model_field (Symbol)
  • public_field (Symbol)


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.

Parameters:

  • model_field (Symbol)
  • public_field (Symbol)
  • date_format (String)

    The Date format to use.

Raises:

  • (ArgumentError)


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.

Parameters:

  • public_field (Symbol)

Yields:

  • (public_value)

    Given the value of public representation’s public_field, return a hash of key:value pairs to merge into the internal 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.

Parameters:

  • model_field (Symbol)

    The field on the model that holds the association, usually the association’s name.

  • public_field (Symbol)

    The public field.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :model_class (Class)

    The class of the model, if it cannot be inferred from the model_field.

  • :sub_method (Symbol)

    The method of the association model that holds the desired data. If this isn’t provided, it’s assumed to be the same as public_field.



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, options = {})
  model_class = options[:model_class] || model_field.to_s.classify.constantize
  sub_method = options[:sub_method] || public_field

  unless options[: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 options[: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.

Parameters:

  • model_field (Symbol)
  • public_field (Symbol)


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.

Parameters:

  • model_field (Symbol)
  • extra_public_fields (Array<Symbol>) (defaults to: [])

    Used to tell the adapter about extra public fields created by this mapping.

Yields:

  • (model_value)

    Given the value of the model’s model_field, return a hash of key:values pairs to merge into 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