Class: Eco::API::Common::Loaders::Parser

Inherits:
CaseBase show all
Defined in:
lib/eco/api/common/loaders/parser.rb

Defined Under Namespace

Classes: RequiredAttrs

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CaseBase

#name, name_only_once!

Methods inherited from Base

<=>, created_at, #name, set_created_at!

Methods included from ClassHelpers

#class_resolver, #descendants, #descendants?, #inheritable_attrs, #inheritable_class_vars, #inherited, #instance_variable_name, #new_class, #resolve_class, #to_constant

Constructor Details

#initialize(person_parser) ⇒ Parser



109
110
111
112
113
114
115
# File 'lib/eco/api/common/loaders/parser.rb', line 109

def initialize(person_parser)
  raise "Expected Eco::API::Common::People::PersonParser. Given #{policies.class}" unless person_parser.is_a?(Eco::API::Common::People::PersonParser)
  person_parser.define_attribute(self.attribute, dependencies: self.class.dependencies) do |attr_parser|
    _define_parser(attr_parser)
    _define_serializer(attr_parser)
  end
end

Class Attribute Details

.active_whenObject (readonly)

Returns the value of attribute active_when.



29
30
31
# File 'lib/eco/api/common/loaders/parser.rb', line 29

def active_when
  @active_when
end

Class Method Details

.active_when_all(*attrs) ⇒ Object

Helper to build the active_when condition.



81
82
83
84
85
86
87
# File 'lib/eco/api/common/loaders/parser.rb', line 81

def active_when_all(*attrs)
  @active_when_attrs = RequiredAttrs.new(attribute, :all, attrs)
  @active_when = Proc.new do |source_data|
    keys = data_keys(source_data)
    attrs.all? {|key| keys.include?(key)}
  end
end

.active_when_any(*attrs) ⇒ Object

Helper to build the active_when condition.



72
73
74
75
76
77
78
# File 'lib/eco/api/common/loaders/parser.rb', line 72

def active_when_any(*attrs)
  @active_when_attrs = RequiredAttrs.new(attribute, :any, attrs)
  @active_when = Proc.new do |source_data|
    keys = data_keys(source_data)
    attrs.any? {|key| keys.include?(key)}
  end
end

.attribute(value = nil) ⇒ String



35
36
37
38
39
40
41
# File 'lib/eco/api/common/loaders/parser.rb', line 35

def attribute(value = nil)
  unless value
    return @attribute || raise("You should specify the 'attribute' this parser/serializer, #{self.class}, is linked to")
  end
  name value
  @attribute = value
end

.dependencies(**value) ⇒ Object

Some parsers require dependencies to do their job.



44
45
46
47
48
49
50
51
# File 'lib/eco/api/common/loaders/parser.rb', line 44

def dependencies(**value)
  @dependencies ||= {}
  return @dependencies.merge({
    required_attrs: @active_when_attrs
  }) unless !value.empty?
  raise "Expected Hash. Given: '#{value.class}'" unless value.is_a?(Hash)
  @dependencies.merge!(value)
end

.parsing_phase(phase = nil) ⇒ Object

Define or get the phase that the parser kicks in.



56
57
58
59
60
# File 'lib/eco/api/common/loaders/parser.rb', line 56

def parsing_phase(phase = nil)
  @parsing_phase ||= :internal
  return @parsing_phase unless phase
  @parsing_phase = phase
end

.serializing_phase(phase = nil) ⇒ Object

Define or get the phase that the serializer kicks in.



65
66
67
68
69
# File 'lib/eco/api/common/loaders/parser.rb', line 65

def serializing_phase(phase = nil)
  @serializing_phase ||= :person
  return @serializing_phase unless phase
  @serializing_phase = phase
end

Instance Method Details

#attributeString, Symbol



135
136
137
# File 'lib/eco/api/common/loaders/parser.rb', line 135

def attribute
  self.class.attribute
end

#parser(data, deps) ⇒ Object



121
122
123
# File 'lib/eco/api/common/loaders/parser.rb', line 121

def parser(data, deps)
  raise "You should implement this method"
end

#seralizer(data, deps) ⇒ Object



130
131
132
# File 'lib/eco/api/common/loaders/parser.rb', line 130

def seralizer(data, deps)
  raise "You should implement this method"
end