Class: Prestashop::Mapper::ProductOption

Inherits:
Model
  • Object
show all
Defined in:
lib/prestashop/mapper/models/product_option.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 = {}) ⇒ ProductOption

Returns a new instance of ProductOption


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

def initialize args = {}
  @id             = args[:id]
  @is_color_group = args.fetch(:is_color_group, 0)
  @group_type     = args.fetch(:group_type, 'select')
  @position       = args[:position]
  @name           = args.fetch(:name)
  @public_name    = args[:public_name]

  @id_lang        = args.fetch(:id_lang)
end

Instance Attribute Details

#group_typeObject

Returns the value of attribute group_type


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

def group_type
  @group_type
end

#idObject

Returns the value of attribute id


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

def id
  @id
end

#id_langObject

Returns the value of attribute id_lang


10
11
12
# File 'lib/prestashop/mapper/models/product_option.rb', line 10

def id_lang
  @id_lang
end

#is_color_groupObject

Returns the value of attribute is_color_group


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

def is_color_group
  @is_color_group
end

#nameObject

Returns the value of attribute name


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

def name
  @name
end

#positionObject

Returns the value of attribute position


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

def position
  @position
end

#public_nameObject


23
24
25
# File 'lib/prestashop/mapper/models/product_option.rb', line 23

def public_name
  @public_name ? @public_name : name
end

Class Method Details

.cacheObject


66
67
68
# File 'lib/prestashop/mapper/models/product_option.rb', line 66

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

.create_from_hash(product_options, id_lang) ⇒ Object


53
54
55
56
57
58
59
60
# File 'lib/prestashop/mapper/models/product_option.rb', line 53

def create_from_hash product_options, id_lang
  result = []
  product_options.each do |product_option|
    id_o = ProductOption.new(name: product_option[:name], id_lang: id_lang).find_or_create
    result << ProductOptionValue.new(name: product_option[:value], id_attribute_group: id_o, id_lang: id_lang).find_or_create
  end if product_options
  result
end

.find_in_cache(name, id_lang) ⇒ Object


62
63
64
# File 'lib/prestashop/mapper/models/product_option.rb', line 62

def find_in_cache name, id_lang
  Client.options_cache.find{|k| k[:name].find_lang(name, id_lang) } if Client.options_cache
end

Instance Method Details

#find_or_createObject


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

def find_or_create
  option = self.class.find_in_cache name, id_lang
  unless option
    option = create
    Client.clear_options_cache
  end
  option[:id]
end

#hashObject


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

def hash
  validate!

  { is_color_group: is_color_group,
    position: position,
    group_type: group_type,
    name: hash_lang(name, id_lang),
    public_name: hash_lang(public_name, id_lang) }
end

#validate!Object

Raises:

  • (ArgumentError)

46
47
48
49
50
# File 'lib/prestashop/mapper/models/product_option.rb', line 46

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, 'group_type must string' unless group_type.kind_of?(String)
end