class ChainableMethods::Link

Public Class Methods

new(object, context) click to toggle source
# File lib/chainable_methods.rb, line 27
def initialize(object, context)
  @state   = object
  @context = context
end

Public Instance Methods

chain(&block) click to toggle source
# File lib/chainable_methods.rb, line 32
def chain(&block)
  new_state = block.call(@state)
  ChainableMethods::Link.new( new_state, @context )
end
method_missing(method_name, *args, &block) click to toggle source
# File lib/chainable_methods.rb, line 37
def method_missing(method_name, *args, &block)
  if is_constant?(method_name)
    return ChainableMethods::Link.new( @state, ::Kernel.const_get(method_name) )
  end

  local_response   = @state.respond_to?(method_name)
  context_response = @context.respond_to?(method_name)

  # if the state itself has the means to respond, delegate to it
  # but if the context has the behavior, it has priority over the delegation
  new_state = if local_response && !context_response
                @state.send(method_name, *args, &block)
              else
                @context.send(method_name, *args.unshift(@state), &block)
              end

  ChainableMethods::Link.new( new_state, @context )
end
unwrap() click to toggle source
# File lib/chainable_methods.rb, line 56
def unwrap
  @state
end

Private Instance Methods

is_constant?(method_name) click to toggle source
# File lib/chainable_methods.rb, line 60
        def is_constant?(method_name)
  method_name_start = method_name.to_s.chars.first
  if method_name_start =~ /\A[[:alpha:]]+\z/i
    return ( method_name_start.upcase == method_name_start )
  end
  false
end