Class: Hubkit::ChainableCollection Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/hubkit/chainable_collection.rb

Overview

This class is abstract.

A class which wraps an array (or Enumerable) and provides convenience

methods for chainable filters, e.g.:

Examples:

repo.issues.unassigned.labeled('in progress')

Direct Known Subclasses

EventCollection, IssueCollection, RepoCollection

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inner) ⇒ ChainableCollection

Create a new ChainableCollection


23
24
25
# File 'lib/hubkit/chainable_collection.rb', line 23

def initialize(inner)
  @inner = inner
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) {|...| ... } ⇒ Object

Call into the wrapped collection if a method has not been implemented on the ChainableCollection

Yield Parameters:

  • ...

    the parameters of the any block given to the method which is being called


60
61
62
63
# File 'lib/hubkit/chainable_collection.rb', line 60

def method_missing(name, *args, &block)
  return super unless @inner.respond_to?(name)
  @inner.send(name, *args, &block)
end

Class Method Details

.scope(name) {|...| ... } ⇒ Object

Allows definition of new chainable filters within the class definition

Examples:

scope :unlabeled, -> { |collection| collection.reject(&:labeled?) }

Yield Parameters:

  • ...

    any arguments needed by the block


12
13
14
15
16
17
18
# File 'lib/hubkit/chainable_collection.rb', line 12

def self.scope(name, &block)
  define_method name do |*args|
    wrap(
      instance_exec(*args, &block),
    )
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if the other collection contains the same elements


79
80
81
# File 'lib/hubkit/chainable_collection.rb', line 79

def ==(other)
  other == self.to_a
end

#notNotCollection

Returns a collection which will contain all elements which are contained in this ChainableCollection, but NOT matching any additional chained filters

Examples:

ChainableCollection.new([1, 2, 3, 4]).not.select(&:odd?) # even->[2, 4]

71
72
73
# File 'lib/hubkit/chainable_collection.rb', line 71

def not
  NotCollection.new(self)
end

#respond_to?(name, include_all = false) ⇒ Boolean

Check if a method is implemented by either this method or the wrapped collection


49
50
51
# File 'lib/hubkit/chainable_collection.rb', line 49

def respond_to?(name, include_all = false)
  super || @inner.respond_to?(name)
end

#select {|item| ... } ⇒ Object

Return a Hubkit::ChainableCollection containing all members for which the block is true. This new Hubkit::ChainableCollection will also be filterable in the same way.

Yield Parameters:

  • item

    the item to be evaluated by the block


31
32
33
34
# File 'lib/hubkit/chainable_collection.rb', line 31

def select(&block)
  return wrap(@inner.select &block) if block_given?
  wrap(@inner.select)
end

#wrap(items) ⇒ Object

Return a collection of the same type as `self`, containing `items`


39
40
41
# File 'lib/hubkit/chainable_collection.rb', line 39

def wrap(items)
  self.class.new(items)
end