class Lupo

Includes Enumerable and provides each. Optionally includes Concord to provide initialize and Equalizer for equality methods

Constants

VERSION

Gem version

Public Class Methods

collection(name) click to toggle source

Build a module providing each and Enumerable

@example

class Collection
  include Lupo.collection(:entries)
end

collection = Collection.new([1,2,3])

collection.each { |i| puts(i) } # => collection
collection.each.to_a            # => [1,2,3]
collection.is_a?(Enumerable)    # => true

other = Collection.new([1,2,3])

# see equalizer for detailed docs
collection.equal?(other) # => false
collection.eql?(other)   # => true
collection == other      # => true

@param [#to_s] name

the name of the enumerable

@return [undefined]

@api public

# File lib/lupo.rb, line 67
def self.collection(name)
  new(name, [Enumerable, Concord.new(name)])
end
enumerable(name) click to toggle source

Build a module providing each, Enumerable and Concord

@example

class Collection
  include Lupo.enumerable(:entries)

  def initialize(entries)
    @entries = entries
  end
end

collection = Collection.new([1,2,3])

collection.each { |i| puts(i) } # => collection
collection.each.to_a            # => [1,2,3]
collection.is_a?(Enumerable)    # => true

@param [#to_s] name

the name of the enumerable

@return [undefined]

@api public

# File lib/lupo.rb, line 36
def self.enumerable(name)
  new(name, [Enumerable])
end
new(name, modules) click to toggle source

Initialize a new instance

@param [#to_s] name

the name of the enumerable

@param [#each] modules

the modules to include into a host

@return [undefined]

@api private

# File lib/lupo.rb, line 82
def initialize(name, modules)
  @modules, @body = modules, ->(&block) {
    return to_enum unless block
    instance_variable_get(:"@#{name}").each(&block)
    self
  }
end

Private Instance Methods

included(host) click to toggle source

Define the each method on the host and include modules

@param [Object] host

the hosting object

@return [undefined]

@api private

# File lib/lupo.rb, line 100
def included(host)
  host.instance_exec(@modules, @body) do |modules, body|
    include(*modules)
    define_method(:each, &body)
  end
end