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

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

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

- (Object) block (readonly)

Returns the value of attribute block



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

def block
  @block
end

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



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

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

- (Object) expanded (readonly)

Returns the value of attribute expanded



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

def expanded
  @expanded
end

- (Object) expanded The MongoDB expanded query operator.(TheMongoDBexpandedqueryoperator.) (readonly)



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

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

- (Object) name (readonly)

Returns the value of attribute name



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

def name
  @name
end

- (Object) name The name of the field.(Thenameofthefield.) (readonly)



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

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

- (Object) operator (readonly)

Returns the value of attribute operator



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

def operator
  @operator
end

- (Object) operator The MongoDB query operator.(TheMongoDBqueryoperator.) (readonly)



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

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

- (Object) strategy (readonly)

Returns the value of attribute strategy



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

def strategy
  @strategy
end

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



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

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

Instance Method Details

- (true, false) ==(other) 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

- (Hash) __expr_part__(object, negating = false)

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

- (Hash) __sort_option__ 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

- (Fixnum) hash

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

- (String) to_s

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