Module: Schema::Model::ClassMethods

Defined in:
lib/schema/model.rb

Overview

no-doc

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.include(base) ⇒ Object



28
29
30
# File 'lib/schema/model.rb', line 28

def self.include(base)
  base.capture_unknown_attributes = true
end

Instance Method Details

#add_aliases(name, options) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/schema/model.rb', line 106

def add_aliases(name, options)
  return unless options[:aliases]

  options[:aliases].each do |alias_name|
    add_value_to_class_method(:schema, alias_name.to_sym => options.merge(key: alias_name.to_s, alias_of: name))
    alias_method(alias_name, options[:getter])
    alias_method("#{alias_name}=", options[:setter])
  end
end

#add_attribute_methods(name, options) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/schema/model.rb', line 92

def add_attribute_methods(name, options)
  class_eval(
"  def \#{options[:getter]}\n    \#{options[:instance_variable]}\n  end\n\n  def \#{options[:setter]}(v)\n    \#{options[:instance_variable]} = \#{options[:parser]}(\#{name.inspect}, parsing_errors, v)\n  end\n", __FILE__, __LINE__ + 1
  )
end

#attribute(name, type, options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/schema/model.rb', line 62

def attribute(name, type, options = {})
  options[:aliases] = [options[:alias]] if options.key?(:alias)

  options = ::Schema::Model.default_attribute_options(name, type)
                           .merge(
                             parser: "parse_#{type}"
                           ).merge(options)

  add_value_to_class_method(:schema, name => options)
  add_attribute_methods(name, options)
  ::Schema::Utils.add_attribute_default_methods(self, options) if options.has_key?(:default)
  add_aliases(name, options)
end

#capture_unknown_attributes=(v) ⇒ Object



52
53
54
55
56
# File 'lib/schema/model.rb', line 52

def capture_unknown_attributes=(v)
  config = schema_config.dup
  config[:capture_unknown_attributes] = v
  redefine_class_method(:schema_config, config.freeze)
end

#capture_unknown_attributes?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/schema/model.rb', line 58

def capture_unknown_attributes?
  schema_config[:capture_unknown_attributes]
end

#from_hash(data) ⇒ Object



76
77
78
# File 'lib/schema/model.rb', line 76

def from_hash(data)
  new.update_attributes(data)
end

#schemaObject



32
33
34
# File 'lib/schema/model.rb', line 32

def schema
  {}.freeze
end

#schema_configObject



45
46
47
48
49
50
# File 'lib/schema/model.rb', line 45

def schema_config
  {
    schema_includes: [],
    capture_unknown_attributes: true
  }.freeze
end

#schema_include(mod) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/schema/model.rb', line 80

def schema_include(mod)
  config = schema_config.dup
  config[:schema_includes] = config[:schema_includes] + [mod]
  redefine_class_method(:schema_config, config.freeze)
  include mod
  schema.values.each do |field_options|
    next unless field_options[:association]

    const_get(field_options[:class_name]).schema_include(mod)
  end
end

#schema_with_string_keysObject



36
37
38
39
40
41
42
43
# File 'lib/schema/model.rb', line 36

def schema_with_string_keys
  @schema_with_string_keys ||=
    begin
      hsh = {}
      schema.each { |field_name, field_options| hsh[field_name.to_s] = field_options }
      hsh.freeze
    end
end