class Rancher::Resource
Attributes
links[R]
meta[R]
Public Class Methods
new(body = nil)
click to toggle source
# File lib/rancher/resource.rb, line 10 def initialize(body = nil) @meta = {} return unless body body.each do |key, val| @links = val if key == :links @actions = val if key == :actions continue if key == :body @meta[key] = val unless key == :links || key == :actions end end
Public Instance Methods
action(name, *args)
click to toggle source
# File lib/rancher/resource.rb, line 42 def action(name, *args) if action?(name) opt = args[0] || {} link = @actions[name.to_sym] return Rancher.post link, opt end end
action?(name)
click to toggle source
# File lib/rancher/resource.rb, line 34 def action?(name) (@actions[name.to_sym]) end
get_link(name)
click to toggle source
# File lib/rancher/resource.rb, line 30 def get_link(name) @links[name.to_sym] if @links[name.to_sym] end
in_meta?(name)
click to toggle source
# File lib/rancher/resource.rb, line 38 def in_meta?(name) (@meta[name.to_sym]) end
method_missing(method_name, *args, &_block)
click to toggle source
# File lib/rancher/resource.rb, line 50 def method_missing(method_name, *args, &_block) str_method_name = method_name.to_s if str_method_name.start_with?('fetch') name = str_method_name[6..-1] do_fetch(name, *args) elsif str_method_name.start_with?('get') name = str_method_name[4..-1] if @meta.key?(name.to_sym) ## Todo Handle TS return in seconds @meta[name.to_sym] else field = schema_field(name) fail("Attempted to access unknown property '#{name}'") if field.nil? nil end elsif str_method_name.start_with?('set') name = str_method_name[4..-1] @meta[name.to_sym] = args[0] true elsif str_method_name.start_with?('do') name = str_method_name[3..-1] action(name, *args) elsif str_method_name.start_with?('can') name = str_method_name[4..-1] action?(name) end end
remove!()
click to toggle source
# File lib/rancher/resource.rb, line 26 def remove! Rancher.delete get_link('self') end
save!()
click to toggle source
# File lib/rancher/resource.rb, line 22 def save! Rancher.put get_link('self'), @meta end
Private Instance Methods
do_fetch(name, *args)
click to toggle source
# File lib/rancher/resource.rb, line 80 def do_fetch(name, *args) opts = args[0] || {} link = Rancher::Type.list_href(get_link(name), opts) Rancher.get link if link end
schema_field(name)
click to toggle source
# File lib/rancher/resource.rb, line 87 def schema_field(name) type_name = get_type type = Rancher.types[type_name.to_sym] type.resource_field(name) unless type.nil? end