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
100 101 102 103 104 105 106 107 |
# File 'lib/schema/model.rb', line 100 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
114 115 116 |
# File 'lib/schema/model.rb', line 114 def parsing_errors @parsing_errors ||= Errors.new end |
#to_hash ⇒ Object Also known as: to_h
109 110 111 |
# File 'lib/schema/model.rb', line 109 def to_hash as_json(include_nils: true) end |
#update_attributes(data) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/schema/model.rb', line 93 def update_attributes(data) schema = get_schema(data) update_model_attributes(schema, data) update_associations(schema, data) self end |