Class: Eco::API::Common::People::PersonFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/eco/api/common/people/person_factory.rb

Overview

Helper factory to build Ecoportal::API::V1::Person or Ecoportal::API::Internal::Person objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(person: nil, schema: {}, account: {}, modifier: Common::People::PersonModifier.new) ⇒ PersonFactory

Returns a new instance of PersonFactory.



11
12
13
14
15
16
17
# File 'lib/eco/api/common/people/person_factory.rb', line 11

def initialize(person: nil, schema: {}, account: {}, modifier: Common::People::PersonModifier.new)
  @modifier     = Common::People::PersonModifier.new(modifier)
  @person       = person
  @account      = 
  @schema       = schema
  @schema_attrs = @schema&.fields&.map(&:alt_id)
end

Instance Attribute Details

#schemaEcoportal::API::V1::PersonSchema (readonly)

person schema to be used in this person factory

Returns:

  • (Ecoportal::API::V1::PersonSchema)

    the current value of schema



8
9
10
# File 'lib/eco/api/common/people/person_factory.rb', line 8

def schema
  @schema
end

#schema_attrsArray<String> (readonly)

inernal names of the schema fields/attributes

Returns:

  • (Array<String>)

    the current value of schema_attrs



8
9
10
# File 'lib/eco/api/common/people/person_factory.rb', line 8

def schema_attrs
  @schema_attrs
end

Instance Method Details

#new(person: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/eco/api/common/people/person_factory.rb', line 19

def new(person: nil)
  in_raw_modifier = Common::People::PersonModifier.new.no_details

  if person
    return PersonFactory.new(
      person:   person,
      schema:   @schema,
      modifier: in_raw_modifier
    ).new
  end

  person = klass.new(person_hash(@person))
  person. = (@account) if @modifier.add_account? && @modifier.internal?
  person.add_details(@schema) unless @modifier.no_details?
  person
end

#new_contact(schema = @schema) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/eco/api/common/people/person_factory.rb', line 36

def new_contact(schema = @schema)
  factory = self.class.new(
    @modifier.,
    person:  @person,
    schema:  schema,
    account: @account
  )
  factory.new
end

#new_docObject



46
47
48
# File 'lib/eco/api/common/people/person_factory.rb', line 46

def new_doc
  new_hash(@modifier)
end

#schema_idObject



50
51
52
53
# File 'lib/eco/api/common/people/person_factory.rb', line 50

def schema_id
  nil unless @schema
  @schema['schema_id'] || @schema['id']
end