Class: AdvancedBilling::Price

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/advanced_billing/models/price.rb

Overview

Price Model.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

#to_hash, #to_json

Constructor Details

#initialize(starting_quantity:, unit_price:, ending_quantity: SKIP, additional_properties: {}) ⇒ Price

Returns a new instance of Price.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/advanced_billing/models/price.rb', line 48

def initialize(starting_quantity:, unit_price:, ending_quantity: SKIP,
               additional_properties: {})
  @starting_quantity = starting_quantity
  @ending_quantity = ending_quantity unless ending_quantity == SKIP
  @unit_price = unit_price

  # Add additional model properties to the instance.
  additional_properties.each do |_name, _value|
    instance_variable_set("@#{_name}", _value)
  end
end

Instance Attribute Details

#ending_quantityObject

TODO: Write general description for this method

Returns:

  • (Object)


18
19
20
# File 'lib/advanced_billing/models/price.rb', line 18

def ending_quantity
  @ending_quantity
end

#starting_quantityObject

TODO: Write general description for this method

Returns:

  • (Object)


14
15
16
# File 'lib/advanced_billing/models/price.rb', line 14

def starting_quantity
  @starting_quantity
end

#unit_priceObject

The price can contain up to 8 decimal places. i.e. 1.00 or 0.0012 or 0.00000065

Returns:

  • (Object)


23
24
25
# File 'lib/advanced_billing/models/price.rb', line 23

def unit_price
  @unit_price
end

Class Method Details

.from_hash(hash) ⇒ Object

Creates an instance of the object from a hash.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/advanced_billing/models/price.rb', line 61

def self.from_hash(hash)
  return nil unless hash

  # Extract variables from the hash.
  starting_quantity = hash.key?('starting_quantity') ? APIHelper.deserialize_union_type(
    UnionTypeLookUp.get(:PriceStartingQuantity), hash['starting_quantity']
  ) : nil
  unit_price = hash.key?('unit_price') ? APIHelper.deserialize_union_type(
    UnionTypeLookUp.get(:PriceUnitPrice), hash['unit_price']
  ) : nil
  ending_quantity = hash.key?('ending_quantity') ? APIHelper.deserialize_union_type(
    UnionTypeLookUp.get(:PriceEndingQuantity), hash['ending_quantity']
  ) : SKIP

  # Clean out expected properties from Hash.
  names.each_value { |k| hash.delete(k) }

  # Create object from extracted values.
  Price.new(starting_quantity: starting_quantity,
            unit_price: unit_price,
            ending_quantity: ending_quantity,
            additional_properties: hash)
end

.namesObject

A mapping from model property names to API property names.



26
27
28
29
30
31
32
# File 'lib/advanced_billing/models/price.rb', line 26

def self.names
  @_hash = {} if @_hash.nil?
  @_hash['starting_quantity'] = 'starting_quantity'
  @_hash['ending_quantity'] = 'ending_quantity'
  @_hash['unit_price'] = 'unit_price'
  @_hash
end

.nullablesObject

An array for nullable fields



42
43
44
45
46
# File 'lib/advanced_billing/models/price.rb', line 42

def self.nullables
  %w[
    ending_quantity
  ]
end

.optionalsObject

An array for optional fields



35
36
37
38
39
# File 'lib/advanced_billing/models/price.rb', line 35

def self.optionals
  %w[
    ending_quantity
  ]
end

.validate(value) ⇒ Object

Validates an instance of the object from a given value.

Parameters:

  • The (Price | Hash)

    value against the validation is performed.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/advanced_billing/models/price.rb', line 87

def self.validate(value)
  if value.instance_of? self
    return (
      UnionTypeLookUp.get(:PriceStartingQuantity)
                     .validate(value.starting_quantity) and
        UnionTypeLookUp.get(:PriceUnitPrice)
                       .validate(value.unit_price)
    )
  end

  return false unless value.instance_of? Hash

  (
    UnionTypeLookUp.get(:PriceStartingQuantity)
                   .validate(value['starting_quantity']) and
      UnionTypeLookUp.get(:PriceUnitPrice)
                     .validate(value['unit_price'])
  )
end