Sha256: 6f9286c9b8585f875d2ad0c40a9dc22a714697fb6f4ba01d25bc4b105a73c655

Contents?: true

Size: 869 Bytes

Versions: 7

Compression:

Stored size: 869 Bytes

Contents

# = Chain
#
# Expiremental library for safe method chaining.
#
#   person = "John Doe"
#   def person.address = "123 West St."
#   person.address.street #=> Error
#
# To avoid the error use #chain:
#
#   person.chain.address.street.end   #=> nil
#
# Or
#
#   ~ person.chain.address.street     #=> nil
#
class Chain

  private *instance_methods.select{|m| m !~ /^__/ }

  def initialize(value)
    @value = value
  end

  def method_missing(s,*a, &b)
    if @value.respond_to?(s)
      @value = @value.__send__(s,*a,&b)
    else
      @missing = true
    end
    #begin
    #  @value = @value.__send__(s,*a,&b)
    #rescue NoMethodError
    #  @missing = true
    #end
    self
  end

  #def value?
  #  @value
  #end

  def end
    @missing ? nil : @value
  end

  def ~@
    @missing ? nil : @value
  end

end

module Kernel
  def chain
    Chain.new(self)
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
facets-2.4.0 lib/facets/chain.rb
facets-2.4.1 lib/facets/chain.rb
facets-2.4.2 lib/more/facets/chain.rb
facets-2.4.3 lib/more/facets/chain.rb
facets-2.4.4 lib/more/facets/chain.rb
facets-2.4.5 lib/more/facets/chain.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/more/facets/chain.rb