Class: Isomorphic::Lens::AbstractLens Abstract

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

Overview

This class is abstract.

Subclass and override #_get and #set to implement a custom class.

Generic base class for Isomorphic lenses.

Direct Known Subclasses

Association, Attribute, Isomorphism

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory, inflector) ⇒ AbstractLens

Default constructor.

Parameters:



81
82
83
84
85
# File 'lib/isomorphic/lens.rb', line 81

def initialize(factory, inflector)
  super()

  @factory, @inflector = factory, inflector
end

Instance Attribute Details

#factoryIsomorphic::Factory::AbstractFactory (readonly)

Returns the Isomorphic factory.

Returns:



75
76
77
# File 'lib/isomorphic/lens.rb', line 75

def factory
  @factory
end

#inflectorObject (readonly)

Returns the value of attribute inflector.



75
# File 'lib/isomorphic/lens.rb', line 75

attr_reader :factory, :inflector

Instance Method Details

#get(record_or_hash) ⇒ Object

Getter.

Parameters:

  • record_or_hash (ActiveRecord::Base, Hash<ActiveRecord::Base, ActiveRecord::Base>)

    the Active Record record or hash of records

Returns:

  • (Object)

    the value of the getter

Raises:



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/isomorphic/lens.rb', line 92

def get(record_or_hash)
  if record_or_hash.is_a?(::Hash)
    record_or_hash.each_pair.inject({}) { |acc, pair|
      association_record, record = *pair

      acc[association_record] = _get(record)
      acc
    }
  else
    _get(record_or_hash)
  end
end

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

Setter.

Parameters:

  • record (ActiveRecord::Base)

    the Active Record record

  • isomorphism_instance (Object)

    the instance

  • xmlattr_acc (ActiveSupport::HashWithIndifferentAccess) (defaults to: ::ActiveSupport::HashWithIndifferentAccess.new)

    the accumulator of XML attributes by name

Returns:

  • (Object)

    the value of the setter

Raises:



112
113
114
# File 'lib/isomorphic/lens.rb', line 112

def set(record, isomorphism_instance, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new)
  raise ::NotImplementedError
end