Module: Traitorous::StandardProcs

Defined in:
lib/traitorous/standard_procs.rb

Constant Summary collapse

NOOP =
proc { |data| data }

Class Method Summary collapse

Class Method Details

.call_on(klass, with_method: :new) ⇒ Proc

This proc calls klass with :with and passes data as params

Parameters:

  • klass (Class, Module, Object)

    obj to call

  • with_method: (Symbol, String) (defaults to: :new)

    method to send klass with data as params

Returns:

  • (Proc)


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.

Parameters:

  • method_name (Symbol, String)

    method to send data

Returns:

  • (Proc)


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

Parameters:

  • default_value (Object)

    the default value to use with data is falsey

Returns:

  • (Proc)


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.

Parameters:

  • memo_obj (Array, Hash, #dup)

    dup called on this and used as the seed object for the inject call.

  • block (Class, Module, Object)

    obj to call

Returns:

  • (Proc)

Raises:

  • (ArgumentError)


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.

Parameters:

  • block (Class, Module, Object)

    obj to call

Returns:

  • (Proc)

Raises:

  • (ArgumentError)


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

.noopObject

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