Class: HaveAPI::GoClient::Resource

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

Constant Summary

Constants included from Utils

Utils::GO_KEYWORDS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#camelize, #go_comment_text, #go_json_tag, #go_module_path, #go_package_name, #go_query_key, #go_string_literal, #safe_file_component

Constructor Details

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

Returns a new instance of Resource.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/haveapi/go_client/resource.rb', line 43

def initialize(parent, name, desc, prefix: nil)
  @parent = parent
  @name = name.to_s
  @prefix = prefix
  @full_name = resource_path.map(&:name).join('_')
  @file_name = resource_path.map { |r| safe_file_component(r.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:



41
42
43
# File 'lib/haveapi/go_client/resource.rb', line 41

def actions
  @actions
end

#file_nameString (readonly)

Safe full name for generated filenames

Returns:

  • (String)


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

def file_name
  @file_name
end

#full_dot_nameString (readonly)

Full name with dots

Returns:

  • (String)


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

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)


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

def go_name
  @go_name
end

#go_typeString (readonly)

Type in Go

Returns:

  • (String)


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

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:



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

def resources
  @resources
end

Instance Method Details

#<=>(other) ⇒ Object



114
115
116
# File 'lib/haveapi/go_client/resource.rb', line 114

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

#api_versionApiVersion

Returns:



61
62
63
64
65
# File 'lib/haveapi/go_client/resource.rb', line 61

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

#generate(gen) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/haveapi/go_client/resource.rb', line 90

def generate(gen)
  ErbTemplate.render_to_if_changed(
    'resource.go',
    {
      package: gen.package,
      resource: self
    },
    File.join(gen.dst, prefix_underscore("resource_#{file_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_#{file_name}_action_#{a.file_name}.go"))
    )
  end
end

#parent_resourcesArray<Resource>

Returns:



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/haveapi/go_client/resource.rb', line 68

def parent_resources
  parents = []
  tmp = parent

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

  parents.reverse
end

#resolve_associationsObject



85
86
87
88
# File 'lib/haveapi/go_client/resource.rb', line 85

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

#resource_pathArray<Resource>

Returns:



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

def resource_path
  parent_resources + [self]
end