class FunctionChain::BaseChain

Base class of PullChain, RelayChain

Public Instance Methods

add(function = nil, &block) click to toggle source

Add function to chain

Example: add(value) or add { your code }

# File lib/function_chain/base_chain.rb, line 13
def add(function = nil, &block)
  insert(chain_elements.size, function, &block)
end
add_all(*functions) click to toggle source

Add functions to chain

# File lib/function_chain/base_chain.rb, line 5
def add_all(*functions)
  insert_all(chain_elements.size, *functions)
  self
end
clear() click to toggle source

Clear function chain

# File lib/function_chain/base_chain.rb, line 43
def clear
  chain_elements.clear
  self
end
delete_at(index) click to toggle source

Delete from chain

# File lib/function_chain/base_chain.rb, line 37
def delete_at(index)
  chain_elements.delete_at(index)
  self
end
insert(index, function = nil, &block) click to toggle source

Insert function to chain

Example: insert(i, value) or insert(i) { your code }

# File lib/function_chain/base_chain.rb, line 26
def insert(index, function = nil, &block)
  validate_exclusive_value(function, block)
  case function
  when String then do_insert_by_string(index, function)
  when NilClass then do_insert(index, block)
  else do_insert(index, function)
  end
  self
end
insert_all(index, *functions) click to toggle source

Insert functions to chain

# File lib/function_chain/base_chain.rb, line 18
def insert_all(index, *functions)
  functions.each_with_index { |f, i| insert(index + i, f) }
  self
end
to_s() click to toggle source
# File lib/function_chain/base_chain.rb, line 48
def to_s
  "#{self.class}#{chain_elements.map(&:to_s)}"
end

Protected Instance Methods

chain_elements() click to toggle source
# File lib/function_chain/base_chain.rb, line 108
def chain_elements
  @chain_elements ||= []
end
create_chain_element(function) click to toggle source
# File lib/function_chain/base_chain.rb, line 69
    def create_chain_element(function)
      case function
      when Symbol then create_chain_element_by_symbol(function)
      when Array then create_chain_element_by_array(function)
      when String then create_chain_element_by_string(function)
      when Proc then create_chain_element_by_proc(function)
      else fail ArgumentError, <<-EOF.gsub(/^\s+|\n/, "")
        Not supported type #{function}(#{function.class}),
        supported type is #{supported_types}.
      EOF
      end
    end
def_to_s(target, value) click to toggle source
# File lib/function_chain/base_chain.rb, line 82
def def_to_s(target, value)
  target.singleton_class.class_eval do
    define_method :to_s do
      value
    end
  end
end
do_insert(index, function) click to toggle source
# File lib/function_chain/base_chain.rb, line 54
def do_insert(index, function)
  chain_element = create_chain_element(function)
  chain_elements.insert(index, chain_element)
  def_to_s(chain_element, function)
end
do_insert_by_string(index, function) click to toggle source
# File lib/function_chain/base_chain.rb, line 60
def do_insert_by_string(index, function)
  function.split(%r{(?<!\\)/}).reject(&:empty?).each_with_index do |f, i|
    splitted_function = f.gsub(%r{\\/}, "/")
    chain_element = create_chain_element(splitted_function)
    chain_elements.insert(index + i, chain_element)
    def_to_s(chain_element, splitted_function)
  end
end
supported_types() click to toggle source
# File lib/function_chain/base_chain.rb, line 112
def supported_types
  [Symbol, Array, String, Proc]
end
target_is_a_one_of_types?(target, types) click to toggle source
# File lib/function_chain/base_chain.rb, line 104
def target_is_a_one_of_types?(target, types)
  types.any? { |type| target.is_a? type }
end
validate_array_length(arr, expect_length, expect_format) click to toggle source
# File lib/function_chain/base_chain.rb, line 90
def validate_array_length(arr, expect_length, expect_format)
  unless expect_length == arr.length
    message = "Format Wrong #{arr}, expected format is [#{expect_format}]"
    fail ArgumentError, message
  end
end
validate_element_type_of_array(arr, i, expect_types, types_as_string) click to toggle source
# File lib/function_chain/base_chain.rb, line 97
def validate_element_type_of_array(arr, i, expect_types, types_as_string)
  unless target_is_a_one_of_types?(arr[i], expect_types)
    fail ArgumentError, "Format Wrong #{arr}," \
      " second element of array is must be #{types_as_string}"
  end
end
validate_exclusive_value(function, block) click to toggle source
# File lib/function_chain/base_chain.rb, line 116
def validate_exclusive_value(function, block)
  prefix = nil
  if function.nil? && block.nil?
    prefix = "Both value and the block is unspecified."
  elsif function && block
    prefix = "Both of value and block is specified."
  end
  if prefix
    fail ArgumentError, "#{prefix} Please specify either value or block."
  end
end