Class: Cornerstore::Product

Inherits:
Model::Base show all
Defined in:
lib/cornerstore/api/product.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) ⇒ Product

Returns a new instance of Product.



10
11
12
13
14
15
# File 'lib/cornerstore/api/product.rb', line 10

def initialize(attributes = {}, parent = nil)
  self.images     = Cornerstore::Image::Resource.new(self, attributes.delete('images') || [], 'images')
  self.variants   = Cornerstore::Variant::Resource.new(self, attributes.delete('variants') || [], 'variants')
  self.properties = Cornerstore::Property::Resource.new(self, attributes.delete('properties') || [], '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

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#enabledObject

Returns the value of attribute enabled.



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

def enabled
  @enabled
end

#imagesObject

Returns the value of attribute images.



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

def images
  @images
end

#manufacturerObject

Returns the value of attribute manufacturer.



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

def manufacturer
  @manufacturer
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#propertiesObject

Returns the value of attribute properties.



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

def properties
  @properties
end

#variantsObject

Returns the value of attribute variants.



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

def variants
  @variants
end

Instance Method Details

#attributesObject



17
18
19
20
21
22
23
24
# File 'lib/cornerstore/api/product.rb', line 17

def attributes
  {
    name: name,
    description: description,
    manufacturer: manufacturer,
    enabled: enabled
  }
end

#cover_imageObject



46
47
48
# File 'lib/cornerstore/api/product.rb', line 46

def cover_image
  @cover_image ||= self.images.to_a.find {|i| i.cover == true }
end

#new?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/cornerstore/api/product.rb', line 42

def new?
  self.created_at >= 2.weeks.ago
end

#offer?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/cornerstore/api/product.rb', line 38

def offer?
  variants.any? { |v| v.offer? }
end

#order_numberObject



30
31
32
# File 'lib/cornerstore/api/product.rb', line 30

def order_number
  variants.many? ? :many : variants.first.order_number
end

#priceObject



26
27
28
# File 'lib/cornerstore/api/product.rb', line 26

def price
  variants.collect { |v| v.price }.sort.first
end

#sold_out?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/cornerstore/api/product.rb', line 34

def sold_out?
  variants.all? { |v| v.sold_out? }
end