Module: Traitorous::StandardProcs
- Defined in:
- lib/traitorous/standard_procs.rb
Constant Summary collapse
- NOOP =
proc { |data| data }
Class Method Summary collapse
-
.call_on(klass, with_method: :new) ⇒ Proc
This proc calls klass with :with and passes data as params.
-
.call_on_self(method_name) ⇒ Proc
This proc calls method_name on data.
-
.default(default_value) ⇒ Proc
a proc that simply passes through it’s input if the input is truthy, otherwise it returns it’s default value.
-
.inject(memo_obj, &block) ⇒ Proc
This proc calls map on data, and calls the block for each element.
-
.map(&block) ⇒ Proc
This proc calls map on data, and calls the block for each element.
-
.noop ⇒ Object
a proc that simply passes through it’s input as output.
Class Method Details
.call_on(klass, with_method: :new) ⇒ Proc
This proc calls klass with :with and passes data as params
31 32 33 |
# File 'lib/traitorous/standard_procs.rb', line 31 def call_on(klass, with_method: :new) proc { |data| klass.send(with_method, data) } end |
.call_on_self(method_name) ⇒ Proc
This proc calls method_name on data.
23 24 25 |
# File 'lib/traitorous/standard_procs.rb', line 23 def call_on_self(method_name) proc { |data| data.send(method_name) } end |
.default(default_value) ⇒ Proc
a proc that simply passes through it’s input if the input is truthy,
otherwise it returns it's default value
16 17 18 |
# File 'lib/traitorous/standard_procs.rb', line 16 def default(default_value) proc { |data| data || default_value } end |
.inject(memo_obj, &block) ⇒ Proc
This proc calls map on data, and calls the block for each element.
48 49 50 51 52 53 54 55 56 |
# File 'lib/traitorous/standard_procs.rb', line 48 def inject(memo_obj, &block) raise ArgumentError.new("Must be called with block") unless block_given? proc do |data_arr| Array(data_arr).inject(memo_obj.dup) do |memo_obj, data| block.call(memo_obj, data) memo_obj end end end |
.map(&block) ⇒ Proc
This proc calls map on data, and calls the block for each element.
38 39 40 41 |
# File 'lib/traitorous/standard_procs.rb', line 38 def map(&block) raise ArgumentError.new("Must be called with block") unless block_given? proc { |data_arr| Array(data_arr).map { |data| block.call(data) } } end |
.noop ⇒ Object
a proc that simply passes through it’s input as output
7 8 9 |
# File 'lib/traitorous/standard_procs.rb', line 7 def noop NOOP end |