module Mongous::Extention

Public Instance Methods

[]( nth_or_range, len = nil ) click to toggle source
# File lib/mongous/filter.rb, line 30
def []( nth_or_range, len = nil )
  Filter.new( self )[ nth_or_range, len ]
end
all() click to toggle source
# File lib/mongous/filter.rb, line 8
def all
  self.collection.find.map do |doc|
    self.new( **doc )
  end
end
and( *filters ) click to toggle source
# 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 ) click to toggle source
# File lib/mongous/filter.rb, line 26
def attach( collection_name )
  Filter.new( self ).attach( collection_name )
end
blocks() click to toggle source
# File lib/mongous/extention.rb, line 74
def blocks
  setup_class_variable( :@@blocks, {} )
end
client() click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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_name() click to toggle source
# 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 ) click to toggle source
# 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
count() click to toggle source
# File lib/mongous/filter.rb, line 4
def count
  self.collection.estimated_document_count
end
create( **doc ) click to toggle source
# File lib/mongous/extention.rb, line 100
def create( **doc )
  self.new( **doc ).save
end
defaults() click to toggle source
# File lib/mongous/extention.rb, line 86
def defaults
  setup_class_variable( :@@defaults, {} )
end
delete() click to toggle source
# File lib/mongous/filter.rb, line 22
def delete
  self.collection.delete_many({})
end
drop() click to toggle source
# File lib/mongous/extention.rb, line 104
def drop
  self.collection.drop
end
each( &block ) click to toggle source
# File lib/mongous/filter.rb, line 14
def each( &block )
  all.each( &block )
end
field( symbol, *attrs, **items ) click to toggle source
# 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
fields() click to toggle source
# File lib/mongous/extention.rb, line 66
def fields
  setup_class_variable( :@@fields, {} )
end
filter( symbol, filter_or_condition ) click to toggle source
# 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
filters() click to toggle source
# File lib/mongous/extention.rb, line 82
def filters
  setup_class_variable( :@@filters, {} )
end
find( conditios = {}, options = {} ) click to toggle source
# File lib/mongous/extention.rb, line 108
def find( conditios = {}, options = {} )
  self.collection.find( conditios, options )
end
first() click to toggle source
# File lib/mongous/filter.rb, line 34
def first
  Filter.new( self ).first
end
index( *symbols, **options ) click to toggle source
# 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
indexes() click to toggle source
# File lib/mongous/extention.rb, line 78
def indexes
  setup_class_variable( :@@indexes, [] )
end
last() click to toggle source
# File lib/mongous/filter.rb, line 38
def last
  Filter.new( self ).last
end
map( &block ) click to toggle source
# File lib/mongous/filter.rb, line 18
def map( &block )
  all.map( &block )
end
normalize( filter, conditions ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# File lib/mongous/filter.rb, line 46
def select( *keys, **hash )
  Filter.new( self ).select( *keys, **hash )
end
setup_class_variable( symbol, default = {}, &block ) click to toggle source
# 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 ) click to toggle source
# File lib/mongous/filter.rb, line 42
def sort( *keys, **hash )
  Filter.new( self ).sort( *keys, **hash )
end
symbols() click to toggle source
# File lib/mongous/extention.rb, line 70
def symbols
  setup_class_variable( :@@symbols, {} )
end
verify( *directives, &block ) click to toggle source
# 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 ) click to toggle source
# File lib/mongous/filter.rb, line 50
def where( filter = nil, **conditions )
  condition  =  normalize( filter, conditions )
  Filter.new( self ).where( condition )
end