Class: BusinessCatalyst::CSV::ProductAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/business_catalyst/csv/product_attribute.rb

Overview

Usage:

# Dropdowns with custom image and price:
size =  BusinessCatalyst::CSV::ProductAttribute.new("Size",
          :display_as => :radio,
          :required => true,
          :keep_stock => false
        )
size.add_option("Small", "live/url/for/small.jpg", "US/20.00")
size.add_option("Large", "live/url/for/large.jpg", "US/25.00")

# Shorthand for multiple radio buttons with no image or price:
color = BusinessCatalyst::CSV::ProductAttribute.new("Size",
          :display_as => :radio,
          :required => true,
          :keep_stock => false
        )
color.add_options("Red", "Green", "Blue")

# Checkboxes are supported as well:
addon = BusinessCatalyst::CSV::ProductAttribute.new("Addon",
          :display_as => :checkbox,
          :required => false,
          :keep_stock => false
        )
addon.add_option("Cool thing 1", nil, "US/5.00")

Defined Under Namespace

Classes: Option

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ ProductAttribute

Available options:

  • :display_as => :dropdown / :checkbox / :radio

  • :required => true / false

  • :keep_stock => true / false



42
43
44
45
46
47
48
49
50
# File 'lib/business_catalyst/csv/product_attribute.rb', line 42

def initialize(name, options = {})
  @name = name
  self.display_as = options.delete(:display_as) { nil }
  @required = options.delete(:required) { false }
  @keep_stock = options.delete(:keep_stock) { false }
  unless options.empty?
    raise ArgumentError, "unrecognized arguments: #{options.keys.inspect}"
  end
end

Instance Attribute Details

#display_asObject

Returns the value of attribute display_as.



35
36
37
# File 'lib/business_catalyst/csv/product_attribute.rb', line 35

def display_as
  @display_as
end

#keep_stockObject

Returns the value of attribute keep_stock.



35
36
37
# File 'lib/business_catalyst/csv/product_attribute.rb', line 35

def keep_stock
  @keep_stock
end

#nameObject

Returns the value of attribute name.



35
36
37
# File 'lib/business_catalyst/csv/product_attribute.rb', line 35

def name
  @name
end

#requiredObject

Returns the value of attribute required.



35
36
37
# File 'lib/business_catalyst/csv/product_attribute.rb', line 35

def required
  @required
end

Class Method Details

.normalize_display_as(display_as) ⇒ Object

Convert display_as to integer for BC, raises ArgumentError if input cannot be mapped to a valid integer. The following symbols are accepted:

  • :dropdown = 5

  • :checkbox = 6

  • :radio = 7



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/business_catalyst/csv/product_attribute.rb', line 138

def self.normalize_display_as(display_as)
  if display_as.kind_of?(Symbol)
    case display_as
    when :dropdown
      5
    when :checkbox
      6
    when :radio
      7
    else
      raise ArgumentError, "display_as must be :dropdown, :checkbox, or :radio"
    end
  elsif ![5, 6, 7].include?(display_as)
    raise ArgumentError, "display_as must be 5, 6, or 7"
  else
    display_as
  end
end

Instance Method Details

#add_option(*args) ⇒ Object

Usage:

# Simplest:
add_option(name, image, price)

# Manually create ProductAttribute::Option instance:
add_option(ProductAttribute::Option.new(name, image, price))


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/business_catalyst/csv/product_attribute.rb', line 68

def add_option(*args)
  option = nil
  if args.length == 1
    arg = args.first
    if arg.kind_of?(Option)
      option = arg
    elsif arg.kind_of?(Hash)
      option = Option.new(arg.fetch(:name), arg.fetch(:image, nil), arg.fetch(:price, nil))
    else
      raise ArgumentError, "unrecognized argument: #{arg.inspect}"
    end
  else
    name, image, price = *args
    option = Option.new(name, image, price)
  end
  options << option
  option
end

#add_options(*args) ⇒ Object

Usage:

# List of names:
add_options("Name 1", "Name 2", ...)

# Arrays:
add_options([name, image, price], [name, image, price], ...)

# Hashes:
add_options({name: name, image: image, price: price}, ...)

# ProductAttribute::Option instances:
option_1 = ProductAttribute::Option.new(name, image, price)
add_options(option_1, ...)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/business_catalyst/csv/product_attribute.rb', line 102

def add_options(*args)
  if args.length > 1
    options = args.map {|arg| Array(arg)}
  elsif args.first.respond_to?(:each)
    options = args.first
  else
    raise ArgumentError, "options must be an Array of ProductAttribute::Option"
  end

  options.each do |option|
    if option.kind_of?(Hash)
      name = option.fetch(:name)
      image = option.fetch(:image, nil)
      price = option.fetch(:price, nil)
      add_option(name, image, price)

    elsif option.kind_of?(Array)
      name, image, price = *option
      add_option(name, image, price)

    elsif option.kind_of?(Option)
      add_option(option)

    else
      raise ArgumentError, "#{option} is not a valid ProductAttribute::Option"
    end
  end
end

#optionsObject



56
57
58
# File 'lib/business_catalyst/csv/product_attribute.rb', line 56

def options
  @options ||= []
end