Class: Eco::API::Common::People::PersonParser
- Inherits:
-
Object
- Object
- Eco::API::Common::People::PersonParser
- Extended by:
- ClassAutoLoader
- Defined in:
- lib/eco/api/common/people/person_parser.rb
Overview
Class to define/group a set of parsers/serializers.
Direct Known Subclasses
Constant Summary collapse
- CORE_ATTRS =
["id", "external_id", "email", "name", "supervisor_id", "filter_tags", "freemium"]
- ACCOUNT_ATTRS =
["policy_group_ids", "default_tag", "send_invites", "landing_page_id", "login_provider_ids"]
- TYPE =
[:select, :text, :date, :number, :phone_number, :boolean, :multiple]
- FORMAT =
[:csv, :xml, :json]
Scopping attributes (identifying, presence & active) collapse
-
#defined_attrs ⇒ Array<String>
readonly
Returns a list of all the internal attributes of the model that have a parser defined.
Instance Attribute Summary collapse
-
#all_attrs ⇒ Array<String>
readonly
all the internal name attributes, including core, account and details.
-
#details_attrs ⇒ Array<String>
readonly
internal names of schema details attributes.
-
#patch_version ⇒ Object
readonly
Returns the value of attribute patch_version.
-
#schema ⇒ Ecoportal::API::V1::PersonSchema?
readonly
schema of person details that this parser will be based upon.
Scopping attributes (identifying, presence & active) collapse
-
#active_attrs(source_data, phase = :any, process: :parse) ⇒ Array<String>
Returns a list of all the internal attributes of the model that have a parser defined & that should be active.
-
#defined?(attr) ⇒ Boolean
trueif the attributeattrhas parser defined, andfalseotherwise. -
#defined_list ⇒ Array<String>
Lists all defined attributes, types and formats.
-
#target_attrs_account(source_attrs = nil) ⇒ Array<String>
Scopes
source_attrsusing the schema account attributes. -
#target_attrs_core(source_attrs = nil) ⇒ Array<String>
Scopes
source_attrsusing the core attributes. -
#target_attrs_details(source_attrs = nil) ⇒ Array<String>
Scopes
source_attrsusing the schema details attributes. -
#undefined_attrs ⇒ Array<String>
Returns a list of all the internal attributes of the model that do not have a parser defined.
Defining attributes collapse
-
#define_attribute(attr, dependencies: {}) {|parser| ... } ⇒ Eco::API::Common::People::PersonParser
Helper to define and associate a parser/serializer to a type or attribute.
-
#merge(parser) ⇒ Eco::API::Common::People::PersonParser
Helper to merge a set of parsers of another
PersonParserinto the current object.
Launching parser/serializer collapse
-
#parse(attr, source, phase = :internal, deps: {}) ⇒ Any
Call to parser
sourcevalue of attribute or typeattrinto an internal valid value. -
#serialize(attr, object, phase = :person, deps: {}) ⇒ Object
Call to serialise
objectvalue of attribute or typeattrinto an external valid value.
Instance Method Summary collapse
-
#initialize(schema: nil) ⇒ PersonParser
constructor
A new instance of PersonParser.
- #new(schema: nil) ⇒ Object
- #patched! ⇒ Object
Methods included from ClassAutoLoader
_autoload_namespace, autoload_children, autoload_class?, autoload_namespace, autoload_namespace_ignore, autoloaded_children, autoloaded_class, autoloaded_namespaces, autoloads_children_of, known_class!, known_classes, new_classes, unloaded_children
Methods included from ClassHelpers
#class_resolver, #descendants, #descendants?, #new_class, #resolve_class, #to_constant
Constructor Details
#initialize(schema: nil) ⇒ PersonParser
Returns a new instance of PersonParser.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/eco/api/common/people/person_parser.rb', line 38 def initialize(schema: nil) raise "Constructor needs a PersonSchema. Given: #{schema}" if schema && !schema.is_a?(Ecoportal::API::V1::PersonSchema) @details_attrs = [] @parsers = {} @patch_version = 0 if schema @schema = Ecoportal::API::Internal::PersonSchema.new(JSON.parse(schema.doc.to_json)) @details_attrs = @schema&.fields.map { |fld| fld.alt_id } end @all_attrs = CORE_ATTRS + ACCOUNT_ATTRS + @details_attrs self.class.autoload_children(self) end |
Instance Attribute Details
#all_attrs ⇒ Array<String> (readonly)
all the internal name attributes, including core, account and details.
11 12 13 |
# File 'lib/eco/api/common/people/person_parser.rb', line 11 def all_attrs @all_attrs end |
#defined_attrs ⇒ Array<String> (readonly)
- it excludes any parser that is not in the model, such as type parsers (i.e.
:boolean,:multiple) - the list is sorted according
CORE_ATTRS+ACCOUNT_ATTRS+ schema attrs
Returns a list of all the internal attributes of the model that have a parser defined.
101 102 103 |
# File 'lib/eco/api/common/people/person_parser.rb', line 101 def defined_attrs @defined_attrs end |
#details_attrs ⇒ Array<String> (readonly)
internal names of schema details attributes.
11 12 13 |
# File 'lib/eco/api/common/people/person_parser.rb', line 11 def details_attrs @details_attrs end |
#patch_version ⇒ Object (readonly)
Returns the value of attribute patch_version.
24 25 26 |
# File 'lib/eco/api/common/people/person_parser.rb', line 24 def patch_version @patch_version end |
#schema ⇒ Ecoportal::API::V1::PersonSchema? (readonly)
schema of person details that this parser will be based upon.
11 12 13 |
# File 'lib/eco/api/common/people/person_parser.rb', line 11 def schema @schema end |
Instance Method Details
#active_attrs(source_data, phase = :any, process: :parse) ⇒ Array<String>
Returns a list of all the internal attributes of the model that have a parser defined & that should be active. Can be [:internal, :final, :person]
113 114 115 116 117 118 119 120 121 |
# File 'lib/eco/api/common/people/person_parser.rb', line 113 def active_attrs(source_data, phase = :any, process: :parse) defined_attrs.select do |attr| if process == :serialize @parsers[attr].serializer_active?(phase) else @parsers[attr].parser_active?(source_data, phase) end end end |
#define_attribute(attr, dependencies: {}) {|parser| ... } ⇒ Eco::API::Common::People::PersonParser
Helper to define and associate a parser/serializer to a type or attribute.
160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/eco/api/common/people/person_parser.rb', line 160 def define_attribute(attr, dependencies: {}, &definition) unless valid?(attr) msg = "The attribute '#{attr_to_str(attr)}' is not part of core, account or target schema, or does not match any type: #{@details_attrs}" raise msg end Eco::API::Common::People::PersonAttributeParser.new(attr, dependencies: dependencies).tap do |parser| @parsers[attr] = parser definition.call(parser) end patched! self end |
#defined?(attr) ⇒ Boolean
Returns true if the attribute attr has parser defined, and false otherwise.
132 133 134 |
# File 'lib/eco/api/common/people/person_parser.rb', line 132 def defined?(attr) @parsers.key?(attr) end |
#defined_list ⇒ Array<String>
Lists all defined attributes, types and formats.
92 93 94 |
# File 'lib/eco/api/common/people/person_parser.rb', line 92 def defined_list @parsers.keys end |
#merge(parser) ⇒ Eco::API::Common::People::PersonParser
if there are parsers with same name, it overrides the ones of the current object with them.
Helper to merge a set of parsers of another PersonParser into the current object.
143 144 145 146 147 148 149 |
# File 'lib/eco/api/common/people/person_parser.rb', line 143 def merge(parser) return self if !parser raise "Expected a PersonParser object. Given #{parser}" if !parser.is_a?(PersonParser) to_h.merge!(parser.to_h) patched! self end |
#new(schema: nil) ⇒ Object
57 58 59 |
# File 'lib/eco/api/common/people/person_parser.rb', line 57 def new(schema: nil) self.class.new(schema: schema || self.schema).merge(self) end |
#parse(attr, source, phase = :internal, deps: {}) ⇒ Any
dependencies introduced on parse call will be merged with those defined during the
initialization of the parser attr.
Call to parser source value of attribute or type attr into an internal valid value.
186 187 188 189 |
# File 'lib/eco/api/common/people/person_parser.rb', line 186 def parse(attr, source, phase = :internal, deps: {}) raise "There is no parser for attribute '#{attr}'" if !self.defined?(attr) @parsers[attr].parse(source, phase, dependencies: deps) end |
#patched! ⇒ Object
53 54 55 |
# File 'lib/eco/api/common/people/person_parser.rb', line 53 def patched! @patch_version += 1 end |
#serialize(attr, object, phase = :person, deps: {}) ⇒ Object
dependencies introduced on serialise call will be merged with those defined during the
initialization of the parser/serialiser attr.
Call to serialise object value of attribute or type attr into an external valid value.
201 202 203 204 |
# File 'lib/eco/api/common/people/person_parser.rb', line 201 def serialize(attr, object, phase = :person, deps: {}) raise "There is no parser for attribute '#{attr}'" if !self.defined?(attr) @parsers[attr].serialize(object, phase, dependencies: deps) end |
#target_attrs_account(source_attrs = nil) ⇒ Array<String>
use this helper to know which among your attributes are account ones.
Scopes source_attrs using the schema account attributes.
85 86 87 88 |
# File 'lib/eco/api/common/people/person_parser.rb', line 85 def target_attrs_account(source_attrs = nil) return ACCOUNT_ATTRS if !source_attrs scoped_attrs(source_attrs, ACCOUNT_ATTRS) end |
#target_attrs_core(source_attrs = nil) ⇒ Array<String>
use this helper to know which among your attributes are core ones.
Scopes source_attrs using the core attributes.
67 68 69 70 |
# File 'lib/eco/api/common/people/person_parser.rb', line 67 def target_attrs_core(source_attrs = nil) return CORE_ATTRS if !source_attrs scoped_attrs(source_attrs, CORE_ATTRS) end |
#target_attrs_details(source_attrs = nil) ⇒ Array<String>
use this helper to know which among your attributes are schema details ones.
Scopes source_attrs using the schema details attributes.
76 77 78 79 |
# File 'lib/eco/api/common/people/person_parser.rb', line 76 def target_attrs_details(source_attrs = nil) return @details_attrs if !source_attrs scoped_attrs(source_attrs, @details_attrs) end |
#undefined_attrs ⇒ Array<String>
it excludes any parser that is not in the model, such as type parsers (i.e. :boolean, :multiple)
Returns a list of all the internal attributes of the model that do not have a parser defined.
126 127 128 |
# File 'lib/eco/api/common/people/person_parser.rb', line 126 def undefined_attrs all_attrs - defined_attrs end |