Class: DockerSwarm::Base
Direct Known Subclasses
Config, Container, Image, Network, Node, Secret, Service, Swarm, System, Task, Volume
Class Method Summary
collapse
Instance Method Summary
collapse
#inspect
Constructor Details
#initialize(attributes = {}) ⇒ Base
Returns a new instance of Base.
71
72
73
74
|
# File 'lib/docker_swarm/base.rb', line 71
def initialize(attributes = {})
assign_attributes(attributes) if attributes.present?
super()
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/docker_swarm/base.rb', line 131
def method_missing(method_name, *args, &block)
method_str = method_name.to_s
if method_str.end_with?("=")
_define_dynamic_accessor(method_str.chomp("="))
instance_variable_set("@#{method_str.chomp('=')}", args.first)
elsif _valid_attribute_name?(method_str) && instance_variable_defined?("@#{method_str}")
instance_variable_get("@#{method_str}")
elsif !method_str.end_with?("?")
_define_dynamic_accessor(method_str)
nil
else
super
end
end
|
Class Method Details
.all(filters = {}) ⇒ Object
30
31
32
33
34
35
36
|
# File 'lib/docker_swarm/base.rb', line 30
def all(filters = {})
response = _fetch_all(filters)
return [] if response.blank?
data = root_key && response.is_a?(Hash) ? response[root_key] : response
Array(data).map { |item| new(item) }
end
|
.defined_attributes ⇒ Set
Cache of attributes already defined for this class
15
16
17
|
# File 'lib/docker_swarm/base.rb', line 15
def defined_attributes
@defined_attributes ||= Set.new([ :validation_context, :errors, :context_for_validation ])
end
|
.find(id) ⇒ Object
38
39
40
41
42
43
|
# File 'lib/docker_swarm/base.rb', line 38
def find(id)
data = Api.request(action: routes[:show], arguments: { id: id })
new(data)
rescue Errors::NotFound
nil
end
|
.resource_name ⇒ Object
9
10
11
|
# File 'lib/docker_swarm/base.rb', line 9
def resource_name
name.demodulize.downcase.pluralize
end
|
.root_key ⇒ String?
Key in the JSON response that contains the array of items. Override in subclasses if the API returns a wrapped object (e.g., Volumes).
26
27
28
|
# File 'lib/docker_swarm/base.rb', line 26
def root_key
nil
end
|
.routes ⇒ Object
19
20
21
|
# File 'lib/docker_swarm/base.rb', line 19
def routes
Api::ENDPOINTS[resource_name.to_sym]
end
|
.where(filters) ⇒ Object
45
46
47
|
# File 'lib/docker_swarm/base.rb', line 45
def where(filters)
all(filters)
end
|
Instance Method Details
#as_json(options = nil) ⇒ Object
105
106
107
|
# File 'lib/docker_swarm/base.rb', line 105
def as_json(options = nil)
serializable_hash(options)
end
|
#assign_attributes(new_attributes) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/docker_swarm/base.rb', line 76
def assign_attributes(new_attributes)
return if new_attributes.blank?
raise ArgumentError, "assign_attributes expects a Hash, got #{new_attributes.class}" unless new_attributes.is_a?(Hash)
attributes_to_assign = new_attributes.deep_dup.with_indifferent_access
normalized_attributes = {}
attributes_to_assign.each do |key, value|
normalized_key = key.to_s == "Id" ? "ID" : key.to_s
_define_dynamic_accessor(normalized_key)
if normalized_key == "Spec" && respond_to?(:Spec) && self.Spec.is_a?(Hash) && value.is_a?(Hash)
value = self.Spec.deep_merge(value)
end
normalized_attributes[normalized_key] = value
end
super(normalized_attributes)
end
|
#attributes ⇒ Object
97
98
99
|
# File 'lib/docker_swarm/base.rb', line 97
def attributes
instance_values.except("validation_context", "errors", "context_for_validation")
end
|
#id ⇒ Object
121
122
123
|
# File 'lib/docker_swarm/base.rb', line 121
def id
self.ID
end
|
#payload_for_docker ⇒ Object
109
110
111
112
113
114
115
|
# File 'lib/docker_swarm/base.rb', line 109
def payload_for_docker
data = attributes.except("ID", "Id", "Version", "CreatedAt", "UpdatedAt").compact
return data unless data.key?("Spec")
spec = data.delete("Spec").deep_dup
spec.merge!(data)
end
|
#persisted? ⇒ Boolean
117
118
119
|
# File 'lib/docker_swarm/base.rb', line 117
def persisted?
respond_to?(:ID) && self.ID.present?
end
|
#reload ⇒ Object
125
126
127
128
129
|
# File 'lib/docker_swarm/base.rb', line 125
def reload
fresh = self.class.find(id)
assign_attributes(fresh.attributes) if fresh
self
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
147
148
149
150
151
152
153
|
# File 'lib/docker_swarm/base.rb', line 147
def respond_to_missing?(method_name, include_private = false)
method_str = method_name.to_s
method_str.end_with?("=") ||
(_valid_attribute_name?(method_str) && instance_variable_defined?("@#{method_str}")) ||
(!method_str.end_with?("?") && _valid_attribute_name?(method_str)) ||
super
end
|
#serializable_hash(_options = nil) ⇒ Object
101
102
103
|
# File 'lib/docker_swarm/base.rb', line 101
def serializable_hash(_options = nil)
attributes
end
|