Class: MonkeyKing::FunctionTag
- Inherits:
-
Object
- Object
- MonkeyKing::FunctionTag
- Defined in:
- lib/monkey_king/parser.rb
Instance Attribute Summary collapse
-
#scalar ⇒ Object
Returns the value of attribute scalar.
Instance Method Summary collapse
- #argment_count_checking(params_count, legit_count, function_name) ⇒ Object
- #encode_with(coder) ⇒ Object
- #expand(expression) ⇒ Object
- #gen_secret(length) ⇒ Object
- #get_env(key) ⇒ Object
- #init_with(coder) ⇒ Object
- #register(tag) ⇒ Object
Instance Attribute Details
#scalar ⇒ Object
Returns the value of attribute scalar.
23 24 25 |
# File 'lib/monkey_king/parser.rb', line 23 def scalar @scalar end |
Instance Method Details
#argment_count_checking(params_count, legit_count, function_name) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/monkey_king/parser.rb', line 45 def argment_count_checking(params_count, legit_count, function_name) if params_count > legit_count raise "too many arguments for #{function_name} function (#{params_count} of #{legit_count})" elsif params_count < legit_count raise "not enough arguments for #{function_name} function (#{params_count} of #{legit_count})" end end |
#encode_with(coder) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/monkey_king/parser.rb', line 36 def encode_with(coder) coder.style = Psych::Nodes::Mapping::FLOW s_expression = coder.tag.sub(/^!MK:/, '') s_expression.gsub!(/,/, ' ') expression_tree = Sexpistol.new.parse_string(s_expression) coder.scalar = (expression_tree).first end |
#expand(expression) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/monkey_king/parser.rb', line 53 def (expression) if expression.is_a? Array function_array = [] expression.each do |ex| function_array << (ex) end process_array = [] while !function_array.empty? do key_word = function_array.shift case key_word when :write params = function_array.shift argment_count_checking(params.size, 2, key_word) key = params.first value = params.last raise "attempting to redefine immutable variable #{key}, exiting" unless MonkeyKing.variables[key].nil? process_array << MonkeyKing.set_variable(key, value.to_s) when :read params = function_array.shift argment_count_checking(params.size, 1, key_word) key = params.first raise "unresolved variables #{key}" if MonkeyKing.variables[key].nil? process_array << MonkeyKing.variables[key] when :secret params = function_array.shift argment_count_checking(params.size, 1, key_word) raise "argument error for secret function: got #{params.first.class} instead of Fixnum" if !params.first.is_a? Fixnum length = params.first process_array << gen_secret(length) when :env params = function_array.shift argment_count_checking(params.size, 1, key_word) key = params.first.to_s process_array << get_env(key) when :write_value params = function_array.shift argment_count_checking(params.size, 1, key_word) key = params.first process_array << MonkeyKing.set_variable(key, self.scalar) when :format params = function_array.shift formating_string = params.pop raise('format template not found') if formating_string.nil? replace_count = formating_string.scan(/%s/).count argment_count_checking(params.size, replace_count, key_word) params.each do |param| formating_string = formating_string.sub(/%s/, param.to_s) end process_array << formating_string else process_array << key_word end end return process_array elsif expression.is_a? Symbol return expression elsif expression.is_a? Numeric return expression elsif expression.is_a? String return expression else raise "unknown expression #{expression}" end end |
#gen_secret(length) ⇒ Object
126 127 128 |
# File 'lib/monkey_king/parser.rb', line 126 def gen_secret(length) return [*('a'..'z'),*('0'..'9'),*('A'..'Z')].shuffle[0,length].join end |
#get_env(key) ⇒ Object
121 122 123 124 |
# File 'lib/monkey_king/parser.rb', line 121 def get_env(key) raise "#{key} not found in env" if ENV[key].nil? return ENV[key] end |
#init_with(coder) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/monkey_king/parser.rb', line 29 def init_with(coder) unless coder.type == :scalar raise "Dunno how to handle #{coder.type} for #{coder.inspect}" end self.scalar = coder.scalar end |
#register(tag) ⇒ Object
25 26 27 |
# File 'lib/monkey_king/parser.rb', line 25 def register(tag) self.class.send(:yaml_tag, tag) end |