class HaveAPI::GoClient::Resource

Attributes

actions[R]

Resource actions @return [Array<Action>]

full_dot_name[R]

Full name with dots @return [String]

full_name[R]

Full name with underscores @return [String]

go_name[R]

Name in Go @return [String]

go_type[R]

Type in Go @return [String]

name[R]

Resource name as returned by the API @return [String]

parent[R]

Parent resource or API version @return [ApiServer, Resource]

prefix[R]
resources[R]

Child resources @return [Array<Resource>]

Public Class Methods

new(parent, name, desc, prefix: nil) click to toggle source
# File lib/haveapi/go_client/resource.rb, line 39
def initialize(parent, name, desc, prefix: nil)
  @parent = parent
  @name = name.to_s
  @prefix = prefix
  @full_name = resource_path.map(&:name).join('_')
  @full_dot_name = resource_path.map(&:name).map(&:capitalize).join('.')
  @go_name = camelize(name)
  @go_type = full_go_type
  @resources = desc[:resources].map { |k, v| Resource.new(self, k, v) }
  @actions = desc[:actions].map do |k, v|
    Action.new(self, k.to_s, v, prefix: prefix)
  end
end

Public Instance Methods

api_version() click to toggle source

@return [ApiVersion]

# File lib/haveapi/go_client/resource.rb, line 54
def api_version
  tmp = parent
  tmp = tmp.parent until tmp.is_a?(ApiVersion)
  tmp
end
generate(gen) click to toggle source
# File lib/haveapi/go_client/resource.rb, line 83
def generate(gen)
  ErbTemplate.render_to_if_changed(
    'resource.go',
    {
      package: gen.package,
      resource: self,
    },
    File.join(gen.dst, prefix_underscore("resource_#{full_name}.go"))
  )

  resources.each { |r| r.generate(gen) }

  actions.each do |a|
    ErbTemplate.render_to_if_changed(
      'action.go',
      {
        package: gen.package,
        action: a,
      },
      File.join(gen.dst, prefix_underscore("resource_#{full_name}_action_#{a.name}.go"))
    )
  end
end
parent_resources() click to toggle source

@return [Array<Resource>]

# File lib/haveapi/go_client/resource.rb, line 61
def parent_resources
  parents = []
  tmp = parent

  while tmp.is_a?(Resource)
    parents << tmp
    tmp = tmp.parent
  end

  parents.reverse
end
resolve_associations() click to toggle source
# File lib/haveapi/go_client/resource.rb, line 78
def resolve_associations
  actions.each { |a| a.resolve_associations }
  resources.each { |r| r.resolve_associations }
end
resource_path() click to toggle source

@return [Array<Resource>]

# File lib/haveapi/go_client/resource.rb, line 74
def resource_path
  parent_resources + [self]
end

Protected Instance Methods

full_go_type() click to toggle source
# File lib/haveapi/go_client/resource.rb, line 126
def full_go_type
  names = ['Resource']
  names.concat(parent_resources.map(&:go_name))
  names << go_name
  prefix_camel(names.join(''))
end
prefix_camel(s) click to toggle source
# File lib/haveapi/go_client/resource.rb, line 118
def prefix_camel(s)
  if prefix
    camelize(prefix) + s
  else
    s
  end
end
prefix_underscore(s) click to toggle source
# File lib/haveapi/go_client/resource.rb, line 110
def prefix_underscore(s)
  if prefix
    "#{prefix}_#{s}"
  else
    s
  end
end