class Prestashop::Mapper::ProductFeature

Attributes

id[RW]
id_lang[RW]
name[W]
position[RW]

Public Class Methods

cache() click to toggle source
# File lib/prestashop/mapper/models/product_feature.rb, line 48
def cache
  all display: '[id,name]'
end
create_from_hash(resources, id_lang) click to toggle source
# 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) click to toggle source
# 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
new(args = {}) click to toggle source
# 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

Public Instance Methods

find_or_create() click to toggle source
# 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
hash() click to toggle source
# File lib/prestashop/mapper/models/product_feature.rb, line 24
def hash
  validate!
  { name: hash_lang(name, id_lang) }
end
name() click to toggle source
# File lib/prestashop/mapper/models/product_feature.rb, line 20
def name
  @name.plain
end
validate!() click to toggle source
# 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