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) ⇒ 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


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)
  # 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
    @json = json.to_json
  else
    @hash = JSON.parse(json)
    @json = json
  end

  __setobj__(@hash.dup)
end

Instance Attribute Details

#resource_groupObject



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

Returns:

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

#inspectObject



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_hObject



72
73
74
# File 'lib/azure/armrest/model/base_model.rb', line 72

def to_h
  @hash
end

#to_hashObject



76
77
78
# File 'lib/azure/armrest/model/base_model.rb', line 76

def to_hash
  @hash
end

#to_jsonObject



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

def to_json
  @json
end

#to_sObject



84
85
86
# File 'lib/azure/armrest/model/base_model.rb', line 84

def to_s
  @json
end

#to_strObject



88
89
90
# File 'lib/azure/armrest/model/base_model.rb', line 88

def to_str
  @json
end