Module: Schema::Model
- Defined in:
- lib/schema/model.rb
Overview
Schema::Model adds schema building methods to a class
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #as_json(opts = {}) ⇒ Object
- #parsing_errors ⇒ Object
- #to_hash ⇒ Object (also: #to_h)
- #update_attributes(data) ⇒ Object
Class Method Details
.default_attribute_options(name, type) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/schema/model.rb', line 14 def self.(name, type) { key: name.to_s.freeze, name: name, type: type, getter: name.to_s.freeze, setter: "#{name}=", instance_variable: "@#{name}" } end |
.included(base) ⇒ Object
8 9 10 11 12 |
# File 'lib/schema/model.rb', line 8 def self.included(base) base.extend InheritanceHelper::Methods base.include Schema::Parsers::Common base.extend ClassMethods end |
Instance Method Details
#as_json(opts = {}) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/schema/model.rb', line 97 def as_json(opts = {}) self.class.schema.each_with_object({}) do |(field_name, ), memo| unless [:alias_of] value = public_send([:getter]) memo[field_name] = value if !value.nil? || opts[:include_nils] end end end |
#parsing_errors ⇒ Object
111 112 113 |
# File 'lib/schema/model.rb', line 111 def parsing_errors @parsing_errors ||= ::Schema::Errors.new end |
#to_hash ⇒ Object Also known as: to_h
106 107 108 |
# File 'lib/schema/model.rb', line 106 def to_hash as_json(include_nils: true) end |
#update_attributes(data) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/schema/model.rb', line 84 def update_attributes(data) self.class.schema.each do |field_name, | next if !data.key?([:key]) && !data.key?(field_name) public_send( [:setter], data[[:key]] || data[field_name.to_sym] ) end self end |