Class: Eco::API::Custom::Parser

Overview

Helper class to create a custom Parser

Examples:

Example of usage:

class Custom::Parser::LoginProviders < Eco::API::Custom::Parser
  attribute "login_provider_ids"

  active_when_any "some-field1", "some-field2" # optional
  parsing_phase     :final # optional, default is  (default :internal)
  serializing_phase :final # optional, it will be    (default :person)

  def parser(hash, deps)
    get_values(hash).map do |name|
      login_providers.to_id(name&.downcase.strip)
    end.compact
  end

  def serializer(hash, deps)
    if ids = hash["login_provider_ids"]
      login_providers.to_name(ids)
    else
      []
    end
  end

  private

  def get_values(hash)
    value, field1, field2 = hash.values_at("login_provider_ids", "some-field1", "some-field2")
    return value if value # at the :final parsing phase, login_provider_ids is already an Array
    some_logics(field1, field2)
  end

  def some_logics(a, b)
    case a
    when "staff"
      ["Company SSO"]
    when "contractor"
      ["Password"]
    elsif b
      return ["Password"] if b.end_with?("Owner")
      ["Password", "Company SSO 2"]
    end
  end

  def login_providers
    @login_providers ||= ASSETS.config.login_providers
  end

end

Instance Attribute Summary

Attributes inherited from Eco::API::Common::Loaders::CaseBase

#usecase

Attributes included from Language::AuxiliarLogger

#logger

Method Summary

Methods inherited from Eco::API::Common::Loaders::Parser

active_when_all, active_when_any, #attribute, attribute, dependencies, #initialize, #parser, parsing_phase, serializing_phase

Methods inherited from Eco::API::Common::Loaders::CaseBase

#name, name_only_once!

Methods inherited from Eco::API::Common::Loaders::Base

<=>, created_at, #initialize, #name, set_created_at!

Methods included from Eco::API::Common::ClassHelpers

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

Methods included from Language::AuxiliarLogger

#log

Constructor Details

This class inherits a constructor from Eco::API::Common::Loaders::Parser