Class: Collins::SimpleCallback
- Inherits:
-
Object
- Object
- Collins::SimpleCallback
- 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
-
#name ⇒ Symbol
readonly
Name of the callback.
-
#options ⇒ Hash
readonly
Options specified with the callback, after normalizing.
Class Method Summary collapse
-
.empty ⇒ Collins::SimpleCallback
#empty? will return true.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Value associated with key.
-
#arity ⇒ Fixnum
Number of arguments that would not be ignored.
-
#binding ⇒ Binding
Binding associated with the proc.
-
#call(*args) ⇒ Object
Call the callback block with the specified arguments.
-
#defined? ⇒ Boolean
True if block was given to constructor.
-
#empty? ⇒ Boolean
False if block was given to constructor.
-
#initialize(*args) {|block| ... } ⇒ SimpleCallback
constructor
Instantiate a new SimpleCallback.
-
#lambda? ⇒ Boolean
Lambda or not.
-
#parameters ⇒ Array<Array<Symbol>>
Array of parameters accepted by proc.
-
#to_option ⇒ Collins::Option
Self as option.
-
#to_proc ⇒ Proc
Proc or noop proc.
-
#to_s ⇒ String
Representation of callback.
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
Constructor Details
#initialize(*args) {|block| ... } ⇒ SimpleCallback
Instantiate a new SimpleCallback
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
#name ⇒ Symbol (readonly)
Returns Name of the callback.
16 17 18 |
# File 'lib/collins/simple_callback.rb', line 16 def name @name end |
#options ⇒ Hash (readonly)
Returns Options specified with the callback, after normalizing.
19 20 21 |
# File 'lib/collins/simple_callback.rb', line 19 def @options end |
Class Method Details
.empty ⇒ Collins::SimpleCallback
Returns #empty? will return true.
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.
71 72 73 |
# File 'lib/collins/simple_callback.rb', line 71 def [](key) [key.to_sym] end |
#arity ⇒ Fixnum
Returns number of arguments that would not be ignored.
77 78 79 |
# File 'lib/collins/simple_callback.rb', line 77 def arity block.map {|b| b.arity}.get_or_else(0) end |
#binding ⇒ Binding
Returns binding associated with the 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
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.
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.
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.
87 88 89 |
# File 'lib/collins/simple_callback.rb', line 87 def lambda? block.map {|b| b.lambda?}.get_or_else(false) end |
#parameters ⇒ Array<Array<Symbol>>
Returns array of parameters accepted by proc.
92 93 94 |
# File 'lib/collins/simple_callback.rb', line 92 def parameters block.map {|b| b.parameters}.get_or_else([]) end |
#to_option ⇒ Collins::Option
Returns Self as option.
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_proc ⇒ Proc
Returns proc or noop 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_s ⇒ String
Returns representation of callback.
132 133 134 |
# File 'lib/collins/simple_callback.rb', line 132 def to_s "Collins::SimpleCallback(name = #{name}, options = #{.inspect})" end |