Class: Jsapi::JSON::Value
- Inherits:
-
Object
- Object
- Jsapi::JSON::Value
- Defined in:
- lib/jsapi/json/value.rb
Overview
Represents a JSON value.
Subclasses of Value must provide a value
method returning the outside representation of the JSON value.
Instance Attribute Summary collapse
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Used by #validate to test whether or not it is empty.
-
#initialize(schema) ⇒ Value
constructor
A new instance of Value.
-
#inspect ⇒ Object
:nodoc:.
-
#null? ⇒ Boolean
Used by #validate to test whether or not it is null.
-
#validate(errors) ⇒ Object
Validates it against #schema.
Constructor Details
#initialize(schema) ⇒ Value
Returns a new instance of Value.
12 13 14 |
# File 'lib/jsapi/json/value.rb', line 12 def initialize(schema) @schema = schema end |
Instance Attribute Details
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
10 11 12 |
# File 'lib/jsapi/json/value.rb', line 10 def schema @schema end |
Instance Method Details
#empty? ⇒ Boolean
Used by #validate to test whether or not it is empty. Returns false by default.
18 19 20 |
# File 'lib/jsapi/json/value.rb', line 18 def empty? false end |
#inspect ⇒ Object
:nodoc:
22 23 24 |
# File 'lib/jsapi/json/value.rb', line 22 def inspect # :nodoc: "#<#{self.class} #{value.inspect}>" end |
#null? ⇒ Boolean
Used by #validate to test whether or not it is null. Returns false by default.
28 29 30 |
# File 'lib/jsapi/json/value.rb', line 28 def null? false end |
#validate(errors) ⇒ Object
Validates it against #schema. Returns true if it is valid, false otherwise. Detected errors are added to errors
.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/jsapi/json/value.rb', line 34 def validate(errors) unless schema.existence.reach?(self) errors.add(:base, :blank) return false end return true if null? schema.validations.each_value.map do |validation| validation.validate(value, errors) end.all? end |