module RatPackSwagger::Definition

Public Class Methods

included(mod) click to toggle source
# File lib/rat_pack_swagger.rb, line 38
def self.included mod
  mod.extend DefinitionClass
end

Public Instance Methods

from_h(h) click to toggle source
# File lib/rat_pack_swagger.rb, line 42
def from_h(h)
  properties = self.class.definition[:properties] 
  h.each do |k,v|
    k = k.to_sym
    setter = "#{k}="
    if properties.keys.include?(k)
      # if property type references another class, instantiate it and use hash data to populate it
      if properties[k][:$ref]
        send(setter, properties[k][:$ref].new.from_h(v))
      # if property type is an ARRAY that references another class, instantiate and use hash data to populate them
      elsif properties[k][:type].to_sym == :array && properties[k][:items][:$ref]
        send(setter, v.map{|_| properties[k][:items][:$ref].new.from_h(_) })
      else
        send(setter, v)
      end
    end
  end
  return self
end
to_h(recur = true) click to toggle source
# File lib/rat_pack_swagger.rb, line 61
def to_h(recur = true)
  h = {}
  self.class.definition[:properties].keys.each do |p|
    val = send(p)
    if recur
      if val.is_a?(Array)
        h[p] = val.map{|v| v.is_a?(Definition) ? v.to_h : v}
      elsif val.is_a?(Definition)
        h[p] = val.to_h
      else
        h[p] = val
      end
    else 
      h[p] = val
    end
  end
  return h
end