Class: Jsapi::JSON::Value

Inherits:
Object
  • Object
show all
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.

Direct Known Subclasses

Array, Boolean, Integer, Null, Number, Object, String

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#schemaObject (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.

Returns:



18
19
20
# File 'lib/jsapi/json/value.rb', line 18

def empty?
  false
end

#inspectObject

: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.

Returns:



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