Module: MongoHelper::ClassMethods
- Defined in:
- lib/mongo_helper.rb
Instance Method Summary collapse
- #attr_alias(new_attr, old_attr) ⇒ Object
- #embedded_in(owner_name) ⇒ Object
- #enum_methods!(enum, opts) ⇒ Object
- #mongo_new ⇒ Object
- #new_embedded ⇒ Object
- #random_token ⇒ Object
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 (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_new ⇒ Object
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_embedded ⇒ Object
35 36 37 38 39 |
# File 'lib/mongo_helper.rb', line 35 def a = self.new a.id = BSON::ObjectId.new a end |
#random_token ⇒ Object
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 |