json_object 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.markdown +78 -3
- data/lib/json_object.rb +12 -15
- data/lib/json_object/version.rb +1 -1
- data/spec/json_object_spec.rb +33 -9
- metadata +2 -2
data/Gemfile.lock
CHANGED
data/README.markdown
CHANGED
@@ -7,13 +7,88 @@
|
|
7
7
|
JSONObject is a simple Ruby library for converting JSON objects into Ruby classes.
|
8
8
|
|
9
9
|
It:
|
10
|
+
|
10
11
|
1. Simplifies parsing JSON objects into classes
|
12
|
+
|
11
13
|
2. Creates a series of setters, getters with instance variables for your new classes
|
12
14
|
|
13
|
-
##
|
15
|
+
## Introduction
|
14
16
|
|
15
17
|
### Installation
|
16
18
|
|
17
|
-
Installation could be simpler
|
19
|
+
Installation could be simpler:
|
20
|
+
|
21
|
+
gem 'json_object', '~> 0.0.2'
|
22
|
+
|
23
|
+
or
|
24
|
+
|
25
|
+
gem install json_object
|
26
|
+
|
27
|
+
|
28
|
+
### Examples
|
29
|
+
|
30
|
+
Once you have the gem installed crack open an IRB session:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
require 'json_object'
|
34
|
+
|
35
|
+
json = '{ "first_name" : "David", "last_name" : "White" }'
|
36
|
+
|
37
|
+
obj = JSONObject.new json
|
38
|
+
|
39
|
+
=> #<Object:0x00000100859688 @first_name="David", @last_name="White">
|
40
|
+
|
41
|
+
obj.first_name
|
42
|
+
|
43
|
+
=> "David"
|
44
|
+
|
45
|
+
obj.last_name
|
46
|
+
|
47
|
+
=> "White"
|
48
|
+
|
49
|
+
obj.first_name = "Dave"
|
50
|
+
|
51
|
+
=> "Dave"
|
52
|
+
|
53
|
+
```
|
54
|
+
|
55
|
+
You can also create new classes completely dynamically without ever writing a class in Ruby:
|
56
|
+
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
require 'json_object'
|
60
|
+
|
61
|
+
json = '{ "first_name" : "David", "last_name" : "White" }'
|
62
|
+
|
63
|
+
person = JSONObject.new json, "person"
|
64
|
+
|
65
|
+
=> #<Person:0x00000100859688 @first_name="David", @last_name="White">
|
66
|
+
|
67
|
+
person.first_name
|
68
|
+
|
69
|
+
=> "David"
|
70
|
+
|
71
|
+
person.last_name
|
72
|
+
|
73
|
+
=> "White"
|
74
|
+
|
75
|
+
person.first_name = "Dave"
|
76
|
+
|
77
|
+
=> "Dave"
|
78
|
+
|
79
|
+
person = Person.new
|
80
|
+
|
81
|
+
=> #<Person:0x00000100859688 @first_name="", @last_name="">
|
82
|
+
|
83
|
+
person.first_name = "Jeremy"
|
84
|
+
|
85
|
+
=> "Jeremy"
|
86
|
+
|
87
|
+
person.last_name = "White"
|
88
|
+
|
89
|
+
=> "White"
|
90
|
+
|
91
|
+
```
|
92
|
+
|
18
93
|
|
19
|
-
|
94
|
+
And now you have a fully functioning Ruby class with data from your json file.
|
data/lib/json_object.rb
CHANGED
@@ -3,26 +3,23 @@ require "json"
|
|
3
3
|
require "active_support/core_ext/string"
|
4
4
|
|
5
5
|
module JSONObject
|
6
|
-
def self.
|
7
|
-
|
6
|
+
def self.new json, class_name = "object"
|
7
|
+
json_obj = JSON.parse(json)
|
8
|
+
|
9
|
+
Object.const_set(class_name.classify, Class.new) unless class_name == "object"
|
8
10
|
|
9
|
-
|
10
|
-
json_obj = JSON.parse(json)
|
11
|
+
klass = class_name.classify.constantize
|
11
12
|
|
12
|
-
|
13
|
-
|
13
|
+
klass.instance_eval do
|
14
|
+
attr_accessor *(json_obj.keys)
|
15
|
+
end
|
14
16
|
|
15
|
-
|
16
|
-
names.each_with_index do |name,i|
|
17
|
-
instance_variable_set("@"+name, values[i])
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
17
|
+
klass = klass.new
|
21
18
|
|
22
|
-
|
23
|
-
|
24
|
-
end
|
19
|
+
json_obj.each do |key, value|
|
20
|
+
klass.instance_variable_set "@#{key}".to_sym, value
|
25
21
|
end
|
22
|
+
|
26
23
|
return klass
|
27
24
|
end
|
28
25
|
end
|
data/lib/json_object/version.rb
CHANGED
data/spec/json_object_spec.rb
CHANGED
@@ -1,18 +1,12 @@
|
|
1
1
|
require 'json_object'
|
2
2
|
|
3
3
|
describe JSONObject do
|
4
|
-
context '#
|
5
|
-
|
6
|
-
object = JSONObject.json_to_object
|
7
|
-
object.class.should == Object.new.class
|
8
|
-
end
|
9
|
-
|
4
|
+
context '#new' do
|
5
|
+
|
10
6
|
context ' created with following JSON "{ "name" : "David" }"' do
|
11
|
-
|
12
7
|
before :all do
|
13
8
|
json = '{ "name" : "David" }'
|
14
|
-
@object = JSONObject.
|
15
|
-
puts @object.inspect
|
9
|
+
@object = JSONObject.new json
|
16
10
|
end
|
17
11
|
|
18
12
|
it 'should return Object class that responds to name method' do
|
@@ -22,7 +16,37 @@ describe JSONObject do
|
|
22
16
|
it 'should return Object class that returns "David" on name method' do
|
23
17
|
@object.name.should == "David"
|
24
18
|
end
|
19
|
+
|
20
|
+
it 'should allow the name to be changed' do
|
21
|
+
@object.name = "Dave"
|
22
|
+
@object.name.should == "Dave"
|
23
|
+
end
|
25
24
|
end
|
26
25
|
|
26
|
+
context ' created with following JSON "{ "first_name" : "David", "last_name" : "White" }"' do
|
27
|
+
before :all do
|
28
|
+
json = '{ "first_name" : "David", "last_name" : "White" }'
|
29
|
+
@person = JSONObject.new json, "person"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should allow me to change the name of the class" do
|
33
|
+
@person.class.should == Person
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should allow me to create new classes with that class" do
|
37
|
+
person = Person.new
|
38
|
+
person.class.should == Person
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should allow me to create a new class that responds to first_name" do
|
42
|
+
person = Person.new
|
43
|
+
person.respond_to?(:first_name).should == true
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should allow me to create a new class that responds to last_name" do
|
47
|
+
person = Person.new
|
48
|
+
person.respond_to?(:last_name).should == true
|
49
|
+
end
|
50
|
+
end
|
27
51
|
end
|
28
52
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: json_object
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- David White
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-08-
|
13
|
+
date: 2011-08-29 00:00:00 +01:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|