Class: JsonWrapper

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

Constant Summary collapse

VERSION =
"0.1.0"

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



12
13
14
# File 'lib/json_wrapper.rb', line 12

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:



126
127
128
# File 'lib/json_wrapper.rb', line 126

def method_missing(*args)
  get args.first
end

Instance Attribute Details

#valueHash, ... (readonly)

Internal value

Returns:

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

    internal value



7
8
9
# File 'lib/json_wrapper.rb', line 7

def value
  @value
end

Instance Method Details

#[](key) ⇒ Object

See Also:



121
122
123
# File 'lib/json_wrapper.rb', line 121

def [](key)
  get(key)
end

#arrayArray, Nil

Get the value if is a Array

Returns:

  • (Array, Nil)

    value if it’s a Array



54
55
56
# File 'lib/json_wrapper.rb', line 54

def array
  @value if array?
end

#array!Array

Force convert to Array

Returns:

  • (Array)

    Array format of value, {} if not capable



78
79
80
# File 'lib/json_wrapper.rb', line 78

def array!
  array || []
end

#array?True, False

If value is a Array

Returns:

  • (True, False)

    if value is an Array



24
25
26
# File 'lib/json_wrapper.rb', line 24

def array?
  @value.kind_of? Array
end

#fixnum!Fixnum

Force convert to Fixnum

Returns:

  • (Fixnum)

    Fixnum format of value



106
107
108
# File 'lib/json_wrapper.rb', line 106

def fixnum!
  number!.to_i
end

#float!Float

Force convert to Float

Returns:

  • (Float)

    Float format of value



100
101
102
# File 'lib/json_wrapper.rb', line 100

def float!
  number!.to_f
end

#get(key) ⇒ JsonWrapper

Try to get value from Hash or Array

Parameters:

  • key (String, Symbol, Fixnum)

    key

Returns:



113
114
115
116
117
118
# File 'lib/json_wrapper.rb', line 113

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



48
49
50
# File 'lib/json_wrapper.rb', line 48

def hash
  @value if hash?
end

#hash!Hash

Force convert to Hash

Returns:

  • (Hash)

    Hash format of value, {} if not capable



72
73
74
# File 'lib/json_wrapper.rb', line 72

def hash!
  hash || {}
end

#hash?True, False

If value is a Hash

Returns:

  • (True, False)

    if value is a Hash



18
19
20
# File 'lib/json_wrapper.rb', line 18

def hash?
  @value.kind_of? Hash
end

#null?True, False

If value is a Nil

Returns:

  • (True, False)

    if value is a Nil



42
43
44
# File 'lib/json_wrapper.rb', line 42

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



66
67
68
# File 'lib/json_wrapper.rb', line 66

def number
  @value if number?
end

#number!Numeric

Force convert to number

Returns:

  • (Numeric)

    Number format of value, 0 if not capable



92
93
94
95
96
# File 'lib/json_wrapper.rb', line 92

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



36
37
38
# File 'lib/json_wrapper.rb', line 36

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



60
61
62
# File 'lib/json_wrapper.rb', line 60

def string
  @value if string?
end

#string!String

Force convert to String

Returns:

  • (String)

    String format of value, “” if not capable



84
85
86
87
88
# File 'lib/json_wrapper.rb', line 84

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



30
31
32
# File 'lib/json_wrapper.rb', line 30

def string?
  @value.kind_of? String
end