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
- #not_set? ⇒ Boolean
- #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 24 |
# 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}", default_method: "#{name}_default" } 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.send(:include, Schema::Parsers::Common) base.extend ClassMethods end |
Instance Method Details
#as_json(opts = {}) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/schema/model.rb', line 104 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 |
#not_set? ⇒ Boolean
122 123 124 125 126 |
# File 'lib/schema/model.rb', line 122 def not_set? self.class.schema.values.all? do || !instance_variable_defined?([:instance_variable]) end end |
#parsing_errors ⇒ Object
118 119 120 |
# File 'lib/schema/model.rb', line 118 def parsing_errors @parsing_errors ||= Errors.new end |
#to_hash ⇒ Object Also known as: to_h
113 114 115 |
# File 'lib/schema/model.rb', line 113 def to_hash as_json(include_nils: true) end |
#update_attributes(data) ⇒ Object
97 98 99 100 101 102 |
# File 'lib/schema/model.rb', line 97 def update_attributes(data) schema = get_schema(data) update_model_attributes(schema, data) update_associations(schema, data) self end |