Class: ZoneUpdate
Instance Attribute Summary
Attributes inherited from Command
#desc, #name
Instance Method Summary
collapse
Methods inherited from Command
#add_arg, #add_cmd, #add_flow, #add_flow_from_usage, #add_input, #add_option, #add_options, #command_name?, #flow_passes_parse, #flow_passes_preconditions, #get_args_used, #init, #initialize, #option_help_string, #run, #show_help, #show_help_option
Constructor Details
This class inherits a constructor from Command
Instance Method Details
#boolean_value_of(s) ⇒ Object
12
13
14
15
16
17
18
19
20
|
# File 'lib/zone_update.rb', line 12
def boolean_value_of(s)
if s == 'true'
true
elsif s == 'false'
false
else
raise "illegal value of boolean:#{s}"
end
end
|
#create_args ⇒ Object
4
5
6
|
# File 'lib/zone_update.rb', line 4
def create_args
add_arg('name-value','of the form name:new-value, where name is the (abbreviated) name of field', /[[:alpha:]]+:.*/)
end
|
#create_flows ⇒ Object
8
9
10
|
# File 'lib/zone_update.rb', line 8
def create_flows
add_flow_from_usage("<name-value>...")
end
|
#empty_binding ⇒ Object
22
23
24
|
# File 'lib/zone_update.rb', line 22
def empty_binding
binding()
end
|
#execute(arghash, rest) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/zone_update.rb', line 84
def execute(arghash, rest)
nvarray = arghash["name-value"]
struct = @input["struct"]
if struct.class == Hash
nvarray.each { | nv |
name, value = Util.parse_line(nv,':')
update_struct(struct, name, value)
}
elsif struct.class == Array
raise "can only update single item" if struct.length > 1
update_struct(struct[0], name, value)
else
raise "can't update at this level"
end
end
|
#guess_cast_value(value) ⇒ Object
def guess_cast_value(value)
b = empty_binding
result = nil
begin
result = eval(value, b)
rescue
raise "Unable to parse expression #{value}"
end
result
end
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/zone_update.rb', line 37
def guess_cast_value(value)
result = nil
if result.to_i.to_s == result
result = value.to_i
elsif value == 'null'
result = nil
elsif value == 'true'
result = true
elsif value == 'false'
result = false
else
result = value
end
result
end
|
#update_struct(struct, key, value) ⇒ 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
|
# File 'lib/zone_update.rb', line 53
def update_struct(struct, key, value)
name = Util.match_key(key, struct)
if name.nil?
raise "no (unique) key matches" + key
end
target_type = struct[name].class
case [target_type]
when [Fixnum]
value = value.to_i
when [TrueClass]
value = boolean_value_of(value)
when [FalseClass]
value = boolean_value_of(value)
when [String]
when [NilClass]
value = guess_cast_value(value)
when [Hash]
if value == 'null'
value = nil
else
raise "cannot change entire object to a scaler value"
end
else
raise "cannot change item of type #{target_type}"
end
struct[name] = value
zone = @input["zone"]
zone.write_zone_file(@input["toplevel"], false)
StructurePrint.new("-").structure_print(struct, "")
end
|