Class: Mongokit::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/mongokit/utils/options.rb

Defined Under Namespace

Modules: Store

Constant Summary collapse

CRITERIAS =
[:only, :except, :add, :replace]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Options

Returns a new instance of Options.



6
7
8
# File 'lib/mongokit/utils/options.rb', line 6

def initialize(options = {})
  @options = options
end

Class Method Details

.add(fields, options) ⇒ Object



33
34
35
36
37
# File 'lib/mongokit/utils/options.rb', line 33

def add(fields, options)
  return unless options[:add]

  fields.is_a?(Hash) ? fields.merge(options[:add]) : fields.concat(options[:add])
end

.except(fields, options) ⇒ Object



28
29
30
31
# File 'lib/mongokit/utils/options.rb', line 28

def except(fields, options)
  fields = fields.reject{ |f, _| options[:except].include?(f) } if options[:except]
  fields
end

.only(fields, options) ⇒ Object



23
24
25
26
# File 'lib/mongokit/utils/options.rb', line 23

def only(fields, options)
  fields = fields.select{ |f, _| options[:only].include?(f) } if options[:only]
  fields
end

.process(fields, options, criterias = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/mongokit/utils/options.rb', line 44

def process(fields, options, criterias = nil)
  criterias = options.keys unless criterias

  criterias.each do |c|
    fields = send(c, fields, options) if CRITERIAS.include?(c)
  end

  fields
end

.replace(fields, options) ⇒ Object



39
40
41
42
# File 'lib/mongokit/utils/options.rb', line 39

def replace(fields, options)
  options[:replace].each { |old_f, new_f| fields[new_f] = fields.delete(old_f) } if options[:replace]
  fields
end

Instance Method Details

#allObject



10
11
12
# File 'lib/mongokit/utils/options.rb', line 10

def all
  @options
end

#get(name, field = nil) ⇒ Object



18
19
20
# File 'lib/mongokit/utils/options.rb', line 18

def get(name, field = nil)
  field ? @options[name][field] : @options[name]
end

#put(name, values) ⇒ Object



14
15
16
# File 'lib/mongokit/utils/options.rb', line 14

def put(name, values)
  @options[name] = values
end