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
Instance Method Details
#add_aliases(name, options) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/schema/model.rb', line 75 def add_aliases(name, ) [: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
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/schema/model.rb', line 61 def add_attribute_methods(name, ) class_eval( <<~STR, __FILE__, __LINE__ + 1 def #{[:getter]} #{[:instance_variable]} end def #{[:setter]}(v) #{[:instance_variable]} = #{[:parser]}(#{name.inspect}, parsing_errors, v) end STR ) end |
#attribute(name, type, options = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/schema/model.rb', line 37 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
50 51 52 |
# File 'lib/schema/model.rb', line 50 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
31 32 33 34 35 |
# File 'lib/schema/model.rb', line 31 def schema_config { schema_includes: [] }.freeze end |
#schema_include(mod) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/schema/model.rb', line 54 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 |