jstruct 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ben Burkert
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def to_jexp
3
+ map {|o| o.to_jexp }
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ class Hash
2
+ def to_jexp
3
+ inject(self::class.new) do |h, (key, value)|
4
+ h.update key.to_s => value.to_jexp
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ class Integer
2
+ def to_jexp() self end
3
+ end
@@ -0,0 +1,3 @@
1
+ class Numeric
2
+ def to_jexp() self end
3
+ end
@@ -0,0 +1,5 @@
1
+ class Object
2
+ def to_jexp
3
+ JSON.parse(JSON.dump([self])).first
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ class String
2
+ def to_jexp() self end
3
+ end
@@ -0,0 +1,68 @@
1
+ module JStruct
2
+ class JClass
3
+ extend Forwardable
4
+
5
+ def_delegator :to_hash, :to_jexp
6
+ def_delegator :to_jexp, :to_json
7
+
8
+ def self.from(data)
9
+ case data
10
+ when String then from(JSON.parse(data))
11
+ when Array then data.map {|o| from(o) }
12
+ when Hash then from_hash(data)
13
+ when NilClass then nil
14
+ else raise("Can't parse a #{data.class} into a #{self}")
15
+ end
16
+ end
17
+
18
+ def self.from_hash(hash)
19
+ attributes = hash.inject({}) do |h, (member, value)|
20
+ if complex_members.keys.include?(member.to_sym)
21
+ klass = complex_members[member.to_sym]
22
+ value = klass.from(value)
23
+ end
24
+ h.update(member.to_sym => value)
25
+ end
26
+
27
+ new(attributes)
28
+ end
29
+
30
+ def self.inherited(klass)
31
+ %w[ simple_members complex_members ].each do |accessor|
32
+ klass.class_eval("def self.#{accessor} ; @@#{accessor} ; end", __FILE__, __LINE__ + 1)
33
+ klass.class_eval("def self.#{accessor}=(val) ; @@#{accessor} = val ; end", __FILE__, __LINE__ + 1)
34
+ end
35
+ end
36
+
37
+ def self.set_members!(simple_members, complex_members)
38
+ self.simple_members = simple_members
39
+ self.complex_members = complex_members
40
+
41
+ attr_accessor(*(simple_members + complex_members.keys))
42
+ end
43
+
44
+ def initialize(attributes)
45
+ attributes.each do |member, value|
46
+ send(:"#{member}=", value)
47
+ end
48
+ end
49
+
50
+ def members
51
+ self::class.simple_members + self::class.complex_members.keys
52
+ end
53
+
54
+ def to_hash
55
+ members.inject({}) {|h, k| h.update(k => send(k.to_sym)) }
56
+ end
57
+
58
+ def to_s
59
+ "#<jstruct #{self.class.inspect} #{to_hash.inspect} >"
60
+ end
61
+
62
+ alias_method :inspect, :to_s
63
+
64
+ def ==(other)
65
+ members.all? {|member| send(member) == other.send(member) }
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ module JStruct
2
+ VERSION = '0.1.0'
3
+ end
data/lib/jstruct.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'forwardable'
2
+ require 'json'
3
+
4
+ module JStruct
5
+ def self.new(*simple_members)
6
+ complex_members = simple_members.last.is_a?(Hash) ? simple_members.pop : {}
7
+
8
+ klass = Class.new(JClass)
9
+ klass.set_members!(simple_members, complex_members)
10
+ klass
11
+ end
12
+ end
13
+
14
+ require 'jstruct/jclass'
15
+ require 'jstruct/ext/array'
16
+ require 'jstruct/ext/hash'
17
+ require 'jstruct/ext/integer'
18
+ require 'jstruct/ext/object'
19
+ require 'jstruct/ext/string'
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe JStruct do
4
+ it 'can roundtrip JSON' do
5
+ klass = Class.new(JStruct.new(:foo, :bar))
6
+ JSON.parse(klass.new(:foo => 'abc', :bar => 123).to_json)
7
+ end
8
+
9
+ it 'can handle complex members' do
10
+ klass1 = Class.new(JStruct.new(:a, :b))
11
+ klass2 = Class.new(JStruct.new(:c, :d => klass1))
12
+
13
+ original = klass2.new(:c => [1], :d => klass1.new(:a => 'a', :b => 1))
14
+
15
+ klass2.from(original.to_json).should == original
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+
3
+ require 'jstruct'
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jstruct
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Ben Burkert
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-07 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: json
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rake
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: rspec
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 2
60
+ - 0
61
+ version: "2.0"
62
+ type: :development
63
+ version_requirements: *id003
64
+ description: A simple libary for objects that dump & load JSON.
65
+ email: ben@benburkert.com
66
+ executables: []
67
+
68
+ extensions: []
69
+
70
+ extra_rdoc_files: []
71
+
72
+ files:
73
+ - ./lib/jstruct/ext/array.rb
74
+ - ./lib/jstruct/ext/hash.rb
75
+ - ./lib/jstruct/ext/integer.rb
76
+ - ./lib/jstruct/ext/numeric.rb
77
+ - ./lib/jstruct/ext/object.rb
78
+ - ./lib/jstruct/ext/string.rb
79
+ - ./lib/jstruct/jclass.rb
80
+ - ./lib/jstruct/version.rb
81
+ - ./lib/jstruct.rb
82
+ - LICENSE
83
+ - ./spec/jstruct_spec.rb
84
+ - ./spec/spec_helper.rb
85
+ has_rdoc: true
86
+ homepage: http://github.com/benburkert/jstruct
87
+ licenses: []
88
+
89
+ post_install_message:
90
+ rdoc_options: []
91
+
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ requirements: []
113
+
114
+ rubyforge_project:
115
+ rubygems_version: 1.5.2
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: A simple libary for objects that dump & load JSON.
119
+ test_files:
120
+ - ./spec/jstruct_spec.rb
121
+ - ./spec/spec_helper.rb