Class: HaveAPI::GoClient::Resource

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/haveapi/go_client/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#camelize

Constructor Details

#initialize(parent, name, desc, prefix: nil) ⇒ Resource

Returns a new instance of Resource.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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 do |k, v|
    Resource.new(self, k, v)
  end.sort!
  @actions = desc[:actions].map do |k, v|
    Action.new(self, k.to_s, v, prefix:)
  end.sort!
end

Instance Attribute Details

#actionsArray<Action> (readonly)

Resource actions

Returns:



37
38
39
# File 'lib/haveapi/go_client/resource.rb', line 37

def actions
  @actions
end

#full_dot_nameString (readonly)

Full name with dots

Returns:

  • (String)


21
22
23
# File 'lib/haveapi/go_client/resource.rb', line 21

def full_dot_name
  @full_dot_name
end

#full_nameString (readonly)

Full name with underscores

Returns:

  • (String)


17
18
19
# File 'lib/haveapi/go_client/resource.rb', line 17

def full_name
  @full_name
end

#go_nameString (readonly)

Name in Go

Returns:

  • (String)


25
26
27
# File 'lib/haveapi/go_client/resource.rb', line 25

def go_name
  @go_name
end

#go_typeString (readonly)

Type in Go

Returns:

  • (String)


29
30
31
# File 'lib/haveapi/go_client/resource.rb', line 29

def go_type
  @go_type
end

#nameString (readonly)

Resource name as returned by the API

Returns:

  • (String)


9
10
11
# File 'lib/haveapi/go_client/resource.rb', line 9

def name
  @name
end

#parentApiServer, Resource (readonly)

Parent resource or API version

Returns:



13
14
15
# File 'lib/haveapi/go_client/resource.rb', line 13

def parent
  @parent
end

#resourcesArray<Resource> (readonly)

Child resources

Returns:



33
34
35
# File 'lib/haveapi/go_client/resource.rb', line 33

def resources
  @resources
end

Instance Method Details

#<=>(other) ⇒ Object



109
110
111
# File 'lib/haveapi/go_client/resource.rb', line 109

def <=>(other)
  go_name <=> other.go_name
end

#api_versionApiVersion

Returns:



56
57
58
59
60
# File 'lib/haveapi/go_client/resource.rb', line 56

def api_version
  tmp = parent
  tmp = tmp.parent until tmp.is_a?(ApiVersion)
  tmp
end

#generate(gen) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/haveapi/go_client/resource.rb', line 85

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_resourcesArray<Resource>

Returns:



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/haveapi/go_client/resource.rb', line 63

def parent_resources
  parents = []
  tmp = parent

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

  parents.reverse
end

#resolve_associationsObject



80
81
82
83
# File 'lib/haveapi/go_client/resource.rb', line 80

def resolve_associations
  actions.each(&:resolve_associations)
  resources.each(&:resolve_associations)
end

#resource_pathArray<Resource>

Returns:



76
77
78
# File 'lib/haveapi/go_client/resource.rb', line 76

def resource_path
  parent_resources + [self]
end