Sha256: c73803897db233e074e9471e47ceffd0f186c3e90c9f1c6b8d4a4d2a38d72920

Contents?: true

Size: 982 Bytes

Versions: 3

Compression:

Stored size: 982 Bytes

Contents

require 'hashie'

module LayerVault
  class Model < Hashie::Mash
    class << self
      def build_associations(hash, *associations)
        mapping = {}

        associations.each do |association|
          mapping[association] = hash.fetch(association.to_s, {})
          hash.delete(association) if mapping[association]
        end

        instance = new(hash)

        associations.each do |association|
          klass = association_to_class(association)
          objs = mapping[association].map { |p| klass.new(p) }
          instance[association.to_s] = objs
        end

        instance
      end

      private

      def association_to_class(association)
        class_name = association.to_s.chomp('s').capitalize
        LayerVault.const_get(class_name)
      end
    end

    def set_context(context_arguments={})
      self.context = {}
      context_arguments.each do |argument, value|
        self.context[argument] = value
      end
      self
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
layervault-0.1.2 lib/layervault/model.rb
layervault-0.1.1 lib/layervault/model.rb
layervault-0.1.0 lib/layervault/model.rb