Sha256: 1728f34e5c0d5b5dd6981d953dcedc0c7178d3cafbe40f46fca37ee0519bbfcf
Contents?: true
Size: 1.41 KB
Versions: 1
Compression:
Stored size: 1.41 KB
Contents
module Toy module Querying extend ActiveSupport::Concern module ClassMethods def get(id) if (attrs = adapter.read(id)) load(id, attrs) end end alias_method :read, :get alias_method :find, :get def get!(id) get(id) || raise(Toy::NotFound.new(id)) end alias_method :read!, :get! alias_method :find!, :get! def get_multiple(*ids) result = adapter.read_multiple(*ids.flatten) result.each do |id, attrs| result[id] = attrs.nil? ? nil : load(id, attrs) end result end alias_method :get_multi, :get_multiple alias_method :read_multiple, :get_multiple alias_method :find_multiple, :get_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) adapter.key?(id) 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.12.0 | lib/toy/querying.rb |