Class: Fortnox::API::Model::Base

Inherits:
Types::Model show all
Defined in:
lib/fortnox/api/models/base.rb

Direct Known Subclasses

Article, Customer, Invoice, Label, Order, Project

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Types::Model

#initialize

Constructor Details

This class inherits a constructor from Fortnox::API::Types::Model

Instance Attribute Details

#parentObject

TODO(jonas): Restructure this class a bit, it is not very readable.



11
12
13
# File 'lib/fortnox/api/models/base.rb', line 11

def parent
  @parent
end

#unsavedObject

TODO(jonas): Restructure this class a bit, it is not very readable.



11
12
13
# File 'lib/fortnox/api/models/base.rb', line 11

def unsaved
  @unsaved
end

Class Method Details

.attribute(name, *args) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/fortnox/api/models/base.rb', line 13

def self.attribute( name, *args )
  define_method( "#{ name }?" ) do
    !send( name ).nil?
  end

  super
end

.new(hash) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fortnox/api/models/base.rb', line 21

def self.new( hash )
  begin
    obj = preserve_meta_properties( hash ) do
      super( hash )
    end
  rescue Dry::Struct::Error => e
    raise Fortnox::API::AttributeError.new e
  end

  IceNine.deep_freeze( obj )
end

.preserve_meta_properties(hash) ⇒ Object

dry-types filter anything that isn’t specified as an attribute on the class that is being instantiated. This wrapper preserves the meta properties we need to track object state during that initilisation and sets them on the object after dry-types is done with it.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fortnox/api/models/base.rb', line 85

def self.preserve_meta_properties( hash )
  is_unsaved = hash.delete( :unsaved ){ true }
  is_new = hash.delete( :new ){ true }
  parent = hash.delete( :parent ){ nil }

  obj = yield

  # TODO: remove new, unsaved, saved
  obj.instance_variable_set( :@unsaved, is_unsaved )
  obj.instance_variable_set( :@saved, !is_unsaved )
  obj.instance_variable_set( :@new, is_new )
  obj.instance_variable_set( :@parent, parent )

  return obj
end

Instance Method Details

#==(other) ⇒ Object

Generic comparison, by value, use .eql? or .equal? for object identity.



50
51
52
53
# File 'lib/fortnox/api/models/base.rb', line 50

def ==( other )
  return false unless other.is_a? self.class
  self.to_hash == other.to_hash
end

#new?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/fortnox/api/models/base.rb', line 55

def new?
  @new
end

#parent?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/fortnox/api/models/base.rb', line 63

def parent?
  not @parent.nil?
end

#saved?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/fortnox/api/models/base.rb', line 59

def saved?
  @saved
end

#to_hash(recursive = false) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/fortnox/api/models/base.rb', line 71

def to_hash( recursive = false )
  return super() if recursive

  self.class.schema.keys.each_with_object({}) do |key, result|
    result[key] = self[key]
  end
end

#unique_idObject



33
34
35
# File 'lib/fortnox/api/models/base.rb', line 33

def unique_id
  send( self.class::UNIQUE_ID )
end

#update(hash) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fortnox/api/models/base.rb', line 37

def update( hash )
  old_attributes = self.to_hash
  new_attributes = old_attributes.merge( hash )

  return self if new_attributes == old_attributes

  new_hash = new_attributes.delete_if{ |_, value| value.nil? }
  new_hash[:new] = @new
  new_hash[:parent] = self
  self.class.new( new_hash )
end