class JsonSpec::Matchers::HaveJsonType
Public Class Methods
Source
# File lib/json_spec/matchers/have_json_type.rb, line 7 def initialize(type) @classes = type_to_classes(type) @path = nil end
Public Instance Methods
Source
# File lib/json_spec/matchers/have_json_type.rb, line 17 def at_path(path) @path = path self end
Source
# File lib/json_spec/matchers/have_json_type.rb, line 32 def description message_with_path(%(have JSON type "#{@classes.join(", ")}")) end
Source
# File lib/json_spec/matchers/have_json_type.rb, line 22 def failure_message message_with_path("Expected JSON value type to be #{@classes.join(", ")}, got #{@ruby.class}") end
Also aliased as: failure_message_for_should
Source
# File lib/json_spec/matchers/have_json_type.rb, line 27 def failure_message_when_negated message_with_path("Expected JSON value type to not be #{@classes.join(", ")}, got #{@ruby.class}") end
Also aliased as: failure_message_for_should_not
Source
# File lib/json_spec/matchers/have_json_type.rb, line 12 def matches?(json) @ruby = parse_json(json, @path) @classes.any? { |c| c === @ruby } end
Private Instance Methods
Source
# File lib/json_spec/matchers/have_json_type.rb, line 37 def type_to_classes(type) case type when Class then [type] when Array then type.map { |t| type_to_classes(t) }.flatten else case type.to_s.downcase when "boolean" then [TrueClass, FalseClass] when "object" then [Hash] when "nil", "null" then [NilClass] else [Module.const_get(type.to_s.capitalize)] end end end