Class: Eco::API::Common::People::PersonParser
- Inherits:
-
Object
- Object
- Eco::API::Common::People::PersonParser
- 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"]
- 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]
Instance Attribute Summary collapse
-
#all_attrs ⇒ Array<String>
readonly
all the internal name attributes, including core, account and details.
-
#defined_attrs ⇒ Array<String>
readonly
Returns a list of all the internal attributes of the model that have a parser defined.
-
#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.
Instance Method Summary collapse
-
#active_attrs(source_data) ⇒ Array<String>
Returns a list of all the internal attributes of the model that have a parser defined & that should be active.
-
#define_attribute(attr, dependencies: {}) {|parser| ... } ⇒ Eco::API::Common::People::PersonParser
Helper to define and associate a parser/serializer to a type or attribute.
-
#defined?(attr) ⇒ Boolean
trueif the attributeattrhas parser defined, andfalseotherwise. -
#initialize(schema: nil) ⇒ PersonParser
constructor
A new instance of PersonParser.
-
#merge(parser) ⇒ Eco::API::Common::People::PersonParser
Helper to merge a set of parsers of another
PersonParserinto the current object. -
#parse(attr, source, deps: {}) ⇒ Any
Call to parser
sourcevalue of attribute or typeattrinto an internal valid value. - #patched! ⇒ Object
-
#serialize(attr, object, deps: {}) ⇒ Object
Call to serialise
objectvalue of attribute or typeattrinto an external valid value. -
#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.
Constructor Details
#initialize(schema: nil) ⇒ PersonParser
Returns a new instance of PersonParser.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/eco/api/common/people/person_parser.rb', line 34 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 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.
90 91 92 |
# File 'lib/eco/api/common/people/person_parser.rb', line 90 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.
20 21 22 |
# File 'lib/eco/api/common/people/person_parser.rb', line 20 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) ⇒ Array<String>
Returns a list of all the internal attributes of the model that have a parser defined & that should be active.
99 100 101 |
# File 'lib/eco/api/common/people/person_parser.rb', line 99 def active_attrs(source_data) defined_attrs.select {|attr| @parsers[attr].parser_active?(source_data)} end |
#define_attribute(attr, dependencies: {}) {|parser| ... } ⇒ Eco::API::Common::People::PersonParser
Helper to define and associate a parser/serializer to a type or attribute.
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/eco/api/common/people/person_parser.rb', line 138 def define_attribute(attr, dependencies: {}, &definition) if !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.
112 113 114 |
# File 'lib/eco/api/common/people/person_parser.rb', line 112 def defined?(attr) @parsers.key?(attr) 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.
120 121 122 123 124 125 126 |
# File 'lib/eco/api/common/people/person_parser.rb', line 120 def merge(parser) return self if !parser raise "Expected a PersonParser object. Given #{parser}" if !parser.is_a?(PersonParser) @parsers.merge!(parser.hash) patched! self end |
#parse(attr, source, 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.
159 160 161 162 |
# File 'lib/eco/api/common/people/person_parser.rb', line 159 def parse(attr, source, deps: {}) raise "There is no parser for attribute '#{attr}'" if !self.defined?(attr) @parsers[attr].parse(source, dependencies: deps) end |
#patched! ⇒ Object
48 49 50 |
# File 'lib/eco/api/common/people/person_parser.rb', line 48 def patched! @patch_version += 1 end |
#serialize(attr, object, 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.
172 173 174 175 |
# File 'lib/eco/api/common/people/person_parser.rb', line 172 def serialize(attr, object, deps: {}) raise "There is no parser for attribute '#{attr}'" if !self.defined?(attr) @parsers[attr].serialize(object, 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.
74 75 76 77 |
# File 'lib/eco/api/common/people/person_parser.rb', line 74 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.
56 57 58 59 |
# File 'lib/eco/api/common/people/person_parser.rb', line 56 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.
65 66 67 68 |
# File 'lib/eco/api/common/people/person_parser.rb', line 65 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.
106 107 108 |
# File 'lib/eco/api/common/people/person_parser.rb', line 106 def undefined_attrs all_attrs - defined_attrs end |