Class: Fortnox::Mappers::Struct

Inherits:
Object
  • Object
show all
Defined in:
lib/fortnox/mappers/struct.rb

Overview

Base class for declarative struct mappers. Subclasses declare the target struct and any API key overrides (acronyms like “EDIInformation” that the PascalCase convention can’t derive). Read-only attributes are excluded from serialisation.

class EmailInformation < Struct
  struct    Structs::EmailInformation
  overrides email_address_cc: 'EmailAddressCC'
end

Direct Known Subclasses

DocumentRow, EDIInformation, EmailInformation

Constant Summary collapse

CONVENTION =
RestEasy::Conventions::PascalCase.new

Class Method Summary collapse

Class Method Details

.for(struct_class) ⇒ Object



18
19
20
# File 'lib/fortnox/mappers/struct.rb', line 18

def for(struct_class)
  Class.new(self) { struct struct_class }
end

.overrides(map) ⇒ Object



26
27
28
# File 'lib/fortnox/mappers/struct.rb', line 26

def overrides(map)
  @overrides = map
end

.parse(data) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/fortnox/mappers/struct.rb', line 30

def parse(data)
  return nil if data.nil?

  attributes = data.transform_keys do |api_key|
    api_to_model_map[api_key] || CONVENTION.parse(api_key)
  end
  struct_class.new(attributes)
end

.serialise(struct) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fortnox/mappers/struct.rb', line 39

def serialise(struct)
  return nil if struct.nil?

  excluded = if struct.class.respond_to?(:read_only_attributes)
               struct.class.read_only_attributes
             else
               []
             end
  struct.to_h.except(*excluded).transform_keys do |key|
    model_to_api_map[key] || CONVENTION.serialise(key)
  end
end

.struct(klass) ⇒ Object



22
23
24
# File 'lib/fortnox/mappers/struct.rb', line 22

def struct(klass)
  @struct_class = klass
end