hashify 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/hashify.rb +2 -1
- data/lib/hashify/auto.rb +47 -0
- data/lib/hashify/json.rb +2 -0
- data/spec/auto_spec.rb +30 -0
- data/spec/json_spec.rb +30 -0
- metadata +7 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/lib/hashify.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
module Hashify
|
2
2
|
|
3
|
-
autoload :Json,
|
3
|
+
autoload :Json, File.join(File.dirname(__FILE__), 'hashify', 'json')
|
4
4
|
autoload :Convert, File.join(File.dirname(__FILE__), 'hashify', 'convert')
|
5
|
+
autoload :Auto, File.join(File.dirname(__FILE__), 'hashify', 'auto')
|
5
6
|
|
6
7
|
def self.included(cls)
|
7
8
|
|
data/lib/hashify/auto.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
module Hashify
|
2
|
+
module Auto
|
3
|
+
|
4
|
+
def self.included(cls)
|
5
|
+
cls.send(:const_set, :Auto__, false)
|
6
|
+
|
7
|
+
cls.instance_eval("include Hashify") unless cls.include?(Hashify)
|
8
|
+
|
9
|
+
cls.instance_eval(<<-HERE_DOC, __FILE__, __LINE__)
|
10
|
+
def self.autoload_hash_accessors
|
11
|
+
readers = instance_methods.find_all{|m| m.to_s[-1] == ?= && m.to_s[0] != ?=}.map{|m| str = m.to_s; str.slice!(-1); str.to_sym}
|
12
|
+
readers.delete(:taguri) # i don't like this method
|
13
|
+
readers.delete_if{|m| instance_method(m).nil? }
|
14
|
+
instance_eval "hash_accessor \#{readers.map{|r| r.inspect} * ', '}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.auto_guard
|
18
|
+
if const_get(:Auto__).equal? false
|
19
|
+
send(:remove_const, :Auto__)
|
20
|
+
send(:const_set, :Auto__, true)
|
21
|
+
autoload_hash_accessors
|
22
|
+
end
|
23
|
+
yield
|
24
|
+
end
|
25
|
+
|
26
|
+
HERE_DOC
|
27
|
+
|
28
|
+
cls.class_eval(<<-HERE_DOC, __FILE__, __LINE__)
|
29
|
+
def to_hash
|
30
|
+
self.class.auto_guard do
|
31
|
+
super
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class << self
|
36
|
+
def self.from_hash(map)
|
37
|
+
auto_guard do
|
38
|
+
super
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
HERE_DOC
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
data/lib/hashify/json.rb
CHANGED
data/spec/auto_spec.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe "Basic object", "auto hashify" do
|
4
|
+
it "should convert into a hash" do
|
5
|
+
|
6
|
+
test = Class.new do
|
7
|
+
include Hashify::Auto
|
8
|
+
|
9
|
+
attr_accessor :prop1
|
10
|
+
end
|
11
|
+
|
12
|
+
t = test.new
|
13
|
+
t.prop1 = 'testing'
|
14
|
+
t.to_hash.should == {:prop1 => 'testing'}
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should convert from a hash" do
|
18
|
+
|
19
|
+
test = Class.new do
|
20
|
+
include Hashify::Auto
|
21
|
+
|
22
|
+
attr_accessor :prop1
|
23
|
+
end
|
24
|
+
|
25
|
+
t = test.from_hash(:prop1 => 'testing')
|
26
|
+
t.prop1.should == 'testing'
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
end
|
data/spec/json_spec.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe "Basic object", "json" do
|
4
|
+
it "should convert to json" do
|
5
|
+
|
6
|
+
test = Class.new do
|
7
|
+
include Hashify::Json
|
8
|
+
attr_accessor :prop1
|
9
|
+
hash_accessor :prop1
|
10
|
+
end
|
11
|
+
|
12
|
+
t = test.new
|
13
|
+
t.prop1 = 'property_value_1'
|
14
|
+
t.to_json.should == '{"prop1":"property_value_1"}'
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should convert from json" do
|
18
|
+
|
19
|
+
test = Class.new do
|
20
|
+
include Hashify::Json
|
21
|
+
attr_accessor :prop1
|
22
|
+
hash_accessor :prop1
|
23
|
+
end
|
24
|
+
|
25
|
+
t = test.from_json '{"prop1":"property_value_1"}'
|
26
|
+
t.prop1.should == 'property_value_1'
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Hull
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-11-01 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -26,10 +26,13 @@ files:
|
|
26
26
|
- Rakefile
|
27
27
|
- VERSION
|
28
28
|
- lib/hashify.rb
|
29
|
+
- lib/hashify/auto.rb
|
29
30
|
- lib/hashify/convert.rb
|
30
31
|
- lib/hashify/json.rb
|
32
|
+
- spec/auto_spec.rb
|
31
33
|
- spec/convert_spec.rb
|
32
34
|
- spec/from_hash_spec.rb
|
35
|
+
- spec/json_spec.rb
|
33
36
|
- spec/spec.opts
|
34
37
|
- spec/spec_helper.rb
|
35
38
|
- spec/to_hash_spec.rb
|
@@ -62,7 +65,9 @@ signing_key:
|
|
62
65
|
specification_version: 3
|
63
66
|
summary: Simple to_hash, to_json <-> from_hash, from_json
|
64
67
|
test_files:
|
68
|
+
- spec/auto_spec.rb
|
65
69
|
- spec/convert_spec.rb
|
66
70
|
- spec/from_hash_spec.rb
|
71
|
+
- spec/json_spec.rb
|
67
72
|
- spec/spec_helper.rb
|
68
73
|
- spec/to_hash_spec.rb
|