Module: Mongous::Base
- Included in:
- Mongous
- Defined in:
- lib/mongous/base.rb
Instance Method Summary collapse
- #client ⇒ Object
- #client=(new_client) ⇒ Object
- #connect(hosts_or_uri = nil, file: nil, mode: nil, loglevel: nil, **options) ⇒ Object
- #connect!(hosts_or_uri = nil, **options) ⇒ Object
- #document!(*names, **opts) ⇒ Object (also: #attach!)
- #loger ⇒ Object
Instance Method Details
#client ⇒ Object
94 95 96 97 98 99 |
# File 'lib/mongous/base.rb', line 94 def client if not self.class_variable_defined?( :@@client ) connect! end self.class_variable_get( :@@client ) end |
#client=(new_client) ⇒ Object
101 102 103 104 105 106 |
# File 'lib/mongous/base.rb', line 101 def client=( new_client ) if not new_client.is_a?( ::Mongo::Client ) raise Mongous::Error, "type invalid. : #{ new_client }" end self.class_variable_set( :@@client, new_client ) end |
#connect(hosts_or_uri = nil, file: nil, mode: nil, loglevel: nil, **options) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mongous/base.rb', line 11 def connect( hosts_or_uri = nil, file: nil, mode: nil, loglevel: nil, ** ) case hosts_or_uri when Array, String hosts_or_uri when NilClass if file mode ||= "development" text = ::File.open( file ).read rescue raise( Mongous::Error, "could not load #{ file }" ) yaml = ::ERB.new( text ).result rescue raise( Mongous::Error, "could not parse by ERB: #{ file }" ) hash = ::YAML.load( yaml ) conf = hash[mode]["clients"]["default"] hosts_or_uri = conf["uri"] if hosts_or_uri conf.delete( "uri" ) .merge!( conf ) else hosts_or_uri = conf["hosts"] conf.delete( "hosts" ) database = conf["database"] conf.delete( "database" ) .merge!( conf ) ["database"] = database end else hosts_or_uri = [ "localhost:27017" ] [:database] = "test" if [:database].nil? || [:database].empty? end end ::Mongo::Logger.logger.level = loglevel || Logger::ERROR ::Mongo::Client.new( hosts_or_uri, ) end |
#connect!(hosts_or_uri = nil, **options) ⇒ Object
48 49 50 51 |
# File 'lib/mongous/base.rb', line 48 def connect!( hosts_or_uri = nil, ** ) client_ = connect( hosts_or_uri, ** ) self.class_variable_set( :@@client, client_ ) end |
#document!(*names, **opts) ⇒ Object Also known as: attach!
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/mongous/base.rb', line 53 def document!( *names, **opts ) raise Mongous::Error, "missing argument." if names.empty? names.each do |name| case name when String name when Symbol name = name.to_s else raise Mongous::Error, "type invalid. : #{ name }" end raise Mongous::Error, "missing argument." unless /^[A-Z]/.match(name) if Object.const_defined?( name ) if Object.const_get( name ).include?( Mongous::Document ) Object.class_eval{ remove_const( name ) } else raise Mongous::Error, "type invalid. : #{ Object.class_eval(name) }" end end klass = Object.class_eval <<-CLASS class #{name} include Mongous::Document end CLASS if opts[:timestamp] klass.class_eval do field :created_at, Time, create: proc{ Time.now } field :updated_at, Time, update: proc{ Time.now } index :created_at index :updated_at end end end end |
#loger ⇒ Object
108 109 110 |
# File 'lib/mongous/base.rb', line 108 def loger ::Mongo::Logger.logger end |