class JsonWrapper
encoding: utf-8
encoding: utf-8
Constants
- VERSION
Attributes
Internal value @return [Hash, Array, String, Number, Nil] internal value
Public Class Methods
Create a JsonWrapper
@param value [Hash, Array, String, Numeric, Nil] parse result from JSON.parse @return [JsonWrapper] instance
# File lib/json_wrapper.rb, line 15 def initialize(value = nil) @value = value end
Public Instance Methods
@see get
# File lib/json_wrapper.rb, line 137 def [](key) get(key) end
Get the value if is a Array @return [Array, Nil] value if it's a Array
# File lib/json_wrapper.rb, line 57 def array value if array? end
Force convert to Array @return [Array] Array format of value, {} if not capable
# File lib/json_wrapper.rb, line 81 def array! array || [] end
If value is a Array @return [True, False] if value is an Array
# File lib/json_wrapper.rb, line 27 def array? value.kind_of? Array end
Return count
if value is a String, Array or Hash @return [Number] count, 0 if not capable
# File lib/json_wrapper.rb, line 148 def count return self.value.count if array? or hash? or string? 0 end
each method for Enumerable
# File lib/json_wrapper.rb, line 154 def each(&block) array!.each do |v| block.call(JsonWrapper.new(v)) end end
Force convert to Fixnum @return [Fixnum] Fixnum format of value
# File lib/json_wrapper.rb, line 109 def fixnum! number!.to_i end
Force convert to Float @return [Float] Float format of value
# File lib/json_wrapper.rb, line 103 def float! number!.to_f end
Try to get value from Hash or Array @param key [String, Symbol, Fixnum] key @return [JsonWrapper] JsonWrapper
of the value
# File lib/json_wrapper.rb, line 116 def get(key) return JsonWrapper.new(value[key]) if array? and key.kind_of?(Fixnum) return JsonWrapper.new(value[key.to_i]) if array? and key.kind_of?(String) return JsonWrapper.new(value[key.to_s.to_i]) if array? and key.kind_of?(Symbol) return JsonWrapper.new(value[key]) if hash? and key.kind_of?(String) return JsonWrapper.new(value[key.to_s]) if hash? and key.kind_of?(Symbol) JsonWrapper.new end
Get the value if is a Hash @return [Hash, nil] value if it's a Hash
# File lib/json_wrapper.rb, line 51 def hash value if hash? end
Force convert to Hash @return [Hash] Hash format of value, {} if not capable
# File lib/json_wrapper.rb, line 75 def hash! hash || {} end
If value is a Hash @return [True, False] if value is a Hash
# File lib/json_wrapper.rb, line 21 def hash? value.kind_of? Hash end
@see get
# File lib/json_wrapper.rb, line 142 def method_missing(*args) get args.first end
If value is a Nil @return [True, False] if value is a Nil
# File lib/json_wrapper.rb, line 45 def null? value.nil? end
Get the value if value is a number @return [Fixnum, Float, Nil] value if it's a Numeric, typically Fixnum or Float
# File lib/json_wrapper.rb, line 69 def number value if number? end
Force convert to number @return [Numeric] Number format of value, 0 if not capable
# File lib/json_wrapper.rb, line 95 def number! return value if number? return value.to_f if string? 0 end
If value is a Number @return [True,False] if value is an Number
# File lib/json_wrapper.rb, line 39 def number? value.kind_of? Numeric end
Try to get value by chained call of keys @param key [Array] array of keys, can be String or Fixnum @return [JsonWrapper] JsonWrapper
of the value
# File lib/json_wrapper.rb, line 128 def resolve_chain(*keys) target = self keys.flatten.each do |key| target = target[key] end target end
Get the value if value is string @return [String, Nil] value if it's a String
# File lib/json_wrapper.rb, line 63 def string value if string? end
Force convert to String @return [String] String format of value, “” if not capable
# File lib/json_wrapper.rb, line 87 def string! return value if string? return value.to_s if number? "" end
If value is String @return [True,False] if value is a String
# File lib/json_wrapper.rb, line 33 def string? value.kind_of? String end