Class: Isomorphic::Lens::Association

Inherits:
AbstractLens show all
Defined in:
lib/isomorphic/lens.rb

Overview

An Isomorphic lens for an Active Record association.

Instance Attribute Summary collapse

Attributes inherited from AbstractLens

#factory, #inflector

Instance Method Summary collapse

Methods inherited from AbstractLens

#get

Constructor Details

#initialize(factory, inflector, association, next_lens = nil) ⇒ Association

Default constructor.

Parameters:



139
140
141
142
143
# File 'lib/isomorphic/lens.rb', line 139

def initialize(factory, inflector, association, next_lens = nil)
  super(factory, inflector)

  @association, @next_lens = association, next_lens
end

Instance Attribute Details

#associationObject (readonly)

Returns the value of attribute association.



127
128
129
# File 'lib/isomorphic/lens.rb', line 127

def association
  @association
end

#next_lensObject (readonly)

Returns the value of attribute next_lens.



131
132
133
# File 'lib/isomorphic/lens.rb', line 131

def next_lens
  @next_lens
end

Instance Method Details

#last_lensIsomorphic::Lens::AbstractLens

Returns the last lens in the composition.

Returns:



148
149
150
151
152
153
154
155
156
# File 'lib/isomorphic/lens.rb', line 148

def last_lens
  next_lens.try { |current_lens|
    while current_lens.is_a?(self.class)
      current_lens = current_lens.next_lens
    end
  }.try { |current_lens|
    current_lens.next_lens
  }
end

#reflect_on_association(association) ⇒ self

Compose this lens with a new lens for the Active Record association with the given name.

Parameters:

  • association (#to_s)

    the association name

Returns:

  • (self)


179
180
181
182
183
# File 'lib/isomorphic/lens.rb', line 179

def reflect_on_association(association)
  append do
    self.class.new(factory, inflector, association, nil)
  end
end

#reflect_on_attribute(attribute_name, to = nil, from = nil) ⇒ self

Compose this lens with a new lens for the attribute with the given name.

Parameters:

  • attribute_name (#to_s)

    the attribute name

  • to (Proc) (defaults to: nil)

    the optional modifier for after the getter

  • from (Proc) (defaults to: nil)

    the optional modifier for before the setter

Returns:

  • (self)


191
192
193
194
195
# File 'lib/isomorphic/lens.rb', line 191

def reflect_on_attribute(attribute_name, to = nil, from = nil)
  append do
    Isomorphic::Lens::Attribute.new(factory, inflector, attribute_name, to, from)
  end
end

#reflect_on_isomorphism(terms, *args) ⇒ self

Compose this lens with a new lens for the given inflectable terms and optional arguments.

Parameters:

  • terms (Array<Object>)

    the inflectable terms

  • args (Array<Object>)

    the arguments for the isomorphism

Returns:

  • (self)

Raises:



203
204
205
206
207
# File 'lib/isomorphic/lens.rb', line 203

def reflect_on_isomorphism(terms, *args)
  append(with_next_lens: true) do |other|
    Isomorphic::Lens::IsomorphismAssociation.new(factory, inflector, other.association, terms, *args)
  end
end

#set(record, isomorphism_instance, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/isomorphic/lens.rb', line 158

def set(record, isomorphism_instance, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new)
  unless next_lens.is_a?(Isomorphic::Lens::AbstractLens)
    raise Isomorphic::InvalidLens.new(nil, self)
  end

  reflection = record.class.reflect_on_association(association)

  case reflection.macro
  when :has_many, :has_and_belongs_to_many
    raise Isomorphic::InvalidLens.new("invalid macro: #{reflection.macro}", self)
  when :has_one, :belongs_to
    next_lens.set(record.send(association) || record.send(:"build_#{association}"), isomorphism_instance, xmlattr_acc)
  else
    raise Isomorphic::InvalidLens.new("unknown macro: #{reflection.macro}", self)
  end
end