Module: MongoHelper::ClassMethods

Defined in:
lib/mongo_helper.rb

Instance Method Summary collapse

Instance Method Details

#attr_alias(new_attr, old_attr) ⇒ Object



5
6
7
8
# File 'lib/mongo_helper.rb', line 5

def attr_alias(new_attr, old_attr)
  alias_method(new_attr, old_attr)
  alias_method("#{new_attr}=", "#{old_attr}=")
end

#embedded_in(owner_name) ⇒ Object



10
11
12
13
# File 'lib/mongo_helper.rb', line 10

def embedded_in(owner_name)
  alias_method(owner_name, :_parent_document)
  alias_method("#{owner_name}=", "_parent_document=")
end

#enum_methods!(enum, opts) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mongo_helper.rb', line 20

def enum_methods!(enum, opts)
	enum = enum.to_s
	define_method "#{enum}?" do |opt|
		val = send(enum)
		if opt.class == Array
			return opt.collect{|a| opts[a]}.include? val
		else
			return val == opts[opt]
		end
	end
	define_method "#{enum}!" do |opt|
		send("#{enum}=", opts[opt])
	end
end

#mongo_newObject



41
42
43
44
45
46
47
48
# File 'lib/mongo_helper.rb', line 41

def mongo_new
	a = self.new
	a.id = BSON::ObjectId.new
	a.created_at = Time.new if a.respond_to? :created_at
	a.updated_at = Time.new if a.respond_to? :updated_at
	a.is_new = true if a.respond_to? :is_new
	return a
end

#new_embeddedObject



35
36
37
38
39
# File 'lib/mongo_helper.rb', line 35

def new_embedded
	a = self.new
	a.id = BSON::ObjectId.new
	a
end

#random_tokenObject



15
16
17
18
# File 'lib/mongo_helper.rb', line 15

def random_token
  # use base64url as defined by RFC4648
  SecureRandom.base64(15).tr('+/=', '').strip.delete("\n")
end