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



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/json/schematized/basic_wrapper.rb', line 129

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



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/json/schematized/basic_wrapper.rb', line 206

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



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/json/schematized/basic_wrapper.rb', line 157

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



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

def attributes
  self
end

#attributes=(hash) ⇒ Object



181
182
183
184
185
186
187
188
# File 'lib/json/schematized/basic_wrapper.rb', line 181

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



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

def json_schema=(*args); end

#read_attribute(name) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/json/schematized/basic_wrapper.rb', line 190

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



149
150
151
# File 'lib/json/schematized/basic_wrapper.rb', line 149

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

#submodels_scopeObject



153
154
155
# File 'lib/json/schematized/basic_wrapper.rb', line 153

def submodels_scope
  self.class
end