Sha256: aed68ac67c6b2ff67f53dac93cca2e8ec2818f5b84a63fc0683e88c957a2e659

Contents?: true

Size: 864 Bytes

Versions: 3

Compression:

Stored size: 864 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({}) do |o, k|
        content = map[k]
        content = content.to_map if content.respond_to? :to_map
        o[k] = content
        o
      end
    end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fastshop_catalog-0.0.8 lib/fastshop_catalog/base_entity.rb
fastshop_catalog-0.0.7 lib/fastshop_catalog/base_entity.rb
fastshop_catalog-0.0.6 lib/fastshop_catalog/base_entity.rb