Module: Schema::Model::ClassMethods
- Defined in:
- lib/schema/model.rb
Overview
no-doc
Class Method Summary collapse
Instance Method Summary collapse
- #add_aliases(name, options) ⇒ Object
- #add_attribute_methods(name, options) ⇒ Object
- #attribute(name, type, options = {}) ⇒ Object
- #capture_unknown_attributes=(v) ⇒ Object
- #capture_unknown_attributes? ⇒ Boolean
- #from_hash(data) ⇒ Object
- #schema ⇒ Object
- #schema_config ⇒ Object
- #schema_include(mod) ⇒ Object
- #schema_with_string_keys ⇒ Object
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, ) return unless [:aliases] [:aliases].each do |alias_name| add_value_to_class_method(:schema, alias_name.to_sym => .merge(key: alias_name.to_s, alias_of: name)) alias_method(alias_name, [:getter]) alias_method("#{alias_name}=", [: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, ) 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, = {}) [:aliases] = [[:alias]] if .key?(:alias) = ::Schema::Model.(name, type) .merge( parser: "parse_#{type}" ).merge() add_value_to_class_method(:schema, name => ) add_attribute_methods(name, ) ::Schema::Utils.add_attribute_default_methods(self, ) if .has_key?(:default) add_aliases(name, ) 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
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 |
#schema ⇒ Object
32 33 34 |
# File 'lib/schema/model.rb', line 32 def schema {}.freeze end |
#schema_config ⇒ Object
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 || next unless [:association] const_get([:class_name]).schema_include(mod) end end |
#schema_with_string_keys ⇒ Object
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, | hsh[field_name.to_s] = } hsh.freeze end end |