Class: Azure::Armrest::BaseModel
- Inherits:
-
Object
- Object
- Azure::Armrest::BaseModel
show all
- Defined in:
- lib/azure/armrest/model/base_model.rb
Overview
Base class for JSON wrapper classes. Each Service class should have a corresponding class that wraps the JSON it collects, and each of them should subclass this base class.
Direct Known Subclasses
AvailabilitySet, Azure::Armrest::Billing::Usage, Container, Endpoint, Event, ExtensionType, HDInsight::HDInsightApplication, HDInsight::HDInsightCluster, ImageVersion, Insights::Alert, Insights::Diagnostic, Insights::Event, Insights::Metric, Insights::MetricDefinition, KeyVault, Location, Network::IpAddress, Network::LoadBalancer, Network::NetworkInterface, Network::NetworkSecurityGroup, Network::RouteTable, Network::VirtualNetwork, Offer, Publisher, Resource, ResourceGroup, ResourceProvider, ResponseBody, ResponseHeaders, Role::Assignment, Role::Definition, Sku, Sql::MariadbDatabase, Sql::MariadbServer, Sql::MysqlDatabase, Sql::MysqlServer, Sql::PostgresqlDatabase, Sql::PostgresqlServer, Sql::SqlDatabase, Sql::SqlServer, Storage::Disk, Storage::Image, Storage::Snapshot, StorageAccount, StorageAccount::Blob, StorageAccount::BlobMetadata, StorageAccount::BlobProperty, StorageAccount::BlobServiceProperty, StorageAccount::BlobServiceStat, StorageAccount::Container, StorageAccount::ContainerProperty, StorageAccount::ShareDirectory, StorageAccount::ShareFile, StorageAccount::Table, StorageAccount::TableData, Subscription, Tag, TemplateDeployment, Tenant, VirtualMachine, VirtualMachineExtension, VirtualMachineImage, VirtualMachineSize
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(json, skip_accessors_definition = false) ⇒ BaseModel
Constructs and returns a new JSON wrapper class. Pass in a plain JSON string and it will automatically give you accessor methods that make it behave like a typical Ruby object. You may also pass in a hash.
Example:
class Person < Azure::ArmRest::BaseModel; end
json_string = '{"firstname":"jeff", "lastname":"durand",
"address": { "street":"22 charlotte rd", "zipcode":"01013"}
}'
person = Person.new(json_string)
person.firstname person.address.zipcode
person.to_json
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/azure/armrest/model/base_model.rb', line 106
def initialize(json, skip_accessors_definition = false)
@child_excl_list = self.class.send(:excl_list).map do |e|
e.index('#') ? e[e.index('#') + 1..-1] : ''
end
if json.kind_of?(Hash)
@hash = json
else
@hash = JSON.parse(json)
@json = json
end
@hashobj = @hash.dup
__setobj__ unless skip_accessors_definition
end
|
Instance Attribute Details
#resource_group ⇒ Object
125
126
127
128
129
130
131
|
# File 'lib/azure/armrest/model/base_model.rb', line 125
def resource_group
@resource_group ||= begin
id_from_hash[/resourcegroups\/(.*?[^\/]+)?/i, 1]
rescue
nil
end
end
|
#response_code ⇒ Object
Returns the value of attribute response_code.
82
83
84
|
# File 'lib/azure/armrest/model/base_model.rb', line 82
def response_code
@response_code
end
|
Returns the value of attribute response_headers.
81
82
83
|
# File 'lib/azure/armrest/model/base_model.rb', line 81
def
@response_headers
end
|
#subscription_id ⇒ Object
133
134
135
136
137
138
139
|
# File 'lib/azure/armrest/model/base_model.rb', line 133
def subscription_id
@subscription_id ||= begin
id_from_hash[/subscriptions\/(.*?[^\/]+)?/i, 1]
rescue
nil
end
end
|
Instance Method Details
#==(other) ⇒ Object
185
186
187
188
|
# File 'lib/azure/armrest/model/base_model.rb', line 185
def ==(other)
return false unless other.kind_of?(BaseModel)
__getobj__ == other.__getobj__
end
|
#[](key) ⇒ Object
Support hash style accessors
196
197
198
|
# File 'lib/azure/armrest/model/base_model.rb', line 196
def [](key)
__getobj__[key]
end
|
#[]=(key, val) ⇒ Object
200
201
202
203
204
205
206
|
# File 'lib/azure/armrest/model/base_model.rb', line 200
def []=(key, val)
key_exists = __getobj__.include?(key)
__getobj__[key] = val
return if key_exists
add_accessor_methods(key.to_s.underscore, key)
end
|
#eql?(other) ⇒ Boolean
190
191
192
193
|
# File 'lib/azure/armrest/model/base_model.rb', line 190
def eql?(other)
return false unless other.kind_of?(BaseModel)
__getobj__.eql?(other.__getobj__)
end
|
#pretty_print(q) ⇒ Object
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/azure/armrest/model/base_model.rb', line 167
def pretty_print(q)
inspect_method_list = methods(false).reject { |m| m.to_s.end_with?('=') }
q.object_address_group(self) {
q.seplist(inspect_method_list, lambda { q.text ',' }) {|v|
q.breakable
q.text v.to_s
q.text '='
q.group(1) {
q.breakable ''
q.pp(send(v))
}
}
}
end
|
#to_h ⇒ Object
144
145
146
|
# File 'lib/azure/armrest/model/base_model.rb', line 144
def to_h
@hash
end
|
#to_hash ⇒ Object
148
149
150
|
# File 'lib/azure/armrest/model/base_model.rb', line 148
def to_hash
@hash
end
|
#to_json(_options = nil) ⇒ Object
Return the original JSON for the model object. The options
argument is for interface compatibility only.
155
156
157
|
# File 'lib/azure/armrest/model/base_model.rb', line 155
def to_json(_options = nil)
@json ||= @hash ? @hash.to_json : ""
end
|
#to_s ⇒ Object
159
160
161
|
# File 'lib/azure/armrest/model/base_model.rb', line 159
def to_s
@json ||= @hash ? @hash.to_json : ""
end
|
#to_str ⇒ Object
163
164
165
|
# File 'lib/azure/armrest/model/base_model.rb', line 163
def to_str
@json ||= @hash ? @hash.to_json : ""
end
|