Class: Isomorphic::Lens::Association
- Inherits:
-
AbstractLens
- Object
- AbstractLens
- Isomorphic::Lens::Association
- Defined in:
- lib/isomorphic/lens.rb
Overview
An Isomorphic lens for an Active Record association.
Instance Attribute Summary collapse
-
#association ⇒ Object
readonly
Returns the value of attribute association.
-
#next_lens ⇒ Object
readonly
Returns the value of attribute next_lens.
Attributes inherited from AbstractLens
Instance Method Summary collapse
-
#initialize(factory, inflector, association, next_lens = nil) ⇒ Association
constructor
Default constructor.
-
#last_lens ⇒ Isomorphic::Lens::AbstractLens
Returns the last lens in the composition.
-
#reflect_on_association(association) ⇒ self
Compose this lens with a new lens for the Active Record association with the given name.
-
#reflect_on_attribute(attribute_name, to = nil, from = nil) ⇒ self
Compose this lens with a new lens for the attribute with the given name.
-
#reflect_on_isomorphism(terms, *args) ⇒ self
Compose this lens with a new lens for the given inflectable terms and optional arguments.
- #set(record, isomorphism_instance, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new) ⇒ Object
Methods inherited from AbstractLens
Constructor Details
#initialize(factory, inflector, association, next_lens = nil) ⇒ Association
Default constructor.
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
#association ⇒ Object (readonly)
Returns the value of attribute association.
127 128 129 |
# File 'lib/isomorphic/lens.rb', line 127 def association @association end |
#next_lens ⇒ Object (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_lens ⇒ Isomorphic::Lens::AbstractLens
Returns the last lens in the composition.
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.
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.
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.
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 |