vfeskov_edu_gem 0.1.11 → 0.1.12
Sign up to get free protection for your applications and to get access to all the features.
data/lib/vfeskov_edu_gem.rb
CHANGED
@@ -1,6 +1,59 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
1
|
+
require 'vfeskov_edu_gem/object_extension'
|
2
|
+
require 'vfeskov_edu_gem/version'
|
3
3
|
|
4
4
|
module VfeskovEduGem
|
5
|
+
def self.extend_to_json(object)
|
5
6
|
|
7
|
+
def object.to_be_serialized(*fields)
|
8
|
+
@to_be_serialized = []
|
9
|
+
fields.each do |field|
|
10
|
+
@to_be_serialized << field.to_s.gsub(/@/,'')
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def object.should_variable_be_serialized?(variable_symbol)
|
15
|
+
variable_name = variable_symbol.to_s.gsub(/@/,'')
|
16
|
+
if defined? @to_be_serialized
|
17
|
+
serialize = false
|
18
|
+
if @to_be_serialized.length > 0 && variable_name != 'to_be_serialized'
|
19
|
+
@to_be_serialized.each do |to_be_serialized|
|
20
|
+
if to_be_serialized == variable_name
|
21
|
+
serialize = true
|
22
|
+
break
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
else
|
27
|
+
serialize = true
|
28
|
+
end
|
29
|
+
return serialize
|
30
|
+
end
|
31
|
+
|
32
|
+
def object.method_missing(name, *args)
|
33
|
+
if name.to_s =~ /to_json_for_/
|
34
|
+
prev_state = @to_be_serialized
|
35
|
+
|
36
|
+
@to_be_serialized = []
|
37
|
+
variables = name.to_s[12..-1].split('_and_');
|
38
|
+
variables.each do |variable_name|
|
39
|
+
@to_be_serialized << variable_name
|
40
|
+
end
|
41
|
+
|
42
|
+
json = self.to_json
|
43
|
+
|
44
|
+
@to_be_serialized = prev_state
|
45
|
+
|
46
|
+
return json
|
47
|
+
else
|
48
|
+
super
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def object.to_json
|
53
|
+
json = self.orig_to_json
|
54
|
+
json.gsub!(/([\"\'\&\/\\\@])/,'\\\\\1')
|
55
|
+
return json
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
6
59
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
class Object
|
2
|
+
|
2
3
|
def to_json
|
3
4
|
if self.instance_variables.length == 0
|
4
5
|
if self.class.name == 'Array'
|
@@ -16,15 +17,23 @@ class Object
|
|
16
17
|
else
|
17
18
|
result = "{";
|
18
19
|
first = true
|
19
|
-
self.instance_variables.each do |
|
20
|
+
self.instance_variables.each do |variable_symbol|
|
21
|
+
|
22
|
+
if defined? self.should_variable_be_serialized?
|
23
|
+
next unless self.should_variable_be_serialized?(variable_symbol)
|
24
|
+
end
|
25
|
+
|
20
26
|
result += ',' unless first
|
21
|
-
|
22
|
-
|
23
|
-
result += '"' +
|
27
|
+
variable_name = variable_symbol.to_s.gsub(/@/,'')
|
28
|
+
variable = self.instance_variable_get(variable_symbol)
|
29
|
+
result += '"' + variable_name + '":' + variable.to_json
|
24
30
|
first = false
|
31
|
+
|
25
32
|
end
|
26
33
|
result += "}"
|
27
34
|
end
|
28
35
|
return result
|
29
36
|
end
|
37
|
+
|
38
|
+
alias_method :orig_to_json, :to_json
|
30
39
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require File.expand_path('../../lib/vfeskov_edu_gem', __FILE__)
|
3
|
+
require File.expand_path('../common/test_object', __FILE__)
|
4
|
+
class FurtherObjectExtensionTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_to_be_serialized
|
7
|
+
|
8
|
+
t = TestObject.new
|
9
|
+
t.property1 = 123
|
10
|
+
t.property2 = 'test string'
|
11
|
+
t.property3 = [123, 'test string', 234]
|
12
|
+
|
13
|
+
VfeskovEduGem.extend_to_json(t)
|
14
|
+
|
15
|
+
t.to_be_serialized(:property1, :property2)
|
16
|
+
assert_equal('{\"property1\":\"123\",\"property2\":\"test string\"}', t.to_json)
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_to_json_for
|
21
|
+
|
22
|
+
t = TestObject.new
|
23
|
+
t.property1 = 123
|
24
|
+
t.property2 = 'test string'
|
25
|
+
t.property3 = [123, 'test string', 234]
|
26
|
+
|
27
|
+
VfeskovEduGem.extend_to_json(t)
|
28
|
+
|
29
|
+
t.to_be_serialized(:property1, :property2)
|
30
|
+
|
31
|
+
assert_equal('{\"property1\":\"123\",\"property3\":[\"123\",\"test string\",\"234\"]}', t.to_json_for_property1_and_property3)
|
32
|
+
|
33
|
+
assert_equal('{\"property1\":\"123\",\"property2\":\"test string\",\"property3\":[\"123\",\"test string\",\"234\"]}', t.to_json_for_property1_and_property2_and_property3)
|
34
|
+
|
35
|
+
assert_equal('{\"property1\":\"123\",\"property2\":\"test string\"}', t.to_json)
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_escape_special_characters
|
40
|
+
|
41
|
+
t = TestObject.new
|
42
|
+
t.property1 = 'bill@gates.cool'
|
43
|
+
t.property2 = 'he said "go"'
|
44
|
+
t.property3 = ['\'guys\' & co', '\--||--/', 234]
|
45
|
+
|
46
|
+
assert_equal('{"property1":"bill@gates.cool","property2":"he said "go"","property3":["\'guys\' & co","\--||--/","234"]}', t.to_json)
|
47
|
+
|
48
|
+
VfeskovEduGem.extend_to_json(t)
|
49
|
+
|
50
|
+
assert_equal('{\"property1\":\"bill\@gates.cool\",\"property2\":\"he said \"go\"\",\"property3\":[\"\\\'guys\\\' \& co\",\"\\\--||--\/\",\"234\"]}', t.to_json)
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require File.expand_path('../../lib/vfeskov_edu_gem', __FILE__)
|
3
|
+
require File.expand_path("../common/test_object", __FILE__)
|
4
|
+
|
5
|
+
class TestObjectExtension < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def test_array
|
8
|
+
assert_equal('["123456","test string","test string 2"]', [123456,"test string","test string 2"].to_json )
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_object
|
12
|
+
t = TestObject.new()
|
13
|
+
t.property1 = 123
|
14
|
+
t.property2 = 'test string'
|
15
|
+
t.property3 = [123, 'test string', 234]
|
16
|
+
assert_equal('{"property1":"123","property2":"test string","property3":["123","test string","234"]}', t.to_json)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_object_in_object
|
20
|
+
t1 = TestObject.new
|
21
|
+
t1.property1 = 123
|
22
|
+
t1.property2 = 'test string'
|
23
|
+
t1.property3 = [123, 'test string', 234]
|
24
|
+
|
25
|
+
t2 = TestObject.new
|
26
|
+
t2.property1 = [t1,'asd']
|
27
|
+
t2.property2 = [213,342,564]
|
28
|
+
|
29
|
+
assert_equal('{"property1":[{"property1":"123","property2":"test string","property3":["123","test string","234"]},"asd"],"property2":["213","342","564"]}', t2.to_json)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vfeskov_edu_gem
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.12
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -24,6 +24,9 @@ files:
|
|
24
24
|
- lib/vfeskov_edu_gem.rb
|
25
25
|
- lib/vfeskov_edu_gem/object_extension.rb
|
26
26
|
- lib/vfeskov_edu_gem/version.rb
|
27
|
+
- test/common/test_object.rb
|
28
|
+
- test/tc_further_object_extension.rb
|
29
|
+
- test/tc_object_extension.rb
|
27
30
|
- vfeskov_edu_gem.gemspec
|
28
31
|
homepage: ''
|
29
32
|
licenses: []
|