Class: JsonWrapper

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/json_wrapper.rb,
lib/json_wrapper/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ JsonWrapper

Create a JsonWrapper

Parameters:

  • value (Hash, Array, String, Numeric, Nil) (defaults to: nil)

    parse result from JSON.parse



15
16
17
# File 'lib/json_wrapper.rb', line 15

def initialize(value = nil)
  @value = value
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object

See Also:



129
130
131
# File 'lib/json_wrapper.rb', line 129

def method_missing(*args)
  get args.first
end

Instance Attribute Details

#valueHash, ... (readonly)

Internal value

Returns:

  • (Hash, Array, String, Number, Nil)

    internal value



10
11
12
# File 'lib/json_wrapper.rb', line 10

def value
  @value
end

Instance Method Details

#[](key) ⇒ Object

See Also:



124
125
126
# File 'lib/json_wrapper.rb', line 124

def [](key)
  get(key)
end

#arrayArray, Nil

Get the value if is a Array

Returns:

  • (Array, Nil)

    value if it’s a Array



57
58
59
# File 'lib/json_wrapper.rb', line 57

def array
  value if array?
end

#array!Array

Force convert to Array

Returns:

  • (Array)

    Array format of value, {} if not capable



81
82
83
# File 'lib/json_wrapper.rb', line 81

def array!
  array || []
end

#array?True, False

If value is a Array

Returns:

  • (True, False)

    if value is an Array



27
28
29
# File 'lib/json_wrapper.rb', line 27

def array?
  value.kind_of? Array
end

#countNumber

Return #count if value is a String, Array or Hash

Returns:

  • (Number)

    count, 0 if not capable



135
136
137
138
# File 'lib/json_wrapper.rb', line 135

def count
  return self.value.count if array? or hash? or string?
  0
end

#each(&block) ⇒ Object

each method for Enumerable



141
142
143
144
145
# File 'lib/json_wrapper.rb', line 141

def each(&block)
  array!.each do |v|
    block.call(JsonWrapper.new(v))
  end
end

#fixnum!Fixnum

Force convert to Fixnum

Returns:

  • (Fixnum)

    Fixnum format of value



109
110
111
# File 'lib/json_wrapper.rb', line 109

def fixnum!
  number!.to_i
end

#float!Float

Force convert to Float

Returns:

  • (Float)

    Float format of value



103
104
105
# File 'lib/json_wrapper.rb', line 103

def float!
  number!.to_f
end

#get(key) ⇒ JsonWrapper

Try to get value from Hash or Array

Parameters:

  • key (String, Symbol, Fixnum)

    key

Returns:



116
117
118
119
120
121
# 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])       if hash? and key.kind_of?(String)
  return JsonWrapper.new(value[key.to_s])  if hash? and key.kind_of?(Symbol)
  JsonWrapper.new
end

#hashHash?

Get the value if is a Hash

Returns:

  • (Hash, nil)

    value if it’s a Hash



51
52
53
# File 'lib/json_wrapper.rb', line 51

def hash
  value if hash?
end

#hash!Hash

Force convert to Hash

Returns:

  • (Hash)

    Hash format of value, {} if not capable



75
76
77
# File 'lib/json_wrapper.rb', line 75

def hash!
  hash || {}
end

#hash?True, False

If value is a Hash

Returns:

  • (True, False)

    if value is a Hash



21
22
23
# File 'lib/json_wrapper.rb', line 21

def hash?
  value.kind_of? Hash
end

#null?True, False

If value is a Nil

Returns:

  • (True, False)

    if value is a Nil



45
46
47
# File 'lib/json_wrapper.rb', line 45

def null?
  value.nil?
end

#numberFixnum, ...

Get the value if value is a number

Returns:

  • (Fixnum, Float, Nil)

    value if it’s a Numeric, typically Fixnum or Float



69
70
71
# File 'lib/json_wrapper.rb', line 69

def number
  value if number?
end

#number!Numeric

Force convert to number

Returns:

  • (Numeric)

    Number format of value, 0 if not capable



95
96
97
98
99
# File 'lib/json_wrapper.rb', line 95

def number!
  return value if number?
  return value.to_f if string?
  0
end

#number?True, False

If value is a Number

Returns:

  • (True, False)

    if value is an Number



39
40
41
# File 'lib/json_wrapper.rb', line 39

def number?
  value.kind_of? Numeric
end

#stringString, Nil

Get the value if value is string

Returns:

  • (String, Nil)

    value if it’s a String



63
64
65
# File 'lib/json_wrapper.rb', line 63

def string
  value if string?
end

#string!String

Force convert to String

Returns:

  • (String)

    String format of value, “” if not capable



87
88
89
90
91
# File 'lib/json_wrapper.rb', line 87

def string!
  return value if string?
  return value.to_s if number?
  ""
end

#string?True, False

If value is String

Returns:

  • (True, False)

    if value is a String



33
34
35
# File 'lib/json_wrapper.rb', line 33

def string?
  value.kind_of? String
end