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
84 85 86 87 88 89 90 91 92 |
# File 'lib/schema/model.rb', line 84 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
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/schema/model.rb', line 70 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
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/schema/model.rb', line 46 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, ) add_aliases(name, ) end |
#from_hash(data) ⇒ Object
59 60 61 |
# File 'lib/schema/model.rb', line 59 def from_hash(data) new.update_attributes(data) end |
#schema ⇒ Object
27 28 29 |
# File 'lib/schema/model.rb', line 27 def schema {}.freeze end |
#schema_config ⇒ Object
40 41 42 43 44 |
# File 'lib/schema/model.rb', line 40 def schema_config { schema_includes: [] }.freeze end |
#schema_include(mod) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/schema/model.rb', line 63 def schema_include(mod) config = schema_config.dup config[:schema_includes] = config[:schema_includes] + [mod] redefine_class_method(:schema_config, config.freeze) include mod end |
#schema_with_string_keys ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/schema/model.rb', line 31 def schema_with_string_keys @schema_with_string_keys ||= begin hsh = {} schema.each { |field_name, | hsh[field_name.to_s] = } hsh.freeze end end |