Class: JsonWrapper
- Inherits:
-
Object
- Object
- JsonWrapper
- Defined in:
- lib/json_wrapper.rb,
lib/json_wrapper/version.rb
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#value ⇒ Hash, ...
readonly
Internal value.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#array ⇒ Array, Nil
Get the value if is a Array.
-
#array! ⇒ Array
Force convert to Array.
-
#array? ⇒ True, False
If value is a Array.
-
#fixnum! ⇒ Fixnum
Force convert to Fixnum.
-
#float! ⇒ Float
Force convert to Float.
-
#get(key) ⇒ JsonWrapper
Try to get value from Hash or Array.
-
#hash ⇒ Hash?
Get the value if is a Hash.
-
#hash! ⇒ Hash
Force convert to Hash.
-
#hash? ⇒ True, False
If value is a Hash.
-
#initialize(value = nil) ⇒ JsonWrapper
constructor
Create a JsonWrapper.
- #method_missing(*args) ⇒ Object
-
#null? ⇒ True, False
If value is a Nil.
-
#number ⇒ Fixnum, ...
Get the value if value is a number.
-
#number! ⇒ Numeric
Force convert to number.
-
#number? ⇒ True, False
If value is a Number.
-
#string ⇒ String, Nil
Get the value if value is string.
-
#string! ⇒ String
Force convert to String.
-
#string? ⇒ True, False
If value is String.
Constructor Details
#initialize(value = nil) ⇒ JsonWrapper
Create a JsonWrapper
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
126 127 128 |
# File 'lib/json_wrapper.rb', line 126 def method_missing(*args) get args.first end |
Instance Attribute Details
#value ⇒ Hash, ... (readonly)
Internal value
7 8 9 |
# File 'lib/json_wrapper.rb', line 7 def value @value end |
Instance Method Details
#[](key) ⇒ Object
121 122 123 |
# File 'lib/json_wrapper.rb', line 121 def [](key) get(key) end |
#array ⇒ Array, Nil
Get the value if is a Array
54 55 56 |
# File 'lib/json_wrapper.rb', line 54 def array @value if array? end |
#array! ⇒ Array
Force convert to Array
78 79 80 |
# File 'lib/json_wrapper.rb', line 78 def array! array || [] end |
#array? ⇒ True, False
If value is a Array
24 25 26 |
# File 'lib/json_wrapper.rb', line 24 def array? @value.kind_of? Array end |
#fixnum! ⇒ Fixnum
Force convert to Fixnum
106 107 108 |
# File 'lib/json_wrapper.rb', line 106 def fixnum! number!.to_i end |
#float! ⇒ Float
Force convert to Float
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
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 |
#hash ⇒ Hash?
Get the value if is a Hash
48 49 50 |
# File 'lib/json_wrapper.rb', line 48 def hash @value if hash? end |
#hash! ⇒ Hash
Force convert to Hash
72 73 74 |
# File 'lib/json_wrapper.rb', line 72 def hash! hash || {} end |
#hash? ⇒ 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
42 43 44 |
# File 'lib/json_wrapper.rb', line 42 def null? @value.nil? end |
#number ⇒ Fixnum, ...
Get the value if value is a number
66 67 68 |
# File 'lib/json_wrapper.rb', line 66 def number @value if number? end |
#number! ⇒ Numeric
Force convert to number
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
36 37 38 |
# File 'lib/json_wrapper.rb', line 36 def number? @value.kind_of? Numeric end |
#string ⇒ String, Nil
Get the value if value is string
60 61 62 |
# File 'lib/json_wrapper.rb', line 60 def string @value if string? end |
#string! ⇒ String
Force convert to String
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
30 31 32 |
# File 'lib/json_wrapper.rb', line 30 def string? @value.kind_of? String end |