Class: Isomorphic::Lens::Attribute

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

Overview

An Isomorphic lens for an attribute.

Constant Summary collapse

IDENTITY =

Returns the identity morphism (returns the argument).

Returns:

  • (Proc)

    the identity morphism (returns the argument)

::Proc.new { |x| x }.freeze

Instance Attribute Summary collapse

Attributes inherited from AbstractLens

#factory, #inflector

Instance Method Summary collapse

Methods inherited from AbstractLens

#get

Constructor Details

#initialize(factory, inflector, attribute_name, to = nil, from = nil) ⇒ Attribute

Default constructor.

Parameters:



278
279
280
281
282
283
284
285
# File 'lib/isomorphic/lens.rb', line 278

def initialize(factory, inflector, attribute_name, to = nil, from = nil)
  super(factory, inflector)

  @attribute_name = attribute_name

  @to = to || IDENTITY
  @from = from || IDENTITY
end

Instance Attribute Details

#attribute_nameObject (readonly)

Returns the value of attribute attribute_name.



263
264
265
# File 'lib/isomorphic/lens.rb', line 263

def attribute_name
  @attribute_name
end

#fromObject (readonly)

Returns the value of attribute from.



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

attr_reader :to, :from

#toProc (readonly)

Returns the modifier for after the getter.

Returns:

  • (Proc)

    the modifier for after the getter



269
270
271
# File 'lib/isomorphic/lens.rb', line 269

def to
  @to
end

Instance Method Details

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



287
288
289
290
291
292
293
294
295
296
# File 'lib/isomorphic/lens.rb', line 287

def set(record, isomorphism_instance, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new)
  args = case from.arity
    when 1 then [isomorphism_instance]
    else [isomorphism_instance, xmlattr_acc]
  end

  record.instance_exec(*args, &from).try { |value|
    record.send(:"#{attribute_name}=", value)
  }
end