Class: JsonSchematize::Generator
- Inherits:
-
Object
- Object
- JsonSchematize::Generator
- Extended by:
- Introspect::ClassMethods
- Includes:
- Introspect::InstanceMethods
- Defined in:
- lib/json_schematize/generator.rb
Direct Known Subclasses
Constant Summary collapse
- EMPTY_VALIDATOR =
->(_transformed_value, _raw_value) { true }
- PROTECTED_METHODS =
[:assign_values!, :convenience_methods, :validate_required!, :validate_optional!, :validate_value]
Instance Attribute Summary collapse
-
#__raw_params ⇒ Object
readonly
Returns the value of attribute __raw_params.
-
#raise_on_error ⇒ Object
readonly
Returns the value of attribute raise_on_error.
-
#values_assigned ⇒ Object
readonly
Returns the value of attribute values_assigned.
Class Method Summary collapse
- .add_field(name:, type: nil, types: nil, dig_type: nil, dig: nil, validator: nil, required: nil, converter: nil, array_of_types: nil, empty_value: nil, hash_of_types: nil, hash_of_types_key: "name") ⇒ Object
-
.convenience_methods(field:) ⇒ Object
def self.update_block(&block) @skip_individual_updates = true yield(cloned) @skip_individual_updates = false end.
- .fields ⇒ Object
- .optional_fields ⇒ Object
- .required_fields ⇒ Object
- .schema_default(option:, value:) ⇒ Object
- .schema_defaults ⇒ Object
Instance Method Summary collapse
-
#initialize(stringified_params = nil, raise_on_error: true, **params) ⇒ Generator
constructor
stringified_params allows for params with stringed keys.
Methods included from Introspect::ClassMethods
Methods included from Introspect::InstanceMethods
#deep_inspect, #inspect, #to_h
Constructor Details
#initialize(stringified_params = nil, raise_on_error: true, **params) ⇒ Generator
stringified_params allows for params with stringed keys
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/json_schematize/generator.rb', line 98 def initialize(stringified_params = nil, raise_on_error: true, **params) @values_assigned = false @__params = stringified_params.nil? ? params : stringified_params @__raw_params = @__params @raise_on_error = raise_on_error if @__params validate_required! validate_optional! assign_values! @values_assigned = true end end |
Instance Attribute Details
#__raw_params ⇒ Object (readonly)
Returns the value of attribute __raw_params.
95 96 97 |
# File 'lib/json_schematize/generator.rb', line 95 def __raw_params @__raw_params end |
#raise_on_error ⇒ Object (readonly)
Returns the value of attribute raise_on_error.
95 96 97 |
# File 'lib/json_schematize/generator.rb', line 95 def raise_on_error @raise_on_error end |
#values_assigned ⇒ Object (readonly)
Returns the value of attribute values_assigned.
95 96 97 |
# File 'lib/json_schematize/generator.rb', line 95 def values_assigned @values_assigned end |
Class Method Details
.add_field(name:, type: nil, types: nil, dig_type: nil, dig: nil, validator: nil, required: nil, converter: nil, array_of_types: nil, empty_value: nil, hash_of_types: nil, hash_of_types_key: "name") ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/json_schematize/generator.rb', line 15 def self.add_field(name:, type: nil, types: nil, dig_type: nil, dig: nil, validator: nil, required: nil, converter: nil, array_of_types: nil, empty_value: nil, hash_of_types: nil, hash_of_types_key: "name") require "json_schematize/empty_value" field_params = { converter: converter || schema_defaults[:converter], dig: dig || schema_defaults[:dig], dig_type: dig_type || schema_defaults[:dig_type], name: name, required: (required.nil? ? schema_defaults.fetch(:required, true) : required), type: type || schema_defaults[:type], types: types || schema_defaults.fetch(:types, []), empty_value: empty_value || schema_defaults.fetch(:empty_value, JsonSchematize::EmptyValue), validator: validator || schema_defaults.fetch(:validator, EMPTY_VALIDATOR), array_of_types: (array_of_types.nil? ? schema_defaults.fetch(:array_of_types, false) : array_of_types), } field = JsonSchematize::Field.new(**field_params) field.setup! if field_params[:required] == true required_fields << field else optional_fields << field end convenience_methods(field: field) end |
.convenience_methods(field:) ⇒ Object
def self.update_block(&block)
@skip_individual_updates = true
yield(cloned)
@skip_individual_updates = false
end
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/json_schematize/generator.rb', line 72 def self.convenience_methods(field:) unless self.instance_methods.include?(:"#{field.name}=") define_method(:"#{field.name}=") do |value| validate_params = { field: field, raw_value: value, transformed_value: value, raise_on_error: raise_on_error, } return false unless validate_value(**validate_params) instance_variable_set(:"@#{field.name}", value) __update_cache__! return true end end unless self.instance_methods.include?(:"#{field.name}") define_method(:"#{field.name}") do instance_variable_get("@#{field.name}".to_sym) end end end |
.fields ⇒ Object
54 55 56 |
# File 'lib/json_schematize/generator.rb', line 54 def self.fields required_fields + optional_fields end |
.optional_fields ⇒ Object
62 63 64 |
# File 'lib/json_schematize/generator.rb', line 62 def self.optional_fields @optional_fields ||= [] end |
.required_fields ⇒ Object
58 59 60 |
# File 'lib/json_schematize/generator.rb', line 58 def self.required_fields @required_fields ||= [] end |
.schema_default(option:, value:) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/json_schematize/generator.rb', line 42 def self.schema_default(option:, value:) if fields.length > 0 ::Kernel.warn("Default [#{option}] set after fields #{fields.map(&:name)} created. #{option} default will behave inconsistently") end schema_defaults[option.to_sym] = value end |
.schema_defaults ⇒ Object
50 51 52 |
# File 'lib/json_schematize/generator.rb', line 50 def self.schema_defaults @schema_defaults ||= {} end |