Class: SimpleJSONSchema::Scope
- Inherits:
-
Object
- Object
- SimpleJSONSchema::Scope
- Extended by:
- Concerns::HashAccessor
- Defined in:
- lib/simple_json_schema/scope.rb
Constant Summary collapse
- CLASS_TYPE_TRANSLATE =
{ Array => 'array', Float => 'number', Hash => 'object', Integer => 'integer', Numeric => 'number', String => 'string' }.freeze
Instance Method Summary collapse
- #[](key) ⇒ Object
- #around_hooks ⇒ Object
- #cache ⇒ Object
- #error(type, details = nil) ⇒ Object
- #id ⇒ Object
-
#initialize(**args) ⇒ Scope
constructor
A new instance of Scope.
- #key?(key) ⇒ Boolean
- #loaded_ids ⇒ Object
- #merge(data_paths: self.data_paths, schema_paths: self.schema_paths, type: nil, parent_uri: nil) ⇒ Object
- #no_hooks ⇒ Object
- #path_to(data_path: nil, schema_path: nil, type: nil) ⇒ Object
- #replace_data(new_data) ⇒ Object
- #segment ⇒ Object
- #segment=(new_segment) ⇒ Object
- #segment? ⇒ Boolean
- #value ⇒ Object
- #value=(new_value) ⇒ Object
Methods included from Concerns::HashAccessor
Constructor Details
#initialize(**args) ⇒ Scope
Returns a new instance of Scope.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/simple_json_schema/scope.rb', line 18 def initialize(**args) scope.merge!(args) self.data_paths ||= [] self.schema_paths ||= [] self.errors ||= [] self.ids ||= {} self. ||= {} self.type ||= evaluate_type end |
Instance Method Details
#[](key) ⇒ Object
102 103 104 105 106 |
# File 'lib/simple_json_schema/scope.rb', line 102 def [](key) return unless segment.is_a?(Hash) segment[key] end |
#around_hooks ⇒ Object
116 117 118 119 120 |
# File 'lib/simple_json_schema/scope.rb', line 116 def around_hooks [:before_property_validation]&.call(self) yield [:after_property_validation]&.call(self) end |
#cache ⇒ Object
112 113 114 |
# File 'lib/simple_json_schema/scope.rb', line 112 def cache [:cache] ||= Cache.new end |
#error(type, details = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/simple_json_schema/scope.rb', line 52 def error(type, details = nil) error = { type: type, segment: segment, value: value, data_pointer: "/#{data_paths.join('/')}", schema_pointer: "/#{schema_paths.join('/')}" } error[:details] = details if details errors.push(error) end |
#id ⇒ Object
98 99 100 |
# File 'lib/simple_json_schema/scope.rb', line 98 def id self[:$id] end |
#key?(key) ⇒ Boolean
108 109 110 |
# File 'lib/simple_json_schema/scope.rb', line 108 def key?(key) segment.is_a?(Hash) && segment.key?(key) end |
#loaded_ids ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/simple_json_schema/scope.rb', line 122 def loaded_ids if ids.empty? resolve_ids(ids, schema) ids[:$loaded] = true end ids end |
#merge(data_paths: self.data_paths, schema_paths: self.schema_paths, type: nil, parent_uri: nil) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/simple_json_schema/scope.rb', line 38 def merge(data_paths: self.data_paths, schema_paths: self.schema_paths, type: nil, parent_uri: nil) self.class.new(**scope.merge(data_paths: data_paths, schema_paths: schema_paths, type: type, parent_uri: parent_uri || URIExtender.join_uri(self.parent_uri, id))) end |
#no_hooks ⇒ Object
66 67 68 69 |
# File 'lib/simple_json_schema/scope.rb', line 66 def no_hooks self. = .except(:before_property_validation, :after_property_validation) self end |
#path_to(data_path: nil, schema_path: nil, type: nil) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/simple_json_schema/scope.rb', line 29 def path_to(data_path: nil, schema_path: nil, type: nil) return self if data_path.nil? && schema_path.nil? && type.nil? new_data_paths = data_paths + [data_path].flatten.compact new_schema_paths = schema_paths + [schema_path].flatten.compact merge(data_paths: new_data_paths, schema_paths: new_schema_paths, type: type) end |
#replace_data(new_data) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/simple_json_schema/scope.rb', line 45 def replace_data(new_data) self.data = new_data self.data_paths = [] self.type = evaluate_type self end |
#segment ⇒ Object
81 82 83 |
# File 'lib/simple_json_schema/scope.rb', line 81 def segment dig(schema, schema_paths) end |
#segment=(new_segment) ⇒ Object
85 86 87 |
# File 'lib/simple_json_schema/scope.rb', line 85 def segment=(new_segment) replace(schema, schema_paths, new_segment) { self.schema = new_segment } end |
#segment? ⇒ Boolean
89 90 91 92 93 94 95 96 |
# File 'lib/simple_json_schema/scope.rb', line 89 def segment? if segment == false error('schema') return false end !(segment == true || segment.nil?) end |
#value ⇒ Object
71 72 73 |
# File 'lib/simple_json_schema/scope.rb', line 71 def value dig(data, data_paths) end |
#value=(new_value) ⇒ Object
75 76 77 78 79 |
# File 'lib/simple_json_schema/scope.rb', line 75 def value=(new_value) return if errors.any? # only convert value until be invalid. replace(data, data_paths, new_value) { self.data = new_value } end |