Class: FastshopCatalog::BaseEntity

Inherits:
Object
  • Object
show all
Defined in:
lib/fastshop_catalog/base_entity.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.translate(map) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/fastshop_catalog/base_entity.rb', line 4

def self.translate(map)
  #instantiates the translation map and creates accessors
  @translate_map = map
  self.instance_eval do
    map.each_key{|k| attr_accessor k}
  end
end

.translate_symbol(symbol) ⇒ Object



12
13
14
# File 'lib/fastshop_catalog/base_entity.rb', line 12

def self.translate_symbol(symbol)
  @translate_map[symbol]
end

Instance Method Details

#to_json(*a) ⇒ Object



31
32
33
# File 'lib/fastshop_catalog/base_entity.rb', line 31

def to_json(*a)
  to_map.to_json(*a)
end

#to_mapObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fastshop_catalog/base_entity.rb', line 16

def to_map
  map = {}
  #translates the variables to it's synonyms
  self.class.public_instance_methods(optional=false).grep(/.*[^\=]$/).each do |m|
    map[self.class.translate_symbol(m.to_sym)] = self.send(m) if self.send(m)
  end
  #orders the map by the synonym
  map.keys.sort.inject({}) do |o, k|
    content = map[k]
    content = content.to_map if content.respond_to? :to_map
    o[k] = content
    o
  end
end