class JsonWrapper

encoding: utf-8

encoding: utf-8

Constants

VERSION

Attributes

value[R]

Internal value @return [Hash, Array, String, Number, Nil] internal value

Public Class Methods

new(value = nil) click to toggle source

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

[](key) click to toggle source

@see get

# File lib/json_wrapper.rb, line 137
def [](key)
  get(key)
end
array() click to toggle source

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
array!() click to toggle source

Force convert to Array @return [Array] Array format of value, {} if not capable

# File lib/json_wrapper.rb, line 81
def array!
  array || []
end
array?() click to toggle source

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
count() click to toggle source

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(&block) click to toggle source

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
fixnum!() click to toggle source

Force convert to Fixnum @return [Fixnum] Fixnum format of value

# File lib/json_wrapper.rb, line 109
def fixnum!
  number!.to_i
end
float!() click to toggle source

Force convert to Float @return [Float] Float format of value

# File lib/json_wrapper.rb, line 103
def float!
  number!.to_f
end
get(key) click to toggle source

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
hash() click to toggle source

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
hash!() click to toggle source

Force convert to Hash @return [Hash] Hash format of value, {} if not capable

# File lib/json_wrapper.rb, line 75
def hash!
  hash || {}
end
hash?() click to toggle source

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
method_missing(*args) click to toggle source

@see get

# File lib/json_wrapper.rb, line 142
def method_missing(*args)
  get args.first
end
null?() click to toggle source

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
number() click to toggle source

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
number!() click to toggle source

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
number?() click to toggle source

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
resolve_chain(*keys) click to toggle source

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
string() click to toggle source

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
string!() click to toggle source

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
string?() click to toggle source

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