Class: Rancher::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/rancher/resource.rb

Overview

A Rancher Resource

Direct Known Subclasses

Collection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body = nil) ⇒ Resource

Returns a new instance of Resource.



10
11
12
13
14
15
16
17
18
19
20
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &_block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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

Instance Attribute Details

Returns the value of attribute links.



8
9
10
# File 'lib/rancher/resource.rb', line 8

def links
  @links
end

#metaObject (readonly)

Returns the value of attribute meta.



8
9
10
# File 'lib/rancher/resource.rb', line 8

def meta
  @meta
end

Instance Method Details

#action(name, *args) ⇒ Object



42
43
44
45
46
47
48
# 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) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/rancher/resource.rb', line 34

def action?(name)
  (@actions[name.to_sym])
end


30
31
32
# File 'lib/rancher/resource.rb', line 30

def get_link(name)
  @links[name.to_sym] if @links[name.to_sym]
end

#in_meta?(name) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/rancher/resource.rb', line 38

def in_meta?(name)
  (@meta[name.to_sym])
end

#remove!Object



26
27
28
# File 'lib/rancher/resource.rb', line 26

def remove!
  Rancher.delete get_link('self')
end

#save!Object



22
23
24
# File 'lib/rancher/resource.rb', line 22

def save!
  Rancher.put get_link('self'), @meta
end