Module: Origin::Extensions::Array

Defined in:
lib/origin/extensions/array.rb

Overview

The array module adds custom behaviour for Origin onto the Array class.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary (collapse)

Instance Method Details

- (Object) __add__(object)

Combine the two objects using the add strategy.

Examples:

Add the object to the array.

[ 1, 2, 3 ].__add__(4)

Parameters:

  • object (Object)

    The object to add.

Returns:

  • (Object)

    The result of the add.

Since:

  • 1.0.0



18
19
20
# File 'lib/origin/extensions/array.rb', line 18

def __add__(object)
  object.__add_from_array__(self)
end

- (Array) __array__

Return the object as an array.

Examples:

Get the array.

[ 1, 2 ].__array__

Returns:

Since:

  • 1.0.0



30
# File 'lib/origin/extensions/array.rb', line 30

def __array__; self; end

- (Array) __deep_copy__

Makes a deep copy of the array, deep copying every element inside the array.

Examples:

Get a deep copy of the array.

[ 1, 2, 3 ].__deep_copy__

Returns:

  • (Array)

    The deep copy of the array.

Since:

  • 1.0.0



41
42
43
# File 'lib/origin/extensions/array.rb', line 41

def __deep_copy__
  map { |value| value.__deep_copy__ }
end

- (Array<Time>) __evolve_date__

Evolve the array into an array of mongo friendly dates. (Times at midnight).

Examples:

Evolve the array to dates.

[ Date.new(2010, 1, 1) ].__evolve_date__

Returns:

  • (Array<Time>)

    The array as times at midnight UTC.

Since:

  • 1.0.0



54
55
56
# File 'lib/origin/extensions/array.rb', line 54

def __evolve_date__
  map { |value| value.__evolve_date__ }
end

- (Array<Time>) __evolve_time__

Evolve the array to an array of times.

Examples:

Evolve the array to times.

[ 1231231231 ].__evolve_time__

Returns:

Since:

  • 1.0.0



80
81
82
# File 'lib/origin/extensions/array.rb', line 80

def __evolve_time__
  map { |value| value.__evolve_time__ }
end

- (Array) __expand_complex__

Get the object as expanded.

Examples:

Get the object expanded.

obj.__expand_complex__

Returns:

  • (Array)

    The expanded array.

Since:

  • 1.1.0



66
67
68
69
70
# File 'lib/origin/extensions/array.rb', line 66

def __expand_complex__
  map do |value|
    value.__expand_complex__
  end
end

- (Object) __intersect__(object)

Combine the two objects using an intersection strategy.

Examples:

Interset with the object.

[ 1, 2 ].__intersect__(3)

Parameters:

  • object (Object)

    The object to intersect with.

Returns:

  • (Object)

    The result of the intersection.

Since:

  • 1.0.0



94
95
96
# File 'lib/origin/extensions/array.rb', line 94

def __intersect__(object)
  object.__intersect_from_array__(self)
end

- (Hash) __sort_option__

Gets the array as options in the proper format to pass as MongoDB sort criteria.

Examples:

Get the array as sorting options.

[ :field, 1 ].__sort_option__

Returns:

  • (Hash)

    The array as sort criterion.

Since:

  • 1.0.0



107
108
109
110
111
112
# File 'lib/origin/extensions/array.rb', line 107

def __sort_option__
  multi.inject({}) do |options, criteria|
    options.merge!(criteria.__sort_pair__)
    options
  end
end

- (Hash) __sort_pair__

Get the array as a sort pair.

Examples:

Get the array as field/direction pair.

[ field, 1 ].__sort_pair__

Returns:

  • (Hash)

    The field/direction pair.

Since:

  • 1.0.0



122
123
124
# File 'lib/origin/extensions/array.rb', line 122

def __sort_pair__
  { first => last.to_direction }
end

- (Array) update_values(&block)

Update all the values in the hash with the provided block.

Examples:

Update the values in place.

[ 1, 2, 3 ].update_values(&:to_s)

Parameters:

  • block (Proc)

    The block to execute on each value.

Returns:

Since:

  • 1.0.0



136
137
138
# File 'lib/origin/extensions/array.rb', line 136

def update_values(&block)
  replace(map(&block))
end