Sha256: 2b4c4d17290b085ffe4017616291fbee22da7c9d618eb599c1e7b8ff7ac0b98c
Contents?: true
Size: 1.46 KB
Versions: 1
Compression:
Stored size: 1.46 KB
Contents
module Toy module Querying extend ActiveSupport::Concern module ClassMethods def read(id, options = nil) if (attrs = adapter.read(id, options)) load(id, attrs) end end alias_method :get, :read alias_method :find, :read def read!(id, options = nil) get(id, options) || raise(Toy::NotFound.new(id)) end alias_method :get!, :read! alias_method :find!, :read! def read_multiple(ids, options = nil) result = adapter.read_multiple(ids, options) result.each do |id, attrs| result[id] = attrs.nil? ? nil : load(id, attrs) end result end alias_method :get_multiple, :read_multiple alias_method :find_multiple, :read_multiple def get_or_new(id) get(id) || new(:id => id) end def get_or_create(id) get(id) || create(:id => id) end def key?(id, options = nil) adapter.key?(id, options) end alias :has_key? :key? def load(id, attrs) attrs ||= {} instance = constant_from_attrs(attrs).allocate instance.initialize_from_database(attrs.update('id' => id)) end def constant_from_attrs(attrs) return self if attrs.nil? type = attrs[:type] || attrs['type'] return self if type.nil? type.constantize rescue NameError self end private :constant_from_attrs end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
toystore-0.13.1 | lib/toy/querying.rb |