Class: Eco::API::Common::Loaders::Parser
- Inherits:
-
BaseLoader
- Object
- BaseLoader
- Eco::API::Common::Loaders::Parser
- Defined in:
- lib/eco/api/common/loaders/parser.rb
Direct Known Subclasses
People::DefaultParsers::BooleanParser, People::DefaultParsers::CSVParser, People::DefaultParsers::DateParser, People::DefaultParsers::FreemiumParser, People::DefaultParsers::LoginProvidersParser, People::DefaultParsers::MultiParser, People::DefaultParsers::NumericParser, People::DefaultParsers::PolicyGroupsParser, People::DefaultParsers::SendInvitesParser, Eco::API::Custom::Parser
Class Attribute Summary collapse
-
.active_when ⇒ Object
readonly
Returns the value of attribute active_when.
Class Method Summary collapse
-
.active_when_all(*attrs) ⇒ Object
Helper to build the
active_when
condition. -
.active_when_any(*attrs) ⇒ Object
Helper to build the
active_when
condition. -
.attribute(value = nil) ⇒ String
The
type
of usecase (i.e.:sync
,:transform
,:import
,:other
). -
.data_keys(source_data) ⇒ Array<String>
Helper to obtain the current internal named attributes of the data.
-
.dependencies(value = nil) ⇒ Object
Some parsers require dependencies to do their job.
-
.parsing_phase(phase = nil) ⇒ Object
Define or get the
phase
that theparser
kicks in. -
.serializing_phase(phase = nil) ⇒ Object
Define or get the
phase
that theserializer
kicks in.
Instance Method Summary collapse
-
#attribute ⇒ String, Symbol
The field/attribute or type this parser is linked to.
-
#initialize(person_parser) ⇒ Parser
constructor
A new instance of Parser.
- #parser(data, deps) ⇒ Object
- #seralizer(data, deps) ⇒ Object
Methods inherited from BaseLoader
<=>, created_at, #name, name_only_once!, set_created_at!
Methods included from ClassHelpers
#class_resolver, #descendants, #descendants?, #new_class, #resolve_class, #to_constant
Constructor Details
#initialize(person_parser) ⇒ Parser
Returns a new instance of Parser.
79 80 81 82 83 84 85 |
# File 'lib/eco/api/common/loaders/parser.rb', line 79 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_when ⇒ Object (readonly)
Returns the value of attribute active_when.
8 9 10 |
# File 'lib/eco/api/common/loaders/parser.rb', line 8 def active_when @active_when end |
Class Method Details
.active_when_all(*attrs) ⇒ Object
Helper to build the active_when
condition.
56 57 58 59 60 61 |
# File 'lib/eco/api/common/loaders/parser.rb', line 56 def active_when_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.
48 49 50 51 52 53 |
# File 'lib/eco/api/common/loaders/parser.rb', line 48 def active_when_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
Returns the type
of usecase (i.e. :sync
, :transform
, :import
, :other
).
14 15 16 17 18 19 20 |
# File 'lib/eco/api/common/loaders/parser.rb', line 14 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 |
.data_keys(source_data) ⇒ Array<String>
Helper to obtain the current internal named attributes of the data
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/eco/api/common/loaders/parser.rb', line 66 def data_keys(source_data) case source_data when Array keys = source_data when Hash keys = source_data.keys else keys = [] end end |
.dependencies(value = nil) ⇒ Object
Some parsers require dependencies to do their job.
23 24 25 26 27 |
# File 'lib/eco/api/common/loaders/parser.rb', line 23 def dependencies(value = nil) @dependencies ||= {} return @dependencies unless value @dependencies = value end |
.parsing_phase(phase = nil) ⇒ Object
Define or get the phase
that the parser
kicks in.
32 33 34 35 36 |
# File 'lib/eco/api/common/loaders/parser.rb', line 32 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.
41 42 43 44 45 |
# File 'lib/eco/api/common/loaders/parser.rb', line 41 def serializing_phase(phase = nil) @serializing_phase ||= :person return @serializing_phase unless phase @serializing_phase = phase end |
Instance Method Details
#attribute ⇒ String, Symbol
Returns the field/attribute or type this parser is linked to.
105 106 107 |
# File 'lib/eco/api/common/loaders/parser.rb', line 105 def attribute self.class.attribute end |
#parser(data, deps) ⇒ Object
91 92 93 |
# File 'lib/eco/api/common/loaders/parser.rb', line 91 def parser(data, deps) raise "You should implement this method" end |
#seralizer(data, deps) ⇒ Object
100 101 102 |
# File 'lib/eco/api/common/loaders/parser.rb', line 100 def seralizer(data, deps) raise "You should implement this method" end |