Class: Xpost::Item

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/xpost/item.rb

Constant Summary collapse

ITEM_TYPES =
Set[:product, :shipping, :tax, :fee, :insurance, :discount]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Item

Returns a new instance of Item.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/xpost/item.rb', line 15

def initialize(options = {})
  # Product Type
  @product_type = options.key?(:product_type) ? options[:product_type] : "product"
  # Product Description
  @description  = options[:description]
  # (Single) Product Amount
  @amount       = options[:amount]
  # Product Quantity
  @quantity     = options.key?(:quantity) ? options[:quantity].to_i : 1
  # Additional Product Data (For third-party purposes)
  @metadata     = options[:metadata]
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



10
11
12
# File 'lib/xpost/item.rb', line 10

def amount
  @amount
end

#descriptionObject

Returns the value of attribute description.



10
11
12
# File 'lib/xpost/item.rb', line 10

def description
  @description
end

#metadataObject

Returns the value of attribute metadata.



10
11
12
# File 'lib/xpost/item.rb', line 10

def 
  @metadata
end

#product_typeObject

Returns the value of attribute product_type.



10
11
12
# File 'lib/xpost/item.rb', line 10

def product_type
  @product_type
end

#quantityObject

Returns the value of attribute quantity.



10
11
12
# File 'lib/xpost/item.rb', line 10

def quantity
  @quantity
end

Instance Method Details

#attributesObject



34
35
36
37
38
39
40
41
42
# File 'lib/xpost/item.rb', line 34

def attributes
  {
    type: self.product_type,
    description: self.description,
    amount: self.amount,
    quantity: self.quantity,
    metadata: self.
  }
end

#check_item_typeObject



28
29
30
31
32
# File 'lib/xpost/item.rb', line 28

def check_item_type
  if self.product_type.present? && !ITEM_TYPES.include?(self.product_type.to_sym)
    errors.add(:type, "wrong item type")
  end
end