Class: AMA::Entity::Mapper::Context

Inherits:
Object
  • Object
show all
Includes:
Mixin::Reflection
Defined in:
lib/ama-entity-mapper/context.rb

Overview

Base class for various contexts, created to define common ground for any traversal operations

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixin::Reflection

#install_object_method, #method_object, #object_variable, #object_variable_exists, #object_variables, #set_object_attribute

Methods included from Mixin::Errors

#compliance_error, #mapping_error, #raise_if_internal, #validation_error

Constructor Details

#initialize(**options) ⇒ Context

Returns a new instance of Context.



30
31
32
33
34
35
36
37
38
# File 'lib/ama-entity-mapper/context.rb', line 30

def initialize(**options)
  defaults = respond_to?(:defaults) ? self.defaults : {}
  options = defaults.merge(options)
  defaults.keys.each do |key|
    instance_variable_set("@#{key}", options[key])
  end
  @logger = @logger.clone
  @logger.progname = "#{Mapper} #{path}"
end

Instance Attribute Details

#include_sensitive_attributesObject (readonly)

Returns the value of attribute include_sensitive_attributes.



28
29
30
# File 'lib/ama-entity-mapper/context.rb', line 28

def include_sensitive_attributes
  @include_sensitive_attributes
end

#loggerObject (readonly)

Returns the value of attribute logger.



22
23
24
# File 'lib/ama-entity-mapper/context.rb', line 22

def logger
  @logger
end

#pathObject (readonly)

Returns the value of attribute path.



19
20
21
# File 'lib/ama-entity-mapper/context.rb', line 19

def path
  @path
end

#strictObject (readonly)

Returns the value of attribute strict.



25
26
27
# File 'lib/ama-entity-mapper/context.rb', line 25

def strict
  @strict
end

Instance Method Details

#advance(segment) ⇒ AMA::Entity::Mapper::Context

Creates new context, resembling traversal to specified segment

Parameters:

Returns:



54
55
56
57
58
# File 'lib/ama-entity-mapper/context.rb', line 54

def advance(segment)
  return self if segment.nil?
  data = to_h.merge(path: path.push(segment))
  self.class.new(**data)
end

#defaultsObject



40
41
42
43
44
45
46
47
48
# File 'lib/ama-entity-mapper/context.rb', line 40

def defaults
  {
    path: Path.new,
    logger: Logger.new(Aux::NullStream::INSTANCE),
    strict: true,
    # Unstable feature, most likely it's name will change
    include_sensitive_attributes: false
  }
end

#to_hObject



60
61
62
# File 'lib/ama-entity-mapper/context.rb', line 60

def to_h
  object_variables(self)
end