Module: SchemaTools::Modules::Attributes::ClassMethods

Defined in:
lib/schema_tools/modules/attributes.rb

Instance Method Summary collapse

Instance Method Details

#from_hash(hash, obj = nil) ⇒ Object

Create a new object from a ruby hash (e.g parsed from json string). Auto-detects nesting by checking for a hash key with the same name as the schema_name:

class Contact
  include SchemaTools::Modules::Attributes
  has_schema_attrs :contact
end
c = Contact.from_hash( {'contact'=>{ "id=>"123456", "last_name"=>"Meier" }} )
c.id #=>123456

attributes. e.g during an update

Parameters:

  • json (Hash{String=>Mixed})

    string or hash

  • obj (Object) (defaults to: nil)

    if you want to update an existing objects



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/schema_tools/modules/attributes.rb', line 79

def from_hash(hash, obj=nil)
  # test if hash is nested and shift up
  if hash.length == 1 && hash["#{schema_name}"]
    hash = hash["#{schema_name}"]
  end
  obj ||= new
  hash.each do |key, val|
    next unless obj.respond_to?(key)
    conv_val = nil
    # set values to raw schema attributes, even if there are no setters
    # assuming this objects comes from a remote site
    field_props = self.schema['properties']["#{key}"]
    field_type = field_props['type']
    unless val.nil?
      case field_type
      when 'string'
        conv_val = process_string_type(field_props['format'], val)
      when 'integer'
        conv_val = val.to_i
      when 'object'
        conv_val = process_object_type(key, val)
      when 'array'
        conv_val = process_array_type(key, val)
      else
        conv_val = val
      end
    end

    obj.schema_attrs["#{key}"] = conv_val
  end
  obj
end

#from_json(json) ⇒ Object

Create a new object from a json string or a ruby hash (already created from json string). Auto-detects nesting by checking for a hash key with the same name as the schema_name:

class Contact
  include SchemaTools::Modules::Attributes
  has_schema_attrs :contact
end
c = Contact.from_json('{ "id": "123456",  "last_name": "Meier" }')
c.id #=>123456
c = Contact.from_json( {'contact'=>{ "id=>"123456", "last_name"=>"Meier" }} )

Parameters:

  • json (String|Hash{String=>Mixed})

    string or hash



60
61
62
63
# File 'lib/schema_tools/modules/attributes.rb', line 60

def from_json(json)
  hash = JSON.parse(json)
  from_hash(hash)
end

#has_schema_attrs(schema_name, opts = {}) ⇒ Object

Parameters:

  • schema (Symbol|String)

    name

  • opts (Hash<Symbol|String>) (defaults to: {})


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/schema_tools/modules/attributes.rb', line 33

def has_schema_attrs(schema_name, opts={})
  reader          = opts[:reader] || SchemaTools::Reader
  schema_location = opts[:path]   || opts[:schema]
  # remember schema + name on class level

  self.schema= reader.read(schema_name, schema_location)
  self.schema_name(schema_name)
  # make getter / setter methods
  self.schema[:properties].each do |key, prop|
    define_method(key) { schema_attrs[key] }
    define_method("#{key}=") { |value| schema_attrs[key] = value } unless prop['readOnly']
  end
end

#schemaObject



116
117
118
# File 'lib/schema_tools/modules/attributes.rb', line 116

def schema
  @schema
end

#schema=(schema_hash) ⇒ Object

Parameters:

  • schema_hash (Hash)


113
114
115
# File 'lib/schema_tools/modules/attributes.rb', line 113

def schema= schema_hash
  @schema = schema_hash
end