Module: Schema::Model::ClassMethods
- Defined in:
- lib/schema/model.rb
Overview
no-doc
Instance Method Summary collapse
- #add_aliases(name, options) ⇒ Object
- #add_attribute_methods(name, options) ⇒ Object
- #attribute(name, type, options = {}) ⇒ Object
- #from_hash(data) ⇒ Object
- #schema ⇒ Object
- #schema_config ⇒ Object
- #schema_include(mod) ⇒ Object
- #schema_with_string_keys ⇒ Object
Instance Method Details
#add_aliases(name, options) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/schema/model.rb', line 91 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
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/schema/model.rb', line 77 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
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/schema/model.rb', line 47 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 |
#from_hash(data) ⇒ Object
61 62 63 |
# File 'lib/schema/model.rb', line 61 def from_hash(data) new.update_attributes(data) end |
#schema ⇒ Object
28 29 30 |
# File 'lib/schema/model.rb', line 28 def schema {}.freeze end |
#schema_config ⇒ Object
41 42 43 44 45 |
# File 'lib/schema/model.rb', line 41 def schema_config { schema_includes: [] }.freeze end |
#schema_include(mod) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/schema/model.rb', line 65 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
32 33 34 35 36 37 38 39 |
# File 'lib/schema/model.rb', line 32 def schema_with_string_keys @schema_with_string_keys ||= begin hsh = {} schema.each { |field_name, | hsh[field_name.to_s] = } hsh.freeze end end |