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

Inherits:
Object
  • Object
show all
Defined in:
lib/fortnox/api/models/base.rb

Direct Known Subclasses

Customer, EDIInformation, EmailInformation, Invoice, Row

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Base

Returns a new instance of Base.



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

def initialize( hash = {} )
  unsaved = hash.delete( :unsaved ){ true }
  @saved = !unsaved
  @new = hash.delete( :new ){ true }

  # .each{|a| p a.name}

  super hash
  IceNine.deep_freeze( self )
end

Instance Attribute Details

#unsavedObject

Returns the value of attribute unsaved.



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

def unsaved
  @unsaved
end

Class Method Details

.attribute(name, *args) ⇒ Object



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

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

  super
end

Instance Method Details

#==(other) ⇒ Object

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



45
46
47
48
# File 'lib/fortnox/api/models/base.rb', line 45

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

#new?Boolean

Returns:

  • (Boolean)


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

def new?
  @new
end

#saved?Boolean

Returns:

  • (Boolean)


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

def saved?
  @saved
end

#update(hash) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/fortnox/api/models/base.rb', line 33

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
  self.class.new( new_hash )
end