Class: 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 =
%w[ id external_id email name supervisor_id filter_tags contractor_organization_id freemium brand_id ].freeze
- ACCOUNT_ATTRS =
%w[ policy_group_ids default_tag send_invites landing_page_id login_provider_ids ].freeze
- TYPE =
%i[ select text date number phone_number boolean multiple ].freeze
- FORMAT =
%i[csv xml json xls].freeze
Instance Attribute Summary collapse
-
#all_model_attrs ⇒ Array<String>
readonly
all the internal name attributes, including core, account and details.
-
#details_attrs ⇒ Array<String>
readonly
internal names of chema 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.
selection options of all select attributes collapse
-
#select_tables ⇒ Object
Select Options.
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.
-
#all_attrs(include_defined_parsers: false) ⇒ Object
All the internal name attributes, including core, account and details.
-
#defined?(attr) ⇒ Boolean
true
if the attributeattr
has parser defined, andfalse
otherwise. -
#defined_attrs ⇒ Array<String>
Returns a list of all the internal attributes that have a parser defined.
-
#defined_list ⇒ Array<String>
Lists all defined attributes, types and formats.
-
#defined_model_attrs ⇒ Array<String>
Returns a list of all the internal attributes of the model that have a parser defined.
- #required_attrs ⇒ Array<Eco::API::Common::Loaders::Parser::RequiredAttrs>
-
#symbol_keys ⇒ Array<Symbol>
Symbol keys are type or import parsers (that do not belong to the model).
-
#target_attrs_account(source_attrs = nil) ⇒ Array<String>
Scopes
source_attrs
using the schema account attributes. -
#target_attrs_core(source_attrs = nil) ⇒ Array<String>
Scopes
source_attrs
using the core attributes. -
#target_attrs_details(source_attrs = nil) ⇒ Array<String>
Scopes
source_attrs
using the schema details attributes. -
#undefined_model_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
PersonParser
into the current object.
Launching parser/serializer collapse
-
#parse(attr, source, phase = :internal, deps: {}) ⇒ Any
Call to parser
source
value of attribute or typeattr
into an internal valid value. -
#serialize(attr, object, phase = :person, deps: {}) ⇒ Object
Call to serialise
object
value of attribute or typeattr
into 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?, #inheritable_attrs, #inheritable_class_vars, #inherited, #instance_variable_name, #new_class, #resolve_class, #to_constant
Constructor Details
#initialize(schema: nil) ⇒ PersonParser
Returns a new instance of PersonParser.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/eco/api/common/people/person_parser.rb', line 53 def initialize(schema: nil) msg = "Constructor needs a PersonSchema. Given: #{schema.class}" raise msg 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(&:alt_id) end @all_model_attrs = CORE_ATTRS + ACCOUNT_ATTRS + @details_attrs self.class.autoload_children(self) end |
Instance Attribute Details
#all_model_attrs ⇒ Array<String> (readonly)
all the internal name attributes, including core, account and details.
13 14 15 |
# File 'lib/eco/api/common/people/person_parser.rb', line 13 def all_model_attrs @all_model_attrs end |
#details_attrs ⇒ Array<String> (readonly)
internal names of chema details attributes.
13 14 15 |
# File 'lib/eco/api/common/people/person_parser.rb', line 13 def details_attrs @details_attrs end |
#patch_version ⇒ Object (readonly)
Returns the value of attribute patch_version.
38 39 40 |
# File 'lib/eco/api/common/people/person_parser.rb', line 38 def patch_version @patch_version end |
#schema ⇒ Ecoportal::API::V1::PersonSchema? (readonly)
schema of person details that this parser will be based upon.
13 14 15 |
# File 'lib/eco/api/common/people/person_parser.rb', line 13 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]
189 190 191 192 193 194 195 196 197 |
# File 'lib/eco/api/common/people/person_parser.rb', line 189 def active_attrs(source_data, phase = :any, process: :parse) defined_model_attrs.select do |attr| if process == :serialize @parsers[attr].serializer_active?(phase) else @parsers[attr].parser_active?(source_data, phase) end end end |
#all_attrs(include_defined_parsers: false) ⇒ Object
All the internal name attributes, including core, account and details.
105 106 107 108 109 |
# File 'lib/eco/api/common/people/person_parser.rb', line 105 def all_attrs(include_defined_parsers: false) return all_model_attrs | defined_model_attrs if include_defined_parsers all_model_attrs end |
#define_attribute(attr, dependencies: {}) {|parser| ... } ⇒ Eco::API::Common::People::PersonParser
Helper to define and associate a parser/serializer to a type or attribute.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/eco/api/common/people/person_parser.rb', line 239 def define_attribute(attr, dependencies: {}, &definition) unless valid?(attr) msg = "The attribute '#{attr_to_str(attr)}' is not part of " msg << "core, account or target schema, or does " msg << "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.
208 209 210 |
# File 'lib/eco/api/common/people/person_parser.rb', line 208 def defined?(attr) @parsers.key?(attr) end |
#defined_attrs ⇒ Array<String>
These attributes do not necessarily belong to the model. They could be virtual attributes
Returns a list of all the internal attributes that have a parser defined.
156 157 158 |
# File 'lib/eco/api/common/people/person_parser.rb', line 156 def defined_attrs defined_list - symbol_keys end |
#defined_list ⇒ Array<String>
Lists all defined attributes, types and formats.
149 150 151 |
# File 'lib/eco/api/common/people/person_parser.rb', line 149 def defined_list @parsers.keys end |
#defined_model_attrs ⇒ Array<String>
- 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.
165 166 167 168 169 |
# File 'lib/eco/api/common/people/person_parser.rb', line 165 def defined_model_attrs defined = @parsers.keys defined = (all_model_attrs | defined) & defined defined - symbol_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.
219 220 221 222 223 224 225 226 227 228 |
# File 'lib/eco/api/common/people/person_parser.rb', line 219 def merge(parser) return self unless parser msg = "Expected a PersonParser object. Given #{parser.class}" raise msg unless parser.is_a?(PersonParser) to_h.merge!(parser.to_h) patched! self end |
#new(schema: nil) ⇒ Object
74 75 76 |
# File 'lib/eco/api/common/people/person_parser.rb', line 74 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.
272 273 274 275 276 277 278 279 280 |
# File 'lib/eco/api/common/people/person_parser.rb', line 272 def parse(attr, source, phase = :internal, deps: {}) msg = "There is no parser for attribute '#{attr}'" raise msg unless self.defined?(attr) @parsers[attr].parse(source, phase, dependencies: deps) do |_dkey, dval| next dval unless dval.is_a?(Proc) dval.call(self) end end |
#patched! ⇒ Object
70 71 72 |
# File 'lib/eco/api/common/people/person_parser.rb', line 70 def patched! @patch_version += 1 end |
#required_attrs ⇒ Array<Eco::API::Common::Loaders::Parser::RequiredAttrs>
100 101 102 |
# File 'lib/eco/api/common/people/person_parser.rb', line 100 def required_attrs @parsers.values_at(*all_attrs(include_defined_parsers: true)).compact.map(&:required_attrs).compact end |
#select_tables ⇒ Object
Select Options
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/eco/api/common/people/person_parser.rb', line 81 def select_tables return nil unless @schema @select_tables ||= @schema.fields.select do |fld| fld.type == "select" end.to_h do |fld| msg = "The schema selection field '#{fld.name}' is missing selection options." raise msg unless fld.&.any? = fld..to_h { |v| [v.downcase.strip, v] } [fld.alt_id, ] end 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.
292 293 294 295 296 297 298 299 300 |
# File 'lib/eco/api/common/people/person_parser.rb', line 292 def serialize(attr, object, phase = :person, deps: {}) msg = "There is no parser for attribute '#{attr}'" raise msg unless self.defined?(attr) @parsers[attr].serialize(object, phase, dependencies: deps) do |_dkey, dval| next dval unless dval.is_a?(Proc) dval.call(self) end end |
#symbol_keys ⇒ Array<Symbol>
this was introduced to boost virtual fields to treat in different phases of the parsing process
Symbol keys are type or import parsers (that do not belong to the model)
174 175 176 |
# File 'lib/eco/api/common/people/person_parser.rb', line 174 def symbol_keys @parsers.keys.select {|k| k.is_a?(Symbol)} 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.
141 142 143 144 145 |
# File 'lib/eco/api/common/people/person_parser.rb', line 141 def target_attrs_account(source_attrs = nil) return ACCOUNT_ATTRS unless 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.
117 118 119 120 121 |
# File 'lib/eco/api/common/people/person_parser.rb', line 117 def target_attrs_core(source_attrs = nil) return CORE_ATTRS unless 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.
129 130 131 132 133 |
# File 'lib/eco/api/common/people/person_parser.rb', line 129 def target_attrs_details(source_attrs = nil) return @details_attrs unless source_attrs scoped_attrs(source_attrs, @details_attrs) end |
#undefined_model_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.
202 203 204 |
# File 'lib/eco/api/common/people/person_parser.rb', line 202 def undefined_model_attrs all_model_attrs - defined_model_attrs end |