Class: ApiValidator::Assertion

Inherits:
Object
  • Object
show all
Defined in:
lib/api-validator/assertion.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, value, options = {}) ⇒ Assertion

Returns a new instance of Assertion.



6
7
8
# File 'lib/api-validator/assertion.rb', line 6

def initialize(path, value, options = {})
  @path, @value, @type, @format = path, value, options.delete(:type), options.delete(:format)
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



5
6
7
# File 'lib/api-validator/assertion.rb', line 5

def format
  @format
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/api-validator/assertion.rb', line 5

def path
  @path
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/api-validator/assertion.rb', line 5

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/api-validator/assertion.rb', line 5

def value
  @value
end

Instance Method Details

#stringified_valueObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/api-validator/assertion.rb', line 19

def stringified_value
  if Regexp === value
    regex = value.to_s.
      sub(%r|\(\?-mix:(.*)\)|) { $1 }.
      gsub("\\A", "^").
      gsub("\\Z", "$")
    "/#{regex}/"
  else
    value
  end
end

#to_hash(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/api-validator/assertion.rb', line 10

def to_hash(options = {})
  _h = { :op => "test", :path => path, :value => stringified_value }
  _h.delete(:value) if type && value.nil?
  _h[:type] = "regexp" if Regexp === value
  _h[:type] = type if type
  _h[:format] = format if format
  _h
end