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

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

Instance Method Summary collapse

Instance Method Details

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

Parameters:

  • schema (Symbol|String)

    name

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


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/schema_tools/modules/attributes.rb', line 20

def has_schema_attrs(schema_name, opts={})
  reader          = opts[:reader] || SchemaTools::Reader
  schema_location = opts[:path]   || opts[:schema]
  schema          = reader.read(schema_name, schema_location)
  # remember name on class level
  self.schema_name(schema_name)
  # make getter / setter
  schema[:properties].each do |key, val|
    # getter
    define_method key do
      schema_attrs[key]
    end
    #setter
    unless val[:readonly]
      define_method "#{key}=" do |value|
        #TODO validations?
        schema_attrs[key] = value
      end
    end
  end
end