Class: Musa::Extension::SmartProcBinder::SmartProcBinder

Inherits:
Object
  • Object
show all
Defined in:
lib/musa-dsl/core-ext/smart-proc-binder.rb

Instance Method Summary collapse

Constructor Details

#initialize(block, on_rescue: nil) ⇒ SmartProcBinder

Returns a new instance of SmartProcBinder.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/musa-dsl/core-ext/smart-proc-binder.rb', line 5

def initialize(block, on_rescue: nil)
  @block = block
  @on_rescue = on_rescue

  @key_parameters = {}
  @has_key_rest = false

  @value_parameters_count = 0
  @has_value_rest = false

  block.parameters.each do |parameter|
    @key_parameters[parameter[1]] = nil if parameter[0] == :key || parameter[0] == :keyreq
    @has_key_rest = true if parameter[0] == :keyrest

    @value_parameters_count += 1 if parameter[0] == :req || parameter[0] == :opt
    @has_value_rest = true if parameter[0] == :rest
  end
end

Instance Method Details

#_apply(value_parameters, key_parameters) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/musa-dsl/core-ext/smart-proc-binder.rb', line 76

def _apply(value_parameters, key_parameters)
  value_parameters ||= []
  key_parameters ||= {}

  if @has_value_rest
    values_result = value_parameters.clone
  else
    values_result = value_parameters.first(@value_parameters_count)
    values_result += Array.new(@value_parameters_count - values_result.size)
  end

  hash_result = @key_parameters.clone

  @key_parameters.each_key do |parameter_name|
    hash_result[parameter_name] = key_parameters[parameter_name]
  end

  if @has_key_rest
    key_parameters.each do |key, value|
      hash_result[key] = value unless hash_result.key?(key)
    end
  end

  return values_result, hash_result
end

#_call(value_parameters, key_parameters = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/musa-dsl/core-ext/smart-proc-binder.rb', line 36

def _call(value_parameters, key_parameters = {})
  if @on_rescue
    begin
      __call value_parameters, key_parameters
    rescue StandardError, ScriptError => e
      @on_rescue.call e
    end
  else
    __call value_parameters, key_parameters
  end
end

#apply(*value_parameters, **key_parameters) ⇒ Object



72
73
74
# File 'lib/musa-dsl/core-ext/smart-proc-binder.rb', line 72

def apply(*value_parameters, **key_parameters)
  _apply(value_parameters, key_parameters)
end

#call(*value_parameters, **key_parameters) ⇒ Object



32
33
34
# File 'lib/musa-dsl/core-ext/smart-proc-binder.rb', line 32

def call(*value_parameters, **key_parameters)
  _call value_parameters, key_parameters
end

#inspectObject Also known as: to_s



102
103
104
# File 'lib/musa-dsl/core-ext/smart-proc-binder.rb', line 102

def inspect
  "SmartProcBinder: parameters = #{parameters} key_parameters = #{@key_parameters} has_rest = #{@has_key_rest}"
end

#key?(key) ⇒ Boolean Also known as: has_key?

Returns:

  • (Boolean)


66
67
68
# File 'lib/musa-dsl/core-ext/smart-proc-binder.rb', line 66

def key?(key)
  @has_key_rest || @key_parameters.include?(key)
end

#parametersObject



28
29
30
# File 'lib/musa-dsl/core-ext/smart-proc-binder.rb', line 28

def parameters
  @block.parameters
end

#procObject



24
25
26
# File 'lib/musa-dsl/core-ext/smart-proc-binder.rb', line 24

def proc
  @block
end