class Mongocore::Schema
Attributes
accessors[RW]
Accessors
defaults[RW]
Accessors
keys[RW]
Accessors
klass[RW]
Accessors
many[RW]
Accessors
meta[RW]
Accessors
path[RW]
Accessors
schema[RW]
Accessors
scopes[RW]
Accessors
Public Class Methods
new(klass)
click to toggle source
Init
# File lib/mongocore/schema.rb, line 12 def initialize(klass) # Store the document @klass = klass # Schema path @path = File.join(Mongocore.schema, "#{@klass.to_s.downcase}.yml") # Load schema @schema = YAML.load(File.read(@path)).deep_symbolize_keys # Meta @meta = @schema[:meta] || {} # Keys @keys = @schema[:keys] || {} # Accessors (@accessors = @schema[:accessor] || []).each{|a| @klass.send(:attr_accessor, a)} # Many (@many = @schema[:many] || []).each{|k| many(k)} # Scopes (@scopes = @schema[:scopes] || {}).each{|k, v| scope(k, v)} # Defaults and foreign keys @defaults = {}; @keys.each{|k, v| foreign(k, v); @defaults[k] = v[:default]} end
Public Instance Methods
attributes(tags)
click to toggle source
Get attributes that has these tags
# File lib/mongocore/schema.rb, line 42 def attributes(tags) (tags[0] ? @keys.select{|k, v| v[:tags] & tags} : @keys).keys end
bin(val)
click to toggle source
Convert to binary
# File lib/mongocore/schema.rb, line 57 def bin(val) val.is_a?(BSON::Binary) ? val : BSON::Binary.new(val) end
foreign(key, data)
click to toggle source
Foreign keys
# File lib/mongocore/schema.rb, line 116 def foreign(key, data) return if key !~ /(.+)_id/ t = %Q{ def #{$1} @#{$1} ||= mq(#{$1.capitalize}, :_id => @#{key}).first end def #{$1}=(m) @#{key} = m._id rescue (BSON::ObjectId.from_string(m) rescue m) @#{$1} = m end } @klass.class_eval t end
ids(h)
click to toggle source
Setup query, replace :id with :_id, set up object ids
# File lib/mongocore/schema.rb, line 78 def ids(h) transform(h).each do |k, v| case v when Hash # Call hashes recursively ids(v) when Array # Return mapped array or recurse hashes v.map!{|r| r.is_a?(Hash) ? ids(r) : oid(r)} else # Convert to object ID if applicable h[k] = oid(v) if v.is_a?(String) end end end
oid(id = nil)
click to toggle source
Convert to BSON::ObjectId
# File lib/mongocore/schema.rb, line 105 def oid(id = nil) return id if id.is_a?(BSON::ObjectId) return BSON::ObjectId.new if !id BSON::ObjectId.from_string(id) rescue id end
scope(key, data)
click to toggle source
Set up scope and insert it
# File lib/mongocore/schema.rb, line 142 def scope(key, data) # Extract the parameters pm = data.delete(:params) || [] # Replace data if we are using parameters d = %{#{data}} pm.each do |a| d.scan(%r{(=>"(#{a})(\.[a-z0-9]+)?")}).each do |n| d.gsub!(n[0], %{=>#{n[1]}#{n[2]}}) end end # Define the scope method so we can call it j = pm.any? ? %{#{pm.join(', ')},} : '' t = %Q{ def #{key}(#{j} q = {}, o = {}) mq(self, q.merge(#{d}), {:scope => [:#{key}]}.merge(o)) end } @klass.instance_eval t end
set(key, val)
click to toggle source
Set value based on type
# File lib/mongocore/schema.rb, line 62 def set(key, val) return nil if val.nil? case type(key) when :string then val.to_s when :integer then val.to_i when :time then time(val) when :float then val.to_f when :boolean then val.to_s.to_bool when :object_id then oid(val) when :array, :hash then ids(val) when :binary then bin(val) else val end end
time(val)
click to toggle source
Get time
# File lib/mongocore/schema.rb, line 47 def time(val) case val when Time then val.utc when String then Time.parse(val).utc when Date, DateTime then val.to_time.utc else nil end end
transform(e)
click to toggle source
Transform :id to _id or id to object id
# File lib/mongocore/schema.rb, line 95 def transform(e) e.is_a?(Hash) ? e.transform_keys!{|k| k == :id ? :_id : k} : e.map!{|r| oid(r)} end
type(key)
click to toggle source
Find type as defined in schema
# File lib/mongocore/schema.rb, line 100 def type(key) @keys[key][:type].to_sym rescue :string end