Class: Isomorphic::Lens::IsomorphismAssociation
- Inherits:
-
Isomorphism
- Object
- AbstractLens
- Isomorphism
- Isomorphic::Lens::IsomorphismAssociation
- Defined in:
- lib/isomorphic/lens.rb
Overview
An Isomorphic lens for inflectable terms whose state is determined by an Active Record association.
Instance Attribute Summary collapse
-
#association ⇒ Object
readonly
Returns the value of attribute association.
Attributes inherited from Isomorphism
Attributes inherited from AbstractLens
Instance Method Summary collapse
-
#initialize(factory, inflector, association, *args) ⇒ IsomorphismAssociation
constructor
Default constructor.
- #set(record, isomorphism_instance, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new) ⇒ Object
Methods inherited from AbstractLens
Constructor Details
#initialize(factory, inflector, association, *args) ⇒ IsomorphismAssociation
Default constructor.
365 366 367 368 369 |
# File 'lib/isomorphic/lens.rb', line 365 def initialize(factory, inflector, association, *args) super(factory, inflector, *args) @association = association end |
Instance Attribute Details
#association ⇒ Object (readonly)
Returns the value of attribute association.
357 358 359 |
# File 'lib/isomorphic/lens.rb', line 357 def association @association end |
Instance Method Details
#set(record, isomorphism_instance, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new) ⇒ Object
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/isomorphic/lens.rb', line 371 def set(record, isomorphism_instance, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new) if association.nil? super(record, isomorphism_instance) else reflection = record.class.reflect_on_association(association) (reflection.klass.try(:"has_#{method_name}?") ? reflection.klass.send(:"from_#{method_name}", isomorphism_instance, nil, xmlattr_acc, *args) : reflection.klass.send(:"from_#{method_name}", isomorphism_instance, *args)).try { |association_record| case reflection.macro when :has_many, :has_and_belongs_to_many factory.xmlattrs.try(:each) do |xmlattr_name| isomorphism_instance.try(:"xmlattr_#{xmlattr_name}").try { |xmlattr_value| xmlattr_acc[xmlattr_name] ||= {} xmlattr_acc[xmlattr_name][xmlattr_value] = association_record } end record.send(association).send(:<<, association_record) when :has_one, :belongs_to factory.xmlattrs.try(:each) do |xmlattr_name| isomorphism_instance.try(:"xmlattr_#{xmlattr_name}").try { |xmlattr_value| xmlattr_acc[xmlattr_name] ||= {} xmlattr_acc[xmlattr_name][xmlattr_value] = association_record } end record.send(:"#{association}=", association_record) else raise Isomorphic::InvalidLens.new("unknown macro: #{reflection.macro}", self) end association_record } end end |