Module: ONIX::DiscountCodeds

Included in:
Price
Defined in:
lib/onix/discount_codeds.rb

Constant Summary collapse

CODE_TYPES =
{:bic => 1, :proprietary => 2, :boeksoort => 3, :german => 4}
DEFAULT_ATTRIBUTE =
:discount_code_type

Instance Method Summary collapse

Instance Method Details

#discount_codedsObject



12
13
14
# File 'lib/onix/discount_codeds.rb', line 12

def discount_codeds
  @discount_codeds ||= []
end

#discount_codeds=(values) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/onix/discount_codeds.rb', line 41

def discount_codeds=(values)
  values = [values] if values.nil? || values.is_a?(ONIX::DiscountCoded)
  if values.is_a?(Array)
    # Empty array
    @discount_codeds = []
    # Add any valid value
    values.each do |value|
      @discount_codeds << value if value.is_a?(ONIX::DiscountCoded)
    end
  else
    raise ArgumentError, "Invalid DiscountCoded value: #{values.inspect}"
  end
end

#find_discount_coded(options = {}) ⇒ Object

find the DiscountCoded matching a particular type

Raises:

  • (NoMethodError)


17
18
19
20
21
22
23
24
25
# File 'lib/onix/discount_codeds.rb', line 17

def find_discount_coded(options = {})
  raise NoMethodError, "Must call initialize_discount_codeds first" unless defined?(@discount_codeds)
  
  # test find parameters
  attribute, value = set_type_options(options).first
  raise ArgumentError, "Find method passed unknown attribute '#{attribute}'" unless ONIX::DiscountCoded.new.attributes.include?(attribute.to_s)
  
  @discount_codeds.find { |obj| obj.send(attribute) == value }
end

#initialize_discount_codeds(options = {}) ⇒ Object



8
9
10
# File 'lib/onix/discount_codeds.rb', line 8

def initialize_discount_codeds(options = {})
  self.discount_codeds = options[:discount_codeds]
end

#set_discount_coded(type, value) ⇒ Object

set the value of a particular DiscountCoded (found by type) type can be either type code or key from CODE_TYPES



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/onix/discount_codeds.rb', line 29

def set_discount_coded(type, value)
  obj = find_discount_coded(type)

  # create a new discount_coded object if necessary
  if obj.nil?
    obj = ONIX::DiscountCoded.new(set_type_options(type))
    @discount_codeds << obj
  end

  obj.discount_code = value
end