Class: JsonWrapper
- Inherits:
-
Object
- Object
- JsonWrapper
- Includes:
- Enumerable
- Defined in:
- lib/json_wrapper.rb,
lib/json_wrapper/version.rb
Constant Summary collapse
- VERSION =
"0.1.1"
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.
-
#count ⇒ Number
Return #count if value is a String, Array or Hash.
-
#each(&block) ⇒ Object
each method for Enumerable.
-
#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
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
129 130 131 |
# File 'lib/json_wrapper.rb', line 129 def method_missing(*args) get args.first end |
Instance Attribute Details
#value ⇒ Hash, ... (readonly)
Internal value
10 11 12 |
# File 'lib/json_wrapper.rb', line 10 def value @value end |
Instance Method Details
#[](key) ⇒ Object
124 125 126 |
# File 'lib/json_wrapper.rb', line 124 def [](key) get(key) end |
#array ⇒ Array, Nil
Get the value if is a Array
57 58 59 |
# File 'lib/json_wrapper.rb', line 57 def array value if array? end |
#array! ⇒ Array
Force convert to Array
81 82 83 |
# File 'lib/json_wrapper.rb', line 81 def array! array || [] end |
#array? ⇒ True, False
If value is a Array
27 28 29 |
# File 'lib/json_wrapper.rb', line 27 def array? value.kind_of? Array end |
#count ⇒ Number
Return #count if value is a String, Array or Hash
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
109 110 111 |
# File 'lib/json_wrapper.rb', line 109 def fixnum! number!.to_i end |
#float! ⇒ Float
Force convert to Float
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
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 |
#hash ⇒ Hash?
Get the value if is a Hash
51 52 53 |
# File 'lib/json_wrapper.rb', line 51 def hash value if hash? end |
#hash! ⇒ Hash
Force convert to Hash
75 76 77 |
# File 'lib/json_wrapper.rb', line 75 def hash! hash || {} end |
#hash? ⇒ 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
45 46 47 |
# File 'lib/json_wrapper.rb', line 45 def null? value.nil? end |
#number ⇒ Fixnum, ...
Get the value if value is a number
69 70 71 |
# File 'lib/json_wrapper.rb', line 69 def number value if number? end |
#number! ⇒ Numeric
Force convert to number
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
39 40 41 |
# File 'lib/json_wrapper.rb', line 39 def number? value.kind_of? Numeric end |
#string ⇒ String, Nil
Get the value if value is string
63 64 65 |
# File 'lib/json_wrapper.rb', line 63 def string value if string? end |
#string! ⇒ String
Force convert to String
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
33 34 35 |
# File 'lib/json_wrapper.rb', line 33 def string? value.kind_of? String end |