Class: Jsapi::JSON::String

Inherits:
Value
  • Object
show all
Defined in:
lib/jsapi/json/string.rb

Overview

Represents a JSON string.

Instance Attribute Summary collapse

Attributes inherited from Value

#schema

Instance Method Summary collapse

Methods inherited from Value

#inspect, #null?

Constructor Details

#initialize(value, schema) ⇒ String

Returns a new instance of String.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/jsapi/json/string.rb', line 9

def initialize(value, schema)
  super(schema)

  @value =
    begin
      case schema.format
      when 'date'
        value.to_date
      when 'date-time'
        value.to_datetime
      when 'duration'
        ActiveSupport::Duration.parse(value)
      else
        value.to_s
      end
    rescue StandardError => e
      @error = e
      value
    end
  @value = schema.convert(@value) unless defined? @error
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'lib/jsapi/json/string.rb', line 7

def value
  @value
end

Instance Method Details

#empty?Boolean

:nodoc:

Returns:



31
32
33
# File 'lib/jsapi/json/string.rb', line 31

def empty? # :nodoc:
  value.blank?
end

#validate(errors) ⇒ Object

:nodoc:



35
36
37
38
39
40
# File 'lib/jsapi/json/string.rb', line 35

def validate(errors) # :nodoc:
  return super unless defined? @error

  errors.add(:base, :invalid)
  false
end