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



52
53
54
55
# File 'lib/origin/key.rb', line 52

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 Also known as: eql?

Does the key equal another object?

Examples:

Is the key equal to another?

key == other
key.eql? other

Parameters:

  • other (Object)

    The object to compare to.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 1.0.0



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

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

#__expr_part__(object, negating = false) ⇒ Hash

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

Examples:

Specify the raw selector.

key.__expr_part__(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



68
69
70
71
72
# File 'lib/origin/key.rb', line 68

def __expr_part__(object, negating = false)
  value = block ? block[object] : object
  expression = { operator => expanded ? { expanded => value } : value }
  { name.to_s => (negating && operator != "$not") ? { "$not" => expression } : expression }
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



82
83
84
# File 'lib/origin/key.rb', line 82

def __sort_option__
  { name => operator }
end

#hashFixnum

Calculate the hash code for a key.

Returns:

  • (Fixnum)

    The hash code for the key.

Since:

  • 1.1.0



37
38
39
# File 'lib/origin/key.rb', line 37

def hash
  [name, operator, expanded].hash
end

#to_sString

Convert the key to a string.

Examples:

Convert the key to a string.

key.to_s

Returns:

  • (String)

    The key as a string.

Since:

  • 1.1.0



95
96
97
# File 'lib/origin/key.rb', line 95

def to_s
  @name.to_s
end