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


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/azure/armrest/model/base_model.rb', line 105

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



124
125
126
127
128
129
130
# File 'lib/azure/armrest/model/base_model.rb', line 124

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.



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

def response_code
  @response_code
end

#response_headersObject

Returns the value of attribute response_headers.



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

def response_headers
  @response_headers
end

#subscription_idObject



132
133
134
135
136
137
138
# File 'lib/azure/armrest/model/base_model.rb', line 132

def subscription_id
  @subscription_id ||= begin
                         id_from_hash[/subscriptions\/(.*?[^\/]+)?/i, 1]
                       rescue
                         nil
                       end
end

Instance Method Details

#==(other) ⇒ Object



184
185
186
187
# File 'lib/azure/armrest/model/base_model.rb', line 184

def ==(other)
  return false unless other.kind_of?(BaseModel)
  __getobj__ == other.__getobj__
end

#[](key) ⇒ Object

Support hash style accessors



195
196
197
# File 'lib/azure/armrest/model/base_model.rb', line 195

def [](key)
  __getobj__[key]
end

#[]=(key, val) ⇒ Object



199
200
201
202
203
204
205
# File 'lib/azure/armrest/model/base_model.rb', line 199

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)


189
190
191
192
# File 'lib/azure/armrest/model/base_model.rb', line 189

def eql?(other)
  return false unless other.kind_of?(BaseModel)
  __getobj__.eql?(other.__getobj__)
end

#pretty_print(q) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/azure/armrest/model/base_model.rb', line 166

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



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

def to_h
  @hash
end

#to_hashObject



147
148
149
# File 'lib/azure/armrest/model/base_model.rb', line 147

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.



154
155
156
# File 'lib/azure/armrest/model/base_model.rb', line 154

def to_json(_options = nil)
  @json ||= @hash ? @hash.to_json : ""
end

#to_sObject



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

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

#to_strObject



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

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