Class: Showroom::ProductVariant

Inherits:
Resource
  • Object
show all
Defined in:
lib/showroom/models/product_variant.rb

Overview

Represents a Shopify product variant.

Examples:

variant = ProductVariant.new('title' => 'S / Red', 'price' => '899.00', 'available' => true)
variant.available? # => true
variant.on_sale?   # => false

Instance Attribute Summary

Attributes inherited from Resource

#attrs, #client

Instance Method Summary collapse

Methods inherited from Resource

#==, #[], has_many, has_one, #initialize, #inspect, main_attr_keys, main_attrs, #method_missing, #respond_to_missing?, #to_h

Constructor Details

This class inherits a constructor from Showroom::Resource

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Showroom::Resource

Instance Method Details

#available?Boolean

Returns true when the variant is available for purchase.

Returns:

  • (Boolean)


16
17
18
# File 'lib/showroom/models/product_variant.rb', line 16

def available?
  @attrs['available'] == true
end

#on_sale?Boolean

Returns true when compare_at_price is present and greater than price.

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/showroom/models/product_variant.rb', line 23

def on_sale?
  cap = @attrs['compare_at_price']
  return false if cap.nil? || cap.to_s.empty?

  cap.to_f > @attrs['price'].to_f
end

#optionsArray<String>

Returns the selected options for this variant as an Array of values.

Collects option1, option2, option3 in order, omitting nil values.

Returns:

  • (Array<String>)


35
36
37
# File 'lib/showroom/models/product_variant.rb', line 35

def options
  [@attrs['option1'], @attrs['option2'], @attrs['option3']].compact
end