Sha256: 49d57b02525c887fa325d02132804b4511f53cfb21b698a6d8e1104bf75d612d

Contents?: true

Size: 761 Bytes

Versions: 5

Compression:

Stored size: 761 Bytes

Contents

module FastshopCatalog
  class BaseEntity
    
    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
    
    def self.translate_symbol(symbol)
      @translate_map[symbol]
    end
    
    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({}){|o, k| o[k] = map[k]; o}
    end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
fastshop_catalog-0.0.5 lib/fastshop_catalog/base_entity.rb
fastshop_catalog-0.0.4 lib/fastshop_catalog/base_entity.rb
fastshop_catalog-0.0.3 lib/fastshop_catalog/base_entity.rb
fastshop_catalog-0.0.2 lib/fastshop_catalog/base_entity.rb
fastshop_catalog-0.0.1 lib/fastshop_catalog/base_entity.rb