Class: Prestashop::Mapper::ProductOptionValue

Inherits:
Model
  • Object
show all
Defined in:
lib/prestashop/mapper/models/product_option_value.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#hash_lang, #meta_description, #meta_keywords, #meta_title, model, resource

Methods included from Extension

included

Constructor Details

#initialize(args = {}) ⇒ ProductOptionValue

Returns a new instance of ProductOptionValue.



11
12
13
14
15
16
17
18
19
# File 'lib/prestashop/mapper/models/product_option_value.rb', line 11

def initialize args = {}
  @id                 = args[:id]
  @id_attribute_group = args.fetch(:id_attribute_group)
  @color              = args.fetch(:color, 0)
  @position           = args[:position]
  @name               = args.fetch(:name)

  @id_lang            = args.fetch(:id_lang)
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



8
9
10
# File 'lib/prestashop/mapper/models/product_option_value.rb', line 8

def color
  @color
end

#idObject

Returns the value of attribute id.



8
9
10
# File 'lib/prestashop/mapper/models/product_option_value.rb', line 8

def id
  @id
end

#id_attribute_groupObject

Returns the value of attribute id_attribute_group.



8
9
10
# File 'lib/prestashop/mapper/models/product_option_value.rb', line 8

def id_attribute_group
  @id_attribute_group
end

#id_langObject

Returns the value of attribute id_lang.



9
10
11
# File 'lib/prestashop/mapper/models/product_option_value.rb', line 9

def id_lang
  @id_lang
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/prestashop/mapper/models/product_option_value.rb', line 8

def name
  @name
end

#positionObject

Returns the value of attribute position.



8
9
10
# File 'lib/prestashop/mapper/models/product_option_value.rb', line 8

def position
  @position
end

Class Method Details

.cacheObject



50
51
52
# File 'lib/prestashop/mapper/models/product_option_value.rb', line 50

def cache
  all display: '[id,id_attribute_group,name]'
end

.find_in_cache(id_attribute_group, name, id_lang) ⇒ Object



46
47
48
# File 'lib/prestashop/mapper/models/product_option_value.rb', line 46

def find_in_cache id_attribute_group, name, id_lang
  Client.option_values_cache.find{|v| v[:id_attribute_group] == id_attribute_group and v[:name].find_lang(name, id_lang)} if Client.option_values_cache
end

Instance Method Details

#find_or_createObject



28
29
30
31
32
33
34
35
# File 'lib/prestashop/mapper/models/product_option_value.rb', line 28

def find_or_create
  result = self.class.find_in_cache id_attribute_group, name, id_lang
  unless result
    result = create
    Client.clear_option_values_cache
  end
  result[:id]
end

#hashObject



21
22
23
24
25
26
# File 'lib/prestashop/mapper/models/product_option_value.rb', line 21

def hash
  validate!
  { name:               hash_lang(name, id_lang),
    id_attribute_group: id_attribute_group,
    color:              color }
end

#validate!Object

Supplier must have 1/0 as active and name must be string

Raises:

  • (ArgumentError)


38
39
40
41
42
43
# File 'lib/prestashop/mapper/models/product_option_value.rb', line 38

def validate!
  raise ArgumentError, 'id lang must be number' unless id_lang.kind_of?(Integer)
  raise ArgumentError, 'name must string' unless name.kind_of?(String)
  raise ArgumentError, 'id attribute group must be number' unless id_attribute_group.kind_of?(Integer)
  raise ArgumentError, 'color must be true or false' unless color == 1 or color == 0
end