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

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

Direct Known Subclasses

Article, Customer, Document, Label, Project, TermsOfPayment, Unit

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



94
95
96
# File 'lib/fortnox/api/models/base.rb', line 94

def parent
  @parent || self.class.new(self.class::STUB.dup)
end

#unsavedObject

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



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

def unsaved
  @unsaved
end

Class Method Details

.attribute(name, *args) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/fortnox/api/models/base.rb', line 16

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

  super
end

.new(hash = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fortnox/api/models/base.rb', line 24

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

  IceNine.deep_freeze(obj)
end

.stubObject



36
37
38
# File 'lib/fortnox/api/models/base.rb', line 36

def self.stub
  new(self::STUB.dup)
end

Instance Method Details

#==(other) ⇒ Object

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



76
77
78
79
80
# File 'lib/fortnox/api/models/base.rb', line 76

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

  to_hash == other.to_hash
end

#attributes(*options) ⇒ Object

This filtering logic could be improved since it is currently O(N*M).



45
46
47
48
49
50
51
52
53
# File 'lib/fortnox/api/models/base.rb', line 45

def attributes(*options)
  return self.class.schema if options.nil?

  options = Array(options)

  self.class.schema.find_all do |_name, attribute|
    options.all? { |option| attribute.is?(option) }
  end
end

#new?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/fortnox/api/models/base.rb', line 82

def new?
  @new
end

#parent?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/fortnox/api/models/base.rb', line 90

def parent?
  !@parent.nil?
end

#saved?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/fortnox/api/models/base.rb', line 86

def saved?
  @saved
end

#to_hash(recursive = false) ⇒ Object

rubocop:disable Style/OptionalBooleanParameter



55
56
57
58
59
60
61
62
# File 'lib/fortnox/api/models/base.rb', line 55

def to_hash(recursive = false) # rubocop:disable Style/OptionalBooleanParameter
  return super() if recursive

  self.class.schema.each_with_object({}) do |key, result|
    # Only output attributes that have a value set
    result[key.name] = self[key.name] if send("#{key.name}?")
  end
end

#unique_idObject



40
41
42
# File 'lib/fortnox/api/models/base.rb', line 40

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

#update(hash) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/fortnox/api/models/base.rb', line 64

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

  return self if new_attributes == old_attributes

  new_attributes[:new] = @new
  new_attributes[:parent] = self
  self.class.new(new_attributes)
end