Class: Dolibarr::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/dolibarr/record.rb

Overview

Base for the gem's value objects (Invoice, Payment, Thirdparty). A thin, read-only view over a normalised response hash: amounts are already coerced to BigDecimal by Coerce at construction, string keys are preserved, and the full payload stays reachable through #raw / #[] so nothing the API returned is ever hidden.

Direct Known Subclasses

Invoice, Payment, Thirdparty

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Record

Returns a new instance of Record.



18
19
20
# File 'lib/dolibarr/record.rb', line 18

def initialize(raw)
  @raw = raw || {}
end

Instance Attribute Details

#rawHash (readonly)

Returns the normalised (amount-coerced) payload.

Returns:

  • (Hash)

    the normalised (amount-coerced) payload



10
11
12
# File 'lib/dolibarr/record.rb', line 10

def raw
  @raw
end

Class Method Details

.from(hash) ⇒ Object

Build from a raw parsed response hash, coercing amounts.

Parameters:

  • hash (Hash)


14
15
16
# File 'lib/dolibarr/record.rb', line 14

def self.from(hash)
  new(Coerce.amounts(hash))
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Two records are equal when they are the same class and wrap the same payload.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


36
37
38
# File 'lib/dolibarr/record.rb', line 36

def ==(other)
  other.is_a?(self.class) && raw == other.raw
end

#[](key) ⇒ Object

Read any field by its Dolibarr key.

Parameters:

  • key (String, Symbol)


24
25
26
# File 'lib/dolibarr/record.rb', line 24

def [](key)
  raw[key.to_s]
end

#hashInteger

Returns a hash consistent with #==, so records key Hashes / Sets correctly.

Returns:

  • (Integer)

    a hash consistent with #==, so records key Hashes / Sets correctly



42
43
44
# File 'lib/dolibarr/record.rb', line 42

def hash
  raw.hash
end

#idString?

Returns the object id (Dolibarr returns it as a string).

Returns:

  • (String, nil)

    the object id (Dolibarr returns it as a string)



29
30
31
# File 'lib/dolibarr/record.rb', line 29

def id
  self['id']
end

#to_hHash

Returns the normalised payload.

Returns:

  • (Hash)

    the normalised payload



47
48
49
# File 'lib/dolibarr/record.rb', line 47

def to_h
  raw
end