Class: JsonType

Inherits:
Object
  • Object
show all
Includes:
DeepEquality, Inspectable
Defined in:
lib/json_patterns.rb

Constant Summary

Constants included from Inspectable

Inspectable::INSPECTING_KEY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DeepEquality

#==

Methods included from Inspectable

#inspect

Constructor Details

#initialize(type) ⇒ JsonType

Returns a new instance of JsonType.



127
128
129
# File 'lib/json_patterns.rb', line 127

def initialize(type)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



125
126
127
# File 'lib/json_patterns.rb', line 125

def type
  @type
end

Class Method Details

.new_from_class(klass) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/json_patterns.rb', line 158

def self.new_from_class(klass)
  case klass.name
  when 'Hash'
    JsonType.new :object
  when 'Array'
    JsonType.new :array
  when 'String'
    JsonType.new :string
  when 'Integer'
    JsonType.new :integer
  when 'Float'
    JsonType.new :float
  when 'Numeric'
    JsonType.new :float
  when 'Boolean'
    JsonType.new :boolean
  when 'NilClass'
    JsonType.new :null
  else
    raise "class has no JsonType: #{klass}"
  end
end

.new_from_value(value) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/json_patterns.rb', line 135

def self.new_from_value(value)
  case value
  when Hash
    JsonType.new :object
  when Array
    JsonType.new :array
  when String
    JsonType.new :string
  when Integer
    JsonType.new :integer
  when Float
    JsonType.new :float
  when TrueClass
    JsonType.new :boolean
  when FalseClass
    JsonType.new :boolean
  when NilClass
    JsonType.new :null
  else
    raise "value has no JsonType: #{value.inspect}"
  end
end

Instance Method Details

#===(value) ⇒ Object



181
182
183
184
# File 'lib/json_patterns.rb', line 181

def ===(value)
  value_type = JsonType.new_from_value(value).type
  value_type == @type or (value_type == 'number' and (@type == :integer or @type == :float))
end

#to_sObject



131
132
133
# File 'lib/json_patterns.rb', line 131

def to_s
  @type.to_s
end