Class: Isomorphic::Lens::Attribute
- Inherits:
-
AbstractLens
- Object
- AbstractLens
- Isomorphic::Lens::Attribute
- Defined in:
- lib/isomorphic/lens.rb
Overview
An Isomorphic lens for an attribute.
Constant Summary collapse
- IDENTITY =
Returns the identity morphism (returns the argument).
::Proc.new { |x| x }.freeze
Instance Attribute Summary collapse
-
#attribute_name ⇒ Object
readonly
Returns the value of attribute attribute_name.
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#to ⇒ Proc
readonly
The modifier for after the getter.
Attributes inherited from AbstractLens
Instance Method Summary collapse
-
#initialize(factory, inflector, attribute_name, to = nil, from = nil) ⇒ Attribute
constructor
Default constructor.
- #set(record, isomorphism_instance, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new) ⇒ Object
Methods inherited from AbstractLens
Constructor Details
#initialize(factory, inflector, attribute_name, to = nil, from = nil) ⇒ Attribute
Default constructor.
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_name ⇒ Object (readonly)
Returns the value of attribute attribute_name.
263 264 265 |
# File 'lib/isomorphic/lens.rb', line 263 def attribute_name @attribute_name end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
269 |
# File 'lib/isomorphic/lens.rb', line 269 attr_reader :to, :from |
#to ⇒ Proc (readonly)
Returns 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 |