json_wrapper 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/json_wrapper/version.rb +1 -1
- data/lib/json_wrapper.rb +33 -16
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 28dd99f95fe890b74342619c48b11462ef23203f
|
4
|
+
data.tar.gz: 97689c5402df83cc1ca53f13faabbf9937761d94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 394045c245b02e1db78826a23d116e4dca53eed2a9771940eb500190eba783694bae727dc4c2cc8092817d6bc119d91bd541920ec70d0ad5da21ef514c37043e
|
7
|
+
data.tar.gz: b9d7923e0b587a96f6c9796eb2ac6df72eb9dec5c81c3a42e641adc7d6e8b5dea2cd68b3b12b06fa35bc52ca5f1c019c0ec3083b2f672aabd03784ad7df95acb
|
data/lib/json_wrapper/version.rb
CHANGED
data/lib/json_wrapper.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
#encoding: utf-8
|
2
2
|
|
3
3
|
class JsonWrapper
|
4
|
+
include Enumerable
|
5
|
+
|
4
6
|
autoload :VERSION, 'json_wrapper/version'
|
7
|
+
|
5
8
|
# Internal value
|
6
9
|
# @return [Hash, Array, String, Number, Nil] internal value
|
7
10
|
attr_reader :value
|
@@ -16,55 +19,55 @@ class JsonWrapper
|
|
16
19
|
# If value is a Hash
|
17
20
|
# @return [True, False] if value is a Hash
|
18
21
|
def hash?
|
19
|
-
|
22
|
+
value.kind_of? Hash
|
20
23
|
end
|
21
24
|
|
22
25
|
# If value is a Array
|
23
26
|
# @return [True, False] if value is an Array
|
24
27
|
def array?
|
25
|
-
|
28
|
+
value.kind_of? Array
|
26
29
|
end
|
27
30
|
|
28
31
|
# If value is String
|
29
32
|
# @return [True,False] if value is a String
|
30
33
|
def string?
|
31
|
-
|
34
|
+
value.kind_of? String
|
32
35
|
end
|
33
36
|
|
34
37
|
# If value is a Number
|
35
38
|
# @return [True,False] if value is an Number
|
36
39
|
def number?
|
37
|
-
|
40
|
+
value.kind_of? Numeric
|
38
41
|
end
|
39
42
|
|
40
43
|
# If value is a Nil
|
41
44
|
# @return [True, False] if value is a Nil
|
42
45
|
def null?
|
43
|
-
|
46
|
+
value.nil?
|
44
47
|
end
|
45
48
|
|
46
49
|
# Get the value if is a Hash
|
47
50
|
# @return [Hash, nil] value if it's a Hash
|
48
51
|
def hash
|
49
|
-
|
52
|
+
value if hash?
|
50
53
|
end
|
51
54
|
|
52
55
|
# Get the value if is a Array
|
53
56
|
# @return [Array, Nil] value if it's a Array
|
54
57
|
def array
|
55
|
-
|
58
|
+
value if array?
|
56
59
|
end
|
57
60
|
|
58
61
|
# Get the value if value is string
|
59
62
|
# @return [String, Nil] value if it's a String
|
60
63
|
def string
|
61
|
-
|
64
|
+
value if string?
|
62
65
|
end
|
63
66
|
|
64
67
|
# Get the value if value is a number
|
65
68
|
# @return [Fixnum, Float, Nil] value if it's a Numeric, typically Fixnum or Float
|
66
69
|
def number
|
67
|
-
|
70
|
+
value if number?
|
68
71
|
end
|
69
72
|
|
70
73
|
# Force convert to Hash
|
@@ -82,16 +85,16 @@ class JsonWrapper
|
|
82
85
|
# Force convert to String
|
83
86
|
# @return [String] String format of value, "" if not capable
|
84
87
|
def string!
|
85
|
-
return
|
86
|
-
return
|
88
|
+
return value if string?
|
89
|
+
return value.to_s if number?
|
87
90
|
""
|
88
91
|
end
|
89
92
|
|
90
93
|
# Force convert to number
|
91
94
|
# @return [Numeric] Number format of value, 0 if not capable
|
92
95
|
def number!
|
93
|
-
return
|
94
|
-
return
|
96
|
+
return value if number?
|
97
|
+
return value.to_f if string?
|
95
98
|
0
|
96
99
|
end
|
97
100
|
|
@@ -111,9 +114,9 @@ class JsonWrapper
|
|
111
114
|
# @param key [String, Symbol, Fixnum] key
|
112
115
|
# @return [JsonWrapper] JsonWrapper of the value
|
113
116
|
def get(key)
|
114
|
-
return JsonWrapper.new(
|
115
|
-
return JsonWrapper.new(
|
116
|
-
return JsonWrapper.new(
|
117
|
+
return JsonWrapper.new(value[key]) if array? and key.kind_of?(Fixnum)
|
118
|
+
return JsonWrapper.new(value[key]) if hash? and key.kind_of?(String)
|
119
|
+
return JsonWrapper.new(value[key.to_s]) if hash? and key.kind_of?(Symbol)
|
117
120
|
JsonWrapper.new
|
118
121
|
end
|
119
122
|
|
@@ -127,4 +130,18 @@ class JsonWrapper
|
|
127
130
|
get args.first
|
128
131
|
end
|
129
132
|
|
133
|
+
# Return #count if value is a String, Array or Hash
|
134
|
+
# @return [Number] count, 0 if not capable
|
135
|
+
def count
|
136
|
+
return self.value.count if array? or hash? or string?
|
137
|
+
0
|
138
|
+
end
|
139
|
+
|
140
|
+
# each method for Enumerable
|
141
|
+
def each(&block)
|
142
|
+
array!.each do |v|
|
143
|
+
block.call(JsonWrapper.new(v))
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
130
147
|
end
|