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, Event, ImageVersion, Insights::Alert, Insights::Event, Insights::Metric, Network::IpAddress, Network::NetworkInterface, Network::NetworkSecurityGroup, Network::VirtualNetwork, Offer, Publisher, Resource, ResourceGroup, ResourceProvider, Role::Assignment, Role::Definition, Sku, Sql::SqlDatabase, Sql::SqlServer, StorageAccount, StorageAccount::Blob, StorageAccount::BlobMetadata, StorageAccount::BlobProperty, StorageAccount::BlobServiceProperty, StorageAccount::BlobServiceStat, StorageAccount::Container, StorageAccount::ContainerProperty, StorageAccount::Table, StorageAccount::TableData, Subscription, Tag, TemplateDeployment, Tenant, Usage, VirtualMachine, VirtualMachineExtension, VirtualMachineImage, VirtualMachineSize
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(json) ⇒ 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/azure/armrest/model/base_model.rb', line 47
def initialize(json)
@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
@json = json.to_json
else
@hash = JSON.parse(json)
@json = json
end
__setobj__(@hash.dup)
end
|
Instance Attribute Details
#resource_group ⇒ Object
66
67
68
|
# File 'lib/azure/armrest/model/base_model.rb', line 66
def resource_group
@resource_group ||= id[/resourceGroups\/(.+?)\//i, 1] rescue nil
end
|
Instance Method Details
#==(other) ⇒ Object
118
119
120
121
|
# File 'lib/azure/armrest/model/base_model.rb', line 118
def ==(other)
return false unless other.kind_of?(BaseModel)
__getobj__ == other.__getobj__
end
|
#[](key) ⇒ Object
Support hash style accessors
129
130
131
|
# File 'lib/azure/armrest/model/base_model.rb', line 129
def [](key)
__getobj__[key]
end
|
#[]=(key, val) ⇒ Object
133
134
135
136
137
138
139
|
# File 'lib/azure/armrest/model/base_model.rb', line 133
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
123
124
125
126
|
# File 'lib/azure/armrest/model/base_model.rb', line 123
def eql?(other)
return false unless other.kind_of?(BaseModel)
__getobj__.eql?(other.__getobj__)
end
|
#inspect ⇒ Object
97
98
99
100
101
102
|
# File 'lib/azure/armrest/model/base_model.rb', line 97
def inspect
Kernel.instance_method(:to_s).bind(self).call.chomp!('>') <<
' ' <<
inspect_method_list.map { |m| "#{m}=#{send(m).inspect}" }.join(', ') <<
'>'
end
|
#pretty_print(q) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/azure/armrest/model/base_model.rb', line 104
def pretty_print(q)
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
72
73
74
|
# File 'lib/azure/armrest/model/base_model.rb', line 72
def to_h
@hash
end
|
#to_hash ⇒ Object
76
77
78
|
# File 'lib/azure/armrest/model/base_model.rb', line 76
def to_hash
@hash
end
|
#to_json ⇒ Object
80
81
82
|
# File 'lib/azure/armrest/model/base_model.rb', line 80
def to_json
@json
end
|
#to_s ⇒ Object
84
85
86
|
# File 'lib/azure/armrest/model/base_model.rb', line 84
def to_s
@json
end
|
#to_str ⇒ Object
88
89
90
|
# File 'lib/azure/armrest/model/base_model.rb', line 88
def to_str
@json
end
|