Class: Azure::Armrest::BaseModel

Inherits:
Object
  • Object
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.

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"}
}'

# Or whatever your subclass happens to be.
person = Person.new(json_string)

# The JSON properties are now available as methods.
person.firstname        # => 'jeff'
person.address.zipcode  # => '01013'

# Or you can get back the original JSON if necessary.
person.to_json # => Returns original 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)
  # Find the exclusion list for the model of next level (@embed_model)
  # '#' is the separator between levels. Remove attributes
  # before the first separator.
  @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_groupObject



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_codeObject

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

#response_headersObject

Returns the value of attribute response_headers.



81
82
83
# File 'lib/azure/armrest/model/base_model.rb', line 81

def response_headers
  @response_headers
end

#subscription_idObject



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

Returns:

  • (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_hObject



144
145
146
# File 'lib/azure/armrest/model/base_model.rb', line 144

def to_h
  @hash
end

#to_hashObject



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_sObject



159
160
161
# File 'lib/azure/armrest/model/base_model.rb', line 159

def to_s
  @json ||= @hash ? @hash.to_json : ""
end

#to_strObject



163
164
165
# File 'lib/azure/armrest/model/base_model.rb', line 163

def to_str
  @json ||= @hash ? @hash.to_json : ""
end