json_object 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json_object (0.0.1)
4
+ json_object (0.0.2)
5
5
  activesupport
6
6
  i18n
7
7
  json
@@ -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
- ## An Introduction
15
+ ## Introduction
14
16
 
15
17
  ### Installation
16
18
 
17
- Installation could be simpler but lets be honest it's not difficult
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
- gem 'json_object', :git => "git@github.com:sprysoft/json_object.git"
94
+ And now you have a fully functioning Ruby class with data from your json file.
@@ -3,26 +3,23 @@ require "json"
3
3
  require "active_support/core_ext/string"
4
4
 
5
5
  module JSONObject
6
- def self.json_to_object json = "", class_name = "object"
7
- klass = class_name == "object" ? Object.new : class_name.constantize.classify.new
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
- unless json == ""
10
- json_obj = JSON.parse(json)
11
+ klass = class_name.classify.constantize
11
12
 
12
- klass.class_eval do
13
- attr_accessor *(json_obj.keys)
13
+ klass.instance_eval do
14
+ attr_accessor *(json_obj.keys)
15
+ end
14
16
 
15
- define_method :initialize do |*values|
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
- json_obj.each do |key, value|
23
- klass.instance_variable_set "@#{key}".to_sym, value
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
@@ -1,3 +1,3 @@
1
1
  module JSONObject
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,18 +1,12 @@
1
1
  require 'json_object'
2
2
 
3
3
  describe JSONObject do
4
- context '#json_to_object' do
5
- it 'should return Object class when no json is passed' do
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.json_to_object json
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.1
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-28 00:00:00 +01:00
13
+ date: 2011-08-29 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency