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.



9
10
11
# File 'lib/rails_view_adapters/definition_proxy.rb', line 9

def initialize(adapter_map)
  @map = adapter_map
end

Instance Attribute Details

#mapObject

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.

Parameters:

  • model_field (Symbol)


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.

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.



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, options = {})
  model_class = options[:model_class] || model_field.to_s.classify.constantize
  sub_method = options[:sub_method] || :id
  model_field_id = :"#{model_field.to_s.sub(/(_id|)\Z/, "_id")}"

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

Parameters:

  • model_field (Symbol)
  • public_field (Symbol)


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.

Parameters:

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

    The Date format to use.

Raises:

  • (ArgumentError)


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.

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.



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.

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.



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, 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)


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.

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.



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