Module: HammerCLI::Apipie::Resource::ClassMethods

Defined in:
lib/hammer_cli/apipie/resource.rb

Instance Method Summary collapse

Instance Method Details

#action(action = nil) ⇒ Object



69
70
71
72
73
# File 'lib/hammer_cli/apipie/resource.rb', line 69

def action(action=nil)
  @api_action = action unless action.nil?
  return @api_action if @api_action
  return superclass.action if superclass.respond_to? :action
end

#class_resourceObject



39
40
41
42
# File 'lib/hammer_cli/apipie/resource.rb', line 39

def class_resource
  return @api_resource if @api_resource
  return superclass.class_resource if superclass.respond_to? :class_resource
end

#connection_name(resource_class) ⇒ Object



35
36
37
# File 'lib/hammer_cli/apipie/resource.rb', line 35

def connection_name(resource_class)
  :apipie
end

#module_resourceObject



44
45
46
47
48
49
50
51
# File 'lib/hammer_cli/apipie/resource.rb', line 44

def module_resource
  return nil unless self.name
  enclosing_module = self.name.split("::")[0..-2].inject(Object) { |mod, cls| mod.const_get cls }

  if enclosing_module.respond_to? :resource
    enclosing_module.resource
  end
end

#resource(resource = nil, action = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hammer_cli/apipie/resource.rb', line 53

def resource(resource=nil, action=nil)
  unless resource.nil?
    api = HammerCLI.context[:api_connection].get(connection_name(resource))
    if api.has_resource?(resource)
      @api_resource = api.resource(resource)
    else
      logger.warn "Resource '#{resource}' does not exist in the API"
    end
  end
  @api_action = action unless action.nil?

  # if the resource definition is not available in this class
  # try to look it up in it's enclosing module/class
  return class_resource || module_resource
end

#resource_configObject



31
32
33
# File 'lib/hammer_cli/apipie/resource.rb', line 31

def resource_config
  {}
end

#resource_defined?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/hammer_cli/apipie/resource.rb', line 75

def resource_defined?
  not (resource.nil? or action.nil?)
end