Class: JsonFunc
- Inherits:
-
Object
show all
- Defined in:
- lib/json_func.rb
Defined Under Namespace
Classes: BadFunctionNameError, Function, ZeroFunctionsError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(handler) ⇒ JsonFunc
Returns a new instance of JsonFunc.
10
11
12
|
# File 'lib/json_func.rb', line 10
def initialize(handler)
@handler = handler
end
|
Instance Attribute Details
#handler ⇒ Object
Returns the value of attribute handler.
6
7
8
|
# File 'lib/json_func.rb', line 6
def handler
@handler
end
|
Instance Method Details
#execute(json) ⇒ Object
14
15
16
17
|
# File 'lib/json_func.rb', line 14
def execute(json)
initial_argument = JSON.parse(json)
parse_arg(initial_argument)
end
|
#execute_fuction(hash) ⇒ Object
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/json_func.rb', line 19
def execute_fuction(hash)
raise ZeroFunctionsError.new(hash.keys) if hash.size == 0
func = hash.first
raise BadFunctionNameError.new(func) unless valid_function?(func)
args = hash[1..-1].map do |arg|
parse_arg(arg)
end
return args if func == 'list'
handler.send(func, *args)
end
|
#parse_arg(arg) ⇒ Object
34
35
36
37
38
39
40
41
|
# File 'lib/json_func.rb', line 34
def parse_arg(arg)
case arg
when Array
execute_fuction(arg)
else
arg
end
end
|
#valid_function?(func) ⇒ Boolean
30
31
32
|
# File 'lib/json_func.rb', line 30
def valid_function?(func)
(handler.public_methods - Object.new.public_methods + ['list']).map(&:to_s).include? func
end
|