Module: Mongous::Extention

Defined in:
lib/mongous/filter.rb,
lib/mongous/extention.rb

Instance Method Summary collapse

Instance Method Details

#[](nth_or_range, len = nil) ⇒ Object



30
31
32
# File 'lib/mongous/filter.rb', line 30

def []( nth_or_range, len = nil )
  Filter.new( self )[ nth_or_range, len ]
end

#allObject



8
9
10
11
12
# File 'lib/mongous/filter.rb', line 8

def all
  self.collection.find.map do |doc|
    self.new( **doc )
  end
end

#and(*filters) ⇒ Object

Raises:



62
63
64
65
66
67
68
69
# File 'lib/mongous/filter.rb', line 62

def and( *filters )
  raise  Mongous::Error, "Unset args for #{self}.and."    if filters.empty?

  conditions  =  filters.map do |filter|
    normalize( filter, {} )
  end
  Filter.new( self ).where({"$and" => conditions})
end

#attach(collection_name) ⇒ Object



26
27
28
# File 'lib/mongous/filter.rb', line 26

def attach( collection_name )
  Filter.new( self ).attach( collection_name )
end

#blocksObject



74
75
76
# File 'lib/mongous/extention.rb', line 74

def blocks
  setup_class_variable( :@@blocks, {} )
end

#clientObject



4
5
6
7
8
9
10
11
# File 'lib/mongous/extention.rb', line 4

def client
  if self.class_variable_defined?( :@@client )
    self.class_variable_get( :@@client )
  else
    new_client  =  Mongous.client
    self.class_variable_set( :@@client, new_client )
  end
end

#client=(new_client) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/mongous/extention.rb', line 13

def client=( new_client )
  if  !new_client.is_a?( Mongo::Client )
    m  =  /(.*?):(\d+)/.match( caller()[0] )
    call_from  =  [ m[1], m[2] ].join(":")
    raise  Mongous::Error, "type invalid. :  #{ new_client } at #{ call_from }"
  end
  self.class_variable_set( :@@client, new_client )
end

#collection(temp_collection_name = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mongous/extention.rb', line 38

def collection( temp_collection_name = nil )
  if  temp_collection_name.nil?
    if self.class_variable_defined?( :@@collection )
      if ( new_collection  =  self.class_variable_get( :@@collection ) )
        return  new_collection
      end
    end
    new_collection_name  =  collection_name
  else
    new_collection_name  =  temp_collection_name
  end
  new_client  =  client

  if  new_client.database.collection_names.include?( new_collection_name )
    new_collection  =  new_client[ new_collection_name ]
  else
    new_collection  =  new_client[ new_collection_name ]
    new_collection.create
  end

  indexes.each do |keys, opts|
    new_collection.indexes.create_one( keys, opts )    rescue  nil
  end

  self.class_variable_set( :@@collection, new_collection )    if temp_collection_name.nil?
  new_collection
end

#collection_nameObject



22
23
24
25
26
27
28
29
# File 'lib/mongous/extention.rb', line 22

def collection_name
  if self.class_variable_defined?( :@@collection_name )
    value  =  self.class_variable_get( :@@collection_name )
    return  value    if value
  end

  self.class_variable_set( :@@collection_name, self.name )
end

#collection_name=(new_collection_name) ⇒ Object



31
32
33
34
35
36
# File 'lib/mongous/extention.rb', line 31

def collection_name=( new_collection_name )
  self.class_variable_set( :@@collection_name, new_collection_name )
  if self.class_variable_defined?( :@@collection )
    self.remove_class_variable( :@@collection )
  end
end

#countObject



4
5
6
# File 'lib/mongous/filter.rb', line 4

def count
  self.collection.estimated_document_count
end

#create(**doc) ⇒ Object



100
101
102
# File 'lib/mongous/extention.rb', line 100

def create( **doc )
  self.new( **doc ).save
end

#defaultsObject



86
87
88
# File 'lib/mongous/extention.rb', line 86

def defaults
  setup_class_variable( :@@defaults, {} )
end

#deleteObject



22
23
24
# File 'lib/mongous/filter.rb', line 22

def delete
  self.collection.delete_many({})
end

#dropObject



104
105
106
# File 'lib/mongous/extention.rb', line 104

def drop
  self.collection.drop
end

#each(&block) ⇒ Object



14
15
16
# File 'lib/mongous/filter.rb', line 14

def each( &block )
  all.each( &block )
end

#field(symbol, *attrs, **items) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mongous/extention.rb', line 112

def field( symbol, *attrs, **items )
  m  =  /(.*?):(\d+)/.match( caller()[0] )
  call_from  =  [ m[1], m[2] ].join(":")

  attrs.each do |attr|
    if ( klass  =  attr.class )
      if ![Class, Range, Array, Regexp, Proc, Symbol].include?(klass)
        raise  Mongous::Error, "'field' arguments error. : #{ attr } on #{ symbol } at #{ call_from }"
      end
    end
  end

  items.each do |key, value|
    next    if [:default, :create, :update].include?(key) && [Proc, String, Numeric].include?(value.class)

    raise  Mongous::Error, "'field' options error. : #{key} on #{ symbol } at #{ call_from }"
  end

  items[:_attrs]  =  attrs
  fields[symbol.to_s]  =  items
end

#fieldsObject



66
67
68
# File 'lib/mongous/extention.rb', line 66

def fields
  setup_class_variable( :@@fields, {} )
end

#filter(symbol, filter_or_condition) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/mongous/extention.rb', line 157

def filter( symbol, filter_or_condition )
  case  filter_or_condition
  when  Filter
    filters[symbol]  =  filter_or_condition.to_condition
  when  Hash
    filters[symbol]  =  filter_or_condition
  else
    m  =  /(.*?):(\d+)/.match( caller()[0] )
    call_from  =  [ m[1], m[2] ].join(":")
    raise  Mongous::Error, "'filter' arguments error. : #{symbol}, #{filter_or_condition} at #{ call_from }"
  end
end

#filtersObject



82
83
84
# File 'lib/mongous/extention.rb', line 82

def filters
  setup_class_variable( :@@filters, {} )
end

#find(conditios = {}, options = {}) ⇒ Object



108
109
110
# File 'lib/mongous/extention.rb', line 108

def find( conditios = {}, options = {} )
  self.collection.find( conditios, options )
end

#firstObject



34
35
36
# File 'lib/mongous/filter.rb', line 34

def first
  Filter.new( self ).first
end

#index(*symbols, **options) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/mongous/extention.rb', line 148

def index( *symbols, **options )
  options[:background]  =  true    unless  options.has_key?(:background)
  keys  =  {}
  symbols.each do |symbol|
    keys[symbol]  =  1
  end
  indexes.push  <<  [keys, options]
end

#indexesObject



78
79
80
# File 'lib/mongous/extention.rb', line 78

def indexes
  setup_class_variable( :@@indexes, [] )
end

#lastObject



38
39
40
# File 'lib/mongous/filter.rb', line 38

def last
  Filter.new( self ).last
end

#map(&block) ⇒ Object



18
19
20
# File 'lib/mongous/filter.rb', line 18

def map( &block )
  all.map( &block )
end

#normalize(filter, conditions) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mongous/filter.rb', line 80

def normalize( filter, conditions )
  case  filter
  when  Filter
    filter.to_condition
  when  Symbol
    case  ( new_filter  =  filters[filter] )
    when  Filter
      new_filter.to_condition
    when  Hash
      new_filter
    end
  when  NilClass
    Filter.new( self ).where( conditions ).to_condition
  else
    caller_method  =  /`(.*?)'/.match( caller()[0] )[1]
    raise  Mongous::Error, "Invalid args for #{self}.#{ caller_method }. : #{filter}, #{conditions}"
  end
end

#not(filter = nil, **conditions) ⇒ Object

Raises:



55
56
57
58
59
60
# File 'lib/mongous/filter.rb', line 55

def not( filter = nil, **conditions )
  raise  Mongous::Error, "Unset args for #{self}.not."    if filter.nil? && conditions.empty?

  condition  =  normalize( filter, conditions )
  Filter.new( self ).not( condition )
end

#or(*filters) ⇒ Object

Raises:



71
72
73
74
75
76
77
78
# File 'lib/mongous/filter.rb', line 71

def or( *filters )
  raise  Mongous::Error, "Unset args for #{self}.or."    if filters.empty?

  conditions  =  filters.map do |filter|
    normalize( filter, {} )
  end
  Filter.new( self ).where({"$or" => conditions})
end

#select(*keys, **hash) ⇒ Object



46
47
48
# File 'lib/mongous/filter.rb', line 46

def select( *keys, **hash )
  Filter.new( self ).select( *keys, **hash )
end

#setup_class_variable(symbol, default = {}, &block) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/mongous/extention.rb', line 90

def setup_class_variable( symbol, default = {}, &block )
  if self.class_variable_defined?( symbol )
    self.class_variable_get( symbol )
  elsif block_given?
    self.class_variable_set( symbol, block.call )
  else
    self.class_variable_set( symbol, default )
  end
end

#sort(*keys, **hash) ⇒ Object



42
43
44
# File 'lib/mongous/filter.rb', line 42

def sort( *keys, **hash )
  Filter.new( self ).sort( *keys, **hash )
end

#symbolsObject



70
71
72
# File 'lib/mongous/extention.rb', line 70

def symbols
  setup_class_variable( :@@symbols, {} )
end

#verify(*directives, &block) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/mongous/extention.rb', line 134

def verify( *directives, &block )
  if !directives.empty?
    directives.each do |directive|
      symbols[directive]  =  true
    end
  elsif block
    m  =  /(.*?):(\d+)/.match( caller()[0] )
    call_from  =  [ m[1], m[2] ].join(":")
    blocks[call_from]  =  block
  else
    raise  Mongous::Error, "'verify' arguments error. need directives or block."
  end
end

#where(filter = nil, **conditions) ⇒ Object



50
51
52
53
# File 'lib/mongous/filter.rb', line 50

def where( filter = nil, **conditions )
  condition  =  normalize( filter, conditions )
  Filter.new( self ).where( condition )
end