Class: NewStoreApi::FixedDiscount

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/new_store_api/models/fixed_discount.rb

Overview

A fixed monetary discount.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

#check_for_conflict, #process_additional_properties, #process_array, #process_basic_value, #process_hash, #to_hash, #to_json

Constructor Details

#initialize(applied_amount = nil, id = nil, original_amount = nil, scope = nil, coupon_code = SKIP, description = SKIP, external_reference = SKIP) ⇒ FixedDiscount

Returns a new instance of FixedDiscount.



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/new_store_api/models/fixed_discount.rb', line 83

def initialize(applied_amount = nil, id = nil, original_amount = nil,
               scope = nil, coupon_code = SKIP, description = SKIP,
               external_reference = SKIP)
  @applied_amount = applied_amount
  @coupon_code = coupon_code unless coupon_code == SKIP
  @description = description unless description == SKIP
  @external_reference = external_reference unless external_reference == SKIP
  @id = id
  @original_amount = original_amount
  @scope = scope
  @type = 'fixed'
end

Instance Attribute Details

#applied_amountInteger

The discount amount applied to this line item, in the currency's minor unit. For a discount prorated from an order-level promotion, this is only this line's share.

Returns:

  • (Integer)


16
17
18
# File 'lib/new_store_api/models/fixed_discount.rb', line 16

def applied_amount
  @applied_amount
end

#coupon_codeString

The coupon code that triggered the discount, when one was used.

Returns:

  • (String)


20
21
22
# File 'lib/new_store_api/models/fixed_discount.rb', line 20

def coupon_code
  @coupon_code
end

#descriptionString

Human-readable description of why the discount was applied.

Returns:

  • (String)


24
25
26
# File 'lib/new_store_api/models/fixed_discount.rb', line 24

def description
  @description
end

#external_referenceString

To identify the discount in the external system.

Returns:

  • (String)


28
29
30
# File 'lib/new_store_api/models/fixed_discount.rb', line 28

def external_reference
  @external_reference
end

#idUUID | String

ID of the discount; shared across all lines from the same promotion so downstream can aggregate them.

Returns:

  • (UUID | String)


33
34
35
# File 'lib/new_store_api/models/fixed_discount.rb', line 33

def id
  @id
end

#original_amountInteger

The fixed discount rule's full amount, in the currency's minor unit. For an item-level discount this equals applied_amount; for an order-level discount it is the entire promotion's amount (e.g. $10 off the order), while applied_amount is this line's prorated share of it.

Returns:

  • (Integer)


40
41
42
# File 'lib/new_store_api/models/fixed_discount.rb', line 40

def original_amount
  @original_amount
end

#scopeLevelEnum

Whether the discount was applied at the item level or prorated from an order-level discount.

Returns:



45
46
47
# File 'lib/new_store_api/models/fixed_discount.rb', line 45

def scope
  @scope
end

#typeString (readonly)

Discriminates a fixed monetary discount.

Returns:

  • (String)


49
50
51
# File 'lib/new_store_api/models/fixed_discount.rb', line 49

def type
  @type
end

Class Method Details

.from_hash(hash) ⇒ Object

Creates an instance of the object from a hash.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/new_store_api/models/fixed_discount.rb', line 97

def self.from_hash(hash)
  return nil unless hash

  # Extract variables from the hash.
  applied_amount =
    hash.key?('applied_amount') ? hash['applied_amount'] : nil
  id = hash.key?('id') ? hash['id'] : nil
  original_amount =
    hash.key?('original_amount') ? hash['original_amount'] : nil
  scope = hash.key?('scope') ? hash['scope'] : nil
  coupon_code = hash.key?('coupon_code') ? hash['coupon_code'] : SKIP
  description = hash.key?('description') ? hash['description'] : SKIP
  external_reference =
    hash.key?('external_reference') ? hash['external_reference'] : SKIP

  # Create object from extracted values.
  FixedDiscount.new(applied_amount,
                    id,
                    original_amount,
                    scope,
                    coupon_code,
                    description,
                    external_reference)
end

.namesObject

A mapping from model property names to API property names.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/new_store_api/models/fixed_discount.rb', line 52

def self.names
  @_hash = {} if @_hash.nil?
  @_hash['applied_amount'] = 'applied_amount'
  @_hash['coupon_code'] = 'coupon_code'
  @_hash['description'] = 'description'
  @_hash['external_reference'] = 'external_reference'
  @_hash['id'] = 'id'
  @_hash['original_amount'] = 'original_amount'
  @_hash['scope'] = 'scope'
  @_hash['type'] = 'type'
  @_hash
end

.nullablesObject

An array for nullable fields



75
76
77
78
79
80
81
# File 'lib/new_store_api/models/fixed_discount.rb', line 75

def self.nullables
  %w[
    coupon_code
    description
    external_reference
  ]
end

.optionalsObject

An array for optional fields



66
67
68
69
70
71
72
# File 'lib/new_store_api/models/fixed_discount.rb', line 66

def self.optionals
  %w[
    coupon_code
    description
    external_reference
  ]
end

.validate(value) ⇒ Object

Validates an instance of the object from a given value.

Parameters:

  • The (FixedDiscount | Hash)

    value against the validation is performed.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/new_store_api/models/fixed_discount.rb', line 124

def self.validate(value)
  if value.instance_of? self
    return (
      APIHelper.valid_type?(value.applied_amount,
                            ->(val) { val.instance_of? Integer }) and
        APIHelper.valid_type?(value.id,
                              ->(val) { val.instance_of? String }) and
        APIHelper.valid_type?(value.original_amount,
                              ->(val) { val.instance_of? Integer }) and
        APIHelper.valid_type?(value.scope,
                              ->(val) { LevelEnum.validate(val) }) and
        APIHelper.valid_type?(value.type,
                              ->(val) { val.instance_of? String })
    )
  end

  return false unless value.instance_of? Hash

  (
    APIHelper.valid_type?(value['applied_amount'],
                          ->(val) { val.instance_of? Integer }) and
      APIHelper.valid_type?(value['id'],
                            ->(val) { val.instance_of? String }) and
      APIHelper.valid_type?(value['original_amount'],
                            ->(val) { val.instance_of? Integer }) and
      APIHelper.valid_type?(value['scope'],
                            ->(val) { LevelEnum.validate(val) }) and
      APIHelper.valid_type?(value['type'],
                            ->(val) { val.instance_of? String })
  )
end

Instance Method Details

#inspectObject

Provides a debugging-friendly string with detailed object information.



165
166
167
168
169
170
171
# File 'lib/new_store_api/models/fixed_discount.rb', line 165

def inspect
  class_name = self.class.name.split('::').last
  "<#{class_name} applied_amount: #{@applied_amount.inspect}, coupon_code:"\
  " #{@coupon_code.inspect}, description: #{@description.inspect}, external_reference:"\
  " #{@external_reference.inspect}, id: #{@id.inspect}, original_amount:"\
  " #{@original_amount.inspect}, scope: #{@scope.inspect}, type: #{@type.inspect}>"
end

#to_sObject

Provides a human-readable string representation of the object.



157
158
159
160
161
162
# File 'lib/new_store_api/models/fixed_discount.rb', line 157

def to_s
  class_name = self.class.name.split('::').last
  "<#{class_name} applied_amount: #{@applied_amount}, coupon_code: #{@coupon_code},"\
  " description: #{@description}, external_reference: #{@external_reference}, id: #{@id},"\
  " original_amount: #{@original_amount}, scope: #{@scope}, type: #{@type}>"
end