Class: Cornerstore::Variant

Inherits:
Model::Base show all
Defined in:
lib/cornerstore/api/variant.rb

Defined Under Namespace

Classes: Resource

Instance Attribute Summary collapse

Attributes inherited from Model::Base

#_id, #_slugs, #created_at, #parent, #updated_at

Instance Method Summary collapse

Methods inherited from Model::Base

#==, #attributes=, #inspect, method_missing, #method_missing, #to_param, #url

Constructor Details

#initialize(attributes = {}, parent = nil) ⇒ Variant

Returns a new instance of Variant.



29
30
31
32
33
# File 'lib/cornerstore/api/variant.rb', line 29

def initialize(attributes = {}, parent = nil)
  self.price = Cornerstore::Price.new(attributes.delete('price'))
  self.properties = Cornerstore::Property::Resource.new(self, attributes.delete('properties') || [])
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Cornerstore::Model::Base

Instance Attribute Details

#differentiating_propertiesObject

Returns the value of attribute differentiating_properties.



2
3
4
# File 'lib/cornerstore/api/variant.rb', line 2

def differentiating_properties
  @differentiating_properties
end

#offerObject Also known as: offer?

Returns the value of attribute offer.



2
3
4
# File 'lib/cornerstore/api/variant.rb', line 2

def offer
  @offer
end

#order_numberObject

Returns the value of attribute order_number.



2
3
4
# File 'lib/cornerstore/api/variant.rb', line 2

def order_number
  @order_number
end

#oversellObject

Returns the value of attribute oversell.



2
3
4
# File 'lib/cornerstore/api/variant.rb', line 2

def oversell
  @oversell
end

#priceObject

Returns the value of attribute price.



2
3
4
# File 'lib/cornerstore/api/variant.rb', line 2

def price
  @price
end

#propertiesObject

Returns the value of attribute properties.



2
3
4
# File 'lib/cornerstore/api/variant.rb', line 2

def properties
  @properties
end

#qty_in_stockObject

Returns the value of attribute qty_in_stock.



2
3
4
# File 'lib/cornerstore/api/variant.rb', line 2

def qty_in_stock
  @qty_in_stock
end

#qty_reservedObject

Returns the value of attribute qty_reserved.



2
3
4
# File 'lib/cornerstore/api/variant.rb', line 2

def qty_reserved
  @qty_reserved
end

#unitObject

Returns the value of attribute unit.



2
3
4
# File 'lib/cornerstore/api/variant.rb', line 2

def unit
  @unit
end

#weightObject

Returns the value of attribute weight.



2
3
4
# File 'lib/cornerstore/api/variant.rb', line 2

def weight
  @weight
end

Instance Method Details

#attributesObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cornerstore/api/variant.rb', line 16

def attributes
  {
    order_number: order_number,
    price: price,
    qty_in_stock: qty_in_stock,
    qty_reserved: qty_reserved,
    offer: offer,
    oversell: oversell,
    weight: weight,
    unit: unit
  }
end

#qty_availableObject



35
36
37
38
39
40
41
# File 'lib/cornerstore/api/variant.rb', line 35

def qty_available
  if self.qty_in_stock and not self.oversell
    self.qty_in_stock - self.qty_reserved
  else
    Float::INFINITY
  end
end

#sold_out?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/cornerstore/api/variant.rb', line 43

def sold_out?
  self.qty_available == 0
end