Class: HaveAPI::GoClient::Resource
- Inherits:
-
Object
- Object
- HaveAPI::GoClient::Resource
- Includes:
- Utils
- Defined in:
- lib/haveapi/go_client/resource.rb
Instance Attribute Summary collapse
-
#actions ⇒ Array<Action>
readonly
Resource actions.
-
#full_dot_name ⇒ String
readonly
Full name with dots.
-
#full_name ⇒ String
readonly
Full name with underscores.
-
#go_name ⇒ String
readonly
Name in Go.
-
#go_type ⇒ String
readonly
Type in Go.
-
#name ⇒ String
readonly
Resource name as returned by the API.
-
#parent ⇒ ApiServer, Resource
readonly
Parent resource or API version.
-
#resources ⇒ Array<Resource>
readonly
Child resources.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #api_version ⇒ ApiVersion
- #generate(gen) ⇒ Object
-
#initialize(parent, name, desc, prefix: nil) ⇒ Resource
constructor
A new instance of Resource.
- #parent_resources ⇒ Array<Resource>
- #resolve_associations ⇒ Object
- #resource_path ⇒ Array<Resource>
Methods included from Utils
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
#actions ⇒ Array<Action> (readonly)
Resource actions
37 38 39 |
# File 'lib/haveapi/go_client/resource.rb', line 37 def actions @actions end |
#full_dot_name ⇒ String (readonly)
Full name with dots
21 22 23 |
# File 'lib/haveapi/go_client/resource.rb', line 21 def full_dot_name @full_dot_name end |
#full_name ⇒ String (readonly)
Full name with underscores
17 18 19 |
# File 'lib/haveapi/go_client/resource.rb', line 17 def full_name @full_name end |
#go_name ⇒ String (readonly)
Name in Go
25 26 27 |
# File 'lib/haveapi/go_client/resource.rb', line 25 def go_name @go_name end |
#go_type ⇒ String (readonly)
Type in Go
29 30 31 |
# File 'lib/haveapi/go_client/resource.rb', line 29 def go_type @go_type end |
#name ⇒ String (readonly)
Resource name as returned by the API
9 10 11 |
# File 'lib/haveapi/go_client/resource.rb', line 9 def name @name end |
#parent ⇒ ApiServer, Resource (readonly)
Parent resource or API version
13 14 15 |
# File 'lib/haveapi/go_client/resource.rb', line 13 def parent @parent end |
#resources ⇒ Array<Resource> (readonly)
Child resources
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_version ⇒ ApiVersion
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_resources ⇒ Array<Resource>
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_associations ⇒ Object
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_path ⇒ Array<Resource>
76 77 78 |
# File 'lib/haveapi/go_client/resource.rb', line 76 def resource_path parent_resources + [self] end |