Class: Collins::SimpleCallback

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/collins/simple_callback.rb

Overview

Represents a simple callback, e.g. something with a name, options and an exec block This is designed to be a building block for other callback implementations as opposed to a complete implementation. Note that we duck-type Proc. We can’t extend it without running into some initialize issues. We do this so people can feel free to tread the class like a proc.

Constant Summary collapse

EMPTY_NAME =
:None

Constants included from Util::Logging

Util::Logging::DEFAULT_LOG_FORMAT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#deep_copy_hash, #get_asset_or_tag, included, #require_non_empty, #require_that, #stringify_hash, #symbolize_hash

Methods included from Util::Logging

#get_logger

Constructor Details

#initialize(*args) {|block| ... } ⇒ SimpleCallback

Instantiate a new SimpleCallback

Examples:

SimpleCallback.new(:my_callback, Proc.new {|arg1| puts(arg1)})
SimpleCallback.new(:my_callback, :block => Proc.new{|arg1| puts(arg1)})
SimpleCallback.new :my_callback, :opt1 => "val" do |arg1|
  puts(arg1)
end

Parameters:

  • args (Array, Hash)

    Arguments for instantiation

Yield Parameters:

  • block (Array<Object>)

    Callback to execute, can also be specified via :block option

Raises:

  • (ArgumentError)

    when name not set, and not created via #empty



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/collins/simple_callback.rb', line 39

def initialize *args, &block
  opts = {}
  while arg = args.shift do
    if arg.is_a?(Hash) then
      opts.update(arg)
    elsif arg.respond_to?(:call) then
      opts.update(:block => arg)
    else
      key = [:name, :options].select{|k| !opts.key?(k)}.first
      opts.update(key => arg) unless key.nil?
    end
  end
  if block && block.respond_to?(:call) then
    opts.update(:block => block)
  end
  opts = symbolize_hash(opts)
  if opts.fetch(:none, false) then
    @name = EMPTY_NAME
    @block = ::Collins::None.new
    @options = {}
  else
    @name = ::Collins::Option(opts.delete(:name)).get_or_else {
      raise ArgumentError.new("SimpleCallback requires a name")
    }.to_sym
    @block = ::Collins::Option(opts.delete(:block))
    @options = opts
  end
end

Instance Attribute Details

#nameSymbol (readonly)

Returns Name of the callback.

Returns:

  • (Symbol)

    Name of the callback



16
17
18
# File 'lib/collins/simple_callback.rb', line 16

def name
  @name
end

#optionsHash (readonly)

Returns Options specified with the callback, after normalizing.

Returns:

  • (Hash)

    Options specified with the callback, after normalizing



19
20
21
# File 'lib/collins/simple_callback.rb', line 19

def options
  @options
end

Class Method Details

.emptyCollins::SimpleCallback

Returns #empty? will return true.

Returns:



22
23
24
# File 'lib/collins/simple_callback.rb', line 22

def self.empty
  Collins::SimpleCallback.new(:none => true) {}
end

Instance Method Details

#[](key) ⇒ Object

Returns value associated with key.

Parameters:

  • key (String, Symbol)

Returns:

  • (Object)

    value associated with key

See Also:

  • Hash


71
72
73
# File 'lib/collins/simple_callback.rb', line 71

def [](key)
  options[key.to_sym]
end

#arityFixnum

Returns number of arguments that would not be ignored.

Returns:

  • (Fixnum)

    number of arguments that would not be ignored

See Also:

  • Proc


77
78
79
# File 'lib/collins/simple_callback.rb', line 77

def arity
  block.map {|b| b.arity}.get_or_else(0)
end

#bindingBinding

Returns binding associated with the proc.

Returns:

  • (Binding)

    binding associated with the proc

See Also:

  • Proc


82
83
84
# File 'lib/collins/simple_callback.rb', line 82

def binding
  to_proc.binding
end

#call(*args) ⇒ Object

Call the callback block with the specified arguments

Parameters:

  • args (Array<Object>)
    • Arguments for callback

Returns:

  • (Object)

    Value from callback

Raises:

  • (NameError)

    if block wasn’t specified but call was executed

See Also:

  • Proc


125
126
127
128
129
# File 'lib/collins/simple_callback.rb', line 125

def call *args
  block.map do |b|
    b.call(*args)
  end.get
end

#defined?Boolean

Returns True if block was given to constructor.

Returns:

  • (Boolean)

    True if block was given to constructor



111
112
113
# File 'lib/collins/simple_callback.rb', line 111

def defined?
  name != EMPTY_NAME || block.defined?
end

#empty?Boolean

Returns False if block was given to constructor.

Returns:

  • (Boolean)

    False if block was given to constructor



115
116
117
# File 'lib/collins/simple_callback.rb', line 115

def empty?
  name == EMPTY_NAME && block.empty?
end

#lambda?Boolean

Returns lambda or not.

Returns:

  • (Boolean)

    lambda or not

See Also:

  • Proc


87
88
89
# File 'lib/collins/simple_callback.rb', line 87

def lambda?
  block.map {|b| b.lambda?}.get_or_else(false)
end

#parametersArray<Array<Symbol>>

Returns array of parameters accepted by proc.

Returns:

  • (Array<Array<Symbol>>)

    array of parameters accepted by proc

See Also:

  • Proc


92
93
94
# File 'lib/collins/simple_callback.rb', line 92

def parameters
  block.map {|b| b.parameters}.get_or_else([])
end

#to_optionCollins::Option

Returns Self as option.

Returns:



102
103
104
105
106
107
108
# File 'lib/collins/simple_callback.rb', line 102

def to_option
  if self.defined? then
    ::Collins::Some.new(self)
  else
    ::Collins::None.new
  end
end

#to_procProc

Returns proc or noop proc.

Returns:

  • (Proc)

    proc or noop proc

See Also:

  • Proc


97
98
99
# File 'lib/collins/simple_callback.rb', line 97

def to_proc
  block.map{|b| b.to_proc}.get_or_else(Proc.new{})
end

#to_sString

Returns representation of callback.

Returns:

  • (String)

    representation of callback



132
133
134
# File 'lib/collins/simple_callback.rb', line 132

def to_s
  "Collins::SimpleCallback(name = #{name}, options = #{options.inspect})"
end