Module: JSON::Schematized::BasicWrapper::SchematizedHash

Defined in:
lib/json/schematized/basic_wrapper.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/json/schematized/basic_wrapper.rb', line 122

def method_missing(name, *args)
  key = name.to_s
  if key =~ /=\z/
    key = $`.to_sym
    if json_schema[:properties][key]
      self[key] = args.first
    else
      super
    end
  else
    read_attribute key
  end
end

Class Method Details

.ensure_structure!(json, schema) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/json/schematized/basic_wrapper.rb', line 199

def self.ensure_structure!(json, schema)
  meta = schema[:properties]
  meta.each_pair do |key, schema|
    if !json.has_key?(key.to_s) && schema[:required]
      json[key.to_s] = case schema[:type]
        when "object" then {}
        when "array" then []
        else nil
      end
    end
  end
end

Instance Method Details

#[]=(key, value) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/json/schematized/basic_wrapper.rb', line 150

def []=(key, value)
  if meta = json_schema[:properties][key.to_sym]
    case meta[:type]
    when "array"
      collection = BasicWrapper.build_collection(submodels_scope, key, meta[:items])
      new_value = collection.class.new
      new_value.coerce_members_to(collection.first, meta[:items])
      new_value.mass_assign!(value) if value.is_a?(Array)
      value = new_value
    when "object"
      model_class = BasicWrapper.build_model(submodels_scope, key, meta)
      new_value = model_class.new
      new_value.json_schema = meta
      new_value.attributes = value if value.is_a?(Hash)
      value = new_value
    else
      attribute = submodels_scope.attribute_set[key.to_sym]
      attribute = attribute.writer if attribute.respond_to?(:writer)
      value = attribute.coerce(value)
    end
  end
  super(key.to_s, value)
end

#attributesObject



136
137
138
# File 'lib/json/schematized/basic_wrapper.rb', line 136

def attributes
  self
end

#attributes=(hash) ⇒ Object



174
175
176
177
178
179
180
181
# File 'lib/json/schematized/basic_wrapper.rb', line 174

def attributes=(hash)
  return unless hash.is_a?(Hash)
  hash.each_pair do |key, value|
    self[key] = value
  end
ensure
  SchematizedHash.ensure_structure!(self, json_schema)
end

#json_schema=(*args) ⇒ Object



140
# File 'lib/json/schematized/basic_wrapper.rb', line 140

def json_schema=(*args); end

#read_attribute(name) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/json/schematized/basic_wrapper.rb', line 183

def read_attribute(name)
  name = name.to_s
  value = self[name]
  if !has_key?(name) && (meta = json_schema[:properties][name.to_sym])
    case meta[:type]
    when "array"
      self[name] = []
      value = self[name]
    when "object"
      self[name] = {}
      value = self[name]
    end
  end
  value
end

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/json/schematized/basic_wrapper.rb', line 142

def respond_to?(method_name)
  json_schema[:properties].has_key?(method_name.to_sym) || super
end

#submodels_scopeObject



146
147
148
# File 'lib/json/schematized/basic_wrapper.rb', line 146

def submodels_scope
  self.class
end