Class: Origin::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/origin/key.rb

Overview

The key is a representation of a field in a queryable, that can be expanded to special MongoDB selectors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, strategy, operator, expanded = nil, &block) ⇒ Key

Instantiate the new key.

Examples:

Instantiate the key.

Key.new("age", "$gt")

Parameters:

  • name (String, Symbol)

    The field name.

  • strategy (Symbol)

    The name of the merge strategy.

  • operator (String)

    The Mongo operator.

  • expanded (String) (defaults to: nil)

    The Mongo expanded operator.

Since:

  • 1.0.0



41
42
43
44
# File 'lib/origin/key.rb', line 41

def initialize(name, strategy, operator, expanded = nil, &block)
  @name, @strategy, @operator, @expanded, @block =
    name, strategy, operator, expanded, block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



13
14
15
# File 'lib/origin/key.rb', line 13

def block
  @block
end

#block The optional block to transform values.(Theoptionalblocktotransformvalues.) ⇒ Object (readonly)



13
# File 'lib/origin/key.rb', line 13

attr_reader :block, :name, :operator, :expanded, :strategy

#expandedObject (readonly)

Returns the value of attribute expanded.



13
14
15
# File 'lib/origin/key.rb', line 13

def expanded
  @expanded
end

#expanded The MongoDB expanded query operator.(TheMongoDBexpandedqueryoperator.) ⇒ Object (readonly)



13
# File 'lib/origin/key.rb', line 13

attr_reader :block, :name, :operator, :expanded, :strategy

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/origin/key.rb', line 13

def name
  @name
end

#name The name of the field.(Thenameofthefield.) ⇒ Object (readonly)



13
# File 'lib/origin/key.rb', line 13

attr_reader :block, :name, :operator, :expanded, :strategy

#operatorObject (readonly)

Returns the value of attribute operator.



13
14
15
# File 'lib/origin/key.rb', line 13

def operator
  @operator
end

#operator The MongoDB query operator.(TheMongoDBqueryoperator.) ⇒ Object (readonly)



13
# File 'lib/origin/key.rb', line 13

attr_reader :block, :name, :operator, :expanded, :strategy

#strategyObject (readonly)

Returns the value of attribute strategy.



13
14
15
# File 'lib/origin/key.rb', line 13

def strategy
  @strategy
end

#strategy The name of the merge strategy.(Thenameofthemergestrategy.) ⇒ Object (readonly)



13
# File 'lib/origin/key.rb', line 13

attr_reader :block, :name, :operator, :expanded, :strategy

Instance Method Details

#==(other) ⇒ true, false

Does the key equal another object?

Examples:

Is the key equal to another?

key == other

Parameters:

  • other (Object)

    The object to compare to.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 1.0.0



25
26
27
28
# File 'lib/origin/key.rb', line 25

def ==(other)
  return false unless other.is_a?(Key)
  name == other.name && operator == other.operator && expanded == other.expanded
end

#__sort_option__Hash Also known as: __sort_pair__

Get the key as raw Mongo sorting options.

Examples:

Get the key as a sort.

key.__sort_option__

Returns:

  • (Hash)

    The field/direction pair.

Since:

  • 1.0.0



71
72
73
# File 'lib/origin/key.rb', line 71

def __sort_option__
  { name => operator }
end

#specify(object, negating = false) ⇒ Hash

Gets the raw selector that would be passed to Mongo from this key.

Examples:

Specify the raw selector.

key.specify(50)

Parameters:

  • object (Object)

    The value to be included.

  • negating (true, false) (defaults to: false)

    If the selection should be negated.

Returns:

  • (Hash)

    The raw MongoDB selector.

Since:

  • 1.0.0



57
58
59
60
61
# File 'lib/origin/key.rb', line 57

def specify(object, negating = false)
  value = block ? block[object] : object
  expression = { operator => expanded ? { expanded => value } : value }
  { name.to_s => (negating && operator != "$not") ? { "$not" => expression } : expression }
end