Class: Prestashop::Mapper::ProductFeature

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

Returns a new instance of ProductFeature.


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

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

  @id_lang  = args.fetch(:id_lang)
end

Instance Attribute Details

#idObject

Returns the value of attribute id


8
9
10
# File 'lib/prestashop/mapper/models/product_feature.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_feature.rb', line 10

def id_lang
  @id_lang
end

#nameObject


20
21
22
# File 'lib/prestashop/mapper/models/product_feature.rb', line 20

def name
  @name.plain
end

#positionObject

Returns the value of attribute position


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

def position
  @position
end

Class Method Details

.cacheObject


48
49
50
# File 'lib/prestashop/mapper/models/product_feature.rb', line 48

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

.create_from_hash(resources, id_lang) ⇒ Object


52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/prestashop/mapper/models/product_feature.rb', line 52

def create_from_hash resources, id_lang
  resources = [resources] if resources.kind_of?(Hash)
  if resources.kind_of?(Array)
    features = []
    resources.each do |resource|
      if resource[:feature] and !resource[:feature].empty? and resource[:value] and !resource[:value].empty?
        id_f = new(name: resource[:feature], id_lang: id_lang).find_or_create
        id_fv = ProductFeatureValue.new(value: resource[:value], id_feature: id_f, id_lang: id_lang).find_or_create
        features << { id_feature: id_f, id_feature_value: id_fv }
      end
    end 
    features
  end
end

.find_in_cache(name, id_lang) ⇒ Object


44
45
46
# File 'lib/prestashop/mapper/models/product_feature.rb', line 44

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

Instance Method Details

#find_or_createObject


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

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

#hashObject


24
25
26
27
# File 'lib/prestashop/mapper/models/product_feature.rb', line 24

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

#validate!Object

Raises:

  • (ArgumentError)

38
39
40
41
# File 'lib/prestashop/mapper/models/product_feature.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)
end