Class: Rails::GraphQL::Helpers::InheritedCollection::Array

Inherits:
Base
  • Object
show all
Defined in:
lib/rails/graphql/helpers/inherited_collection/array.rb

Overview

A inherited collection of arrays that can be unique when it is a set

Instance Method Summary collapse

Methods inherited from Base

handle, #initialize

Constructor Details

This class inherits a constructor from Rails::GraphQL::Helpers::InheritedCollection::Base

Instance Method Details

#+(other) ⇒ Object

Allow concatenating objects



43
44
45
46
47
# File 'lib/rails/graphql/helpers/inherited_collection/array.rb', line 43

def +(other)
  result = to_a
  result = result.to_set if @type == :set
  result + other
end

#each(&block) ⇒ Object

The normal each is the reverse each of the definitions



28
29
30
# File 'lib/rails/graphql/helpers/inherited_collection/array.rb', line 28

def each(&block)
  lazy.reverse_each(&block)
end

#empty?Boolean

If any elements appears, the each block will run and return true

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/rails/graphql/helpers/inherited_collection/array.rb', line 22

def empty?
  lazy.each { return true }
  false
end

#find(value = nil, &block) ⇒ Object

Provide similar functionality of any? but returns the object instead



10
11
12
13
14
# File 'lib/rails/graphql/helpers/inherited_collection/array.rb', line 10

def find(value = nil, &block)
  block ||= !value.is_a?(Module) ? value.method(:==) : ->(val) { val.class <= value }
  reverse_each { |item| return item if block.call(item) }
  nil
end

#include?(value) ⇒ Boolean

Check if a given value is included in any of the definitions

Returns:

  • (Boolean)


17
18
19
# File 'lib/rails/graphql/helpers/inherited_collection/array.rb', line 17

def include?(value)
  each_definition.any? { |definition| definition.include?(value) }
end

#lazyObject

Overrides the lazy operator



38
39
40
# File 'lib/rails/graphql/helpers/inherited_collection/array.rb', line 38

def lazy
  (@type == :set) ? super.uniq : super
end

#reverse_each(&block) ⇒ Object

The reverse each is the normal each of the definitions



33
34
35
# File 'lib/rails/graphql/helpers/inherited_collection/array.rb', line 33

def reverse_each(&block)
  block.nil? ? lazy : lazy.each(&block)
end