jeokkarak 1.0.2 → 1.0.4
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.
- data/README.textile +3 -3
- data/Rakefile +2 -2
- data/lib/hashi.rb +44 -16
- data/lib/jeokkarak.rb +2 -2
- metadata +3 -3
data/README.textile
CHANGED
@@ -107,7 +107,7 @@ Jeokkarak supports schema evoluted relationships: newly created relationships wi
|
|
107
107
|
<pre>
|
108
108
|
hash = {"player" => [{"name" => "guilherme silveira"}, {"name" => "caue guerra"}], "new_relation" => {"new_entry" => 20}}
|
109
109
|
team = Team.from_hash(hash)
|
110
|
-
team.new_relation.new_entry.should
|
110
|
+
team.new_relation.new_entry.should == 20
|
111
111
|
</pre>
|
112
112
|
|
113
113
|
It also supports pre-existing relations through the use of has_child:
|
@@ -121,7 +121,7 @@ end
|
|
121
121
|
# will instantiate a Player type:
|
122
122
|
hash = {"player" => {"name" => "guilherme silveira"}}
|
123
123
|
team = Team.from_hash(hash)
|
124
|
-
team.player.class.should
|
124
|
+
team.player.class.should == Player
|
125
125
|
</pre>
|
126
126
|
|
127
127
|
And finally, it supports rails's ActiveRecord:
|
@@ -133,7 +133,7 @@ end
|
|
133
133
|
|
134
134
|
hash = {"players" => [{"name" => "guilherme silveira"}]}
|
135
135
|
team = Team.from_hash(hash)
|
136
|
-
team.players[0].class.should
|
136
|
+
team.players[0].class.should == Player
|
137
137
|
</pre>
|
138
138
|
|
139
139
|
h1. Installing
|
data/Rakefile
CHANGED
@@ -5,9 +5,9 @@ require 'rake/gempackagetask'
|
|
5
5
|
require 'spec/rake/spectask'
|
6
6
|
|
7
7
|
GEM = "jeokkarak"
|
8
|
-
GEM_VERSION = "1.0.
|
8
|
+
GEM_VERSION = "1.0.4"
|
9
9
|
SUMMARY = "Hash to object helper methods for schema evolution forward-compatible services."
|
10
|
-
AUTHOR = "Guilherme Silveira, Jose Donizetti"
|
10
|
+
AUTHOR = "Guilherme Silveira, Jose Donizetti, Caue Guerra"
|
11
11
|
EMAIL = "guilherme.silveira@caelum.com.br"
|
12
12
|
HOMEPAGE = "http://github.com/caelum/jeokkarak"
|
13
13
|
|
data/lib/hashi.rb
CHANGED
@@ -1,41 +1,69 @@
|
|
1
1
|
module Hashi
|
2
|
-
class UndefinedMethod
|
2
|
+
class UndefinedMethod < Exception
|
3
|
+
attr_reader :msg
|
4
|
+
def initialize(msg)
|
5
|
+
@msg = msg
|
6
|
+
end
|
7
|
+
def to_s
|
8
|
+
@msg
|
9
|
+
end
|
3
10
|
end
|
11
|
+
|
4
12
|
class CustomHash
|
5
13
|
|
6
|
-
attr_reader :
|
14
|
+
attr_reader :internal_hash
|
7
15
|
|
8
|
-
def initialize(
|
9
|
-
@
|
16
|
+
def initialize(hash = {})
|
17
|
+
@internal_hash = hash
|
10
18
|
end
|
11
|
-
|
19
|
+
|
20
|
+
def method_missing(name, *args, &block)
|
12
21
|
name = name.to_s if name.kind_of? Symbol
|
13
22
|
if name[-1,1] == "?"
|
14
|
-
parse(@
|
23
|
+
parse(name, @internal_hash[name.chop])
|
15
24
|
elsif name[-1,1] == "="
|
16
|
-
@
|
25
|
+
@internal_hash[name.chop] = args[0]
|
26
|
+
elsif @internal_hash.kind_of?(Array) && name == "each"
|
27
|
+
@internal_hash.each do |k| block.call(transform(k)) end
|
28
|
+
elsif name.respond_to? name
|
29
|
+
@internal_hash.send(name, *args, &block)
|
17
30
|
else
|
18
|
-
|
31
|
+
return nil if @internal_hash.has_key?(name) && @internal_hash[name].nil?
|
32
|
+
parse(name, transform(@internal_hash[name]))
|
19
33
|
end
|
20
34
|
end
|
35
|
+
|
36
|
+
def respond_to?(symbol)
|
37
|
+
super(symbol) || (is_hash? && @internal_hash.key?(symbol.to_s))
|
38
|
+
end
|
39
|
+
|
40
|
+
def is_hash?
|
41
|
+
@internal_hash.kind_of? Hash
|
42
|
+
end
|
43
|
+
|
21
44
|
def [](x)
|
22
|
-
transform(@
|
45
|
+
transform(@internal_hash[x])
|
23
46
|
end
|
47
|
+
|
24
48
|
private
|
25
49
|
def transform(value)
|
26
50
|
return CustomHash.new(value) if value.kind_of?(Hash) || value.kind_of?(Array)
|
27
51
|
value
|
28
52
|
end
|
29
|
-
|
30
|
-
|
31
|
-
|
53
|
+
|
54
|
+
def parse(name, value)
|
55
|
+
raise Hashi::UndefinedMethod.new("undefined method '#{name}'") if value.nil?
|
56
|
+
value
|
32
57
|
end
|
33
58
|
|
34
59
|
end
|
35
|
-
|
36
|
-
|
60
|
+
|
61
|
+
def self.from_hash(hash)
|
62
|
+
CustomHash.new(hash)
|
37
63
|
end
|
38
|
-
|
39
|
-
|
64
|
+
|
65
|
+
def self.to_object(hash)
|
66
|
+
CustomHash.new(hash)
|
40
67
|
end
|
68
|
+
|
41
69
|
end
|
data/lib/jeokkarak.rb
CHANGED
@@ -10,7 +10,7 @@ module Jeokkarak
|
|
10
10
|
|
11
11
|
# checks what is the type element for this type (supports rails ActiveRecord, has_child and Hashi)
|
12
12
|
def child_type_for(name)
|
13
|
-
return reflect_on_association(
|
13
|
+
return reflect_on_association(name.to_sym ).klass if respond_to? :reflect_on_association
|
14
14
|
resource_children[name] || Hashi
|
15
15
|
end
|
16
16
|
|
@@ -30,7 +30,7 @@ module Jeokkarak
|
|
30
30
|
from_hash_parse result, h, key, value
|
31
31
|
end
|
32
32
|
def result.method_missing(name, *args, &block)
|
33
|
-
|
33
|
+
Hashi.to_object(@_internal_hash).send(name, args[0], block)
|
34
34
|
end
|
35
35
|
result
|
36
36
|
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jeokkarak
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- Guilherme Silveira, Jose Donizetti
|
7
|
+
- Guilherme Silveira, Jose Donizetti, Caue Guerra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-01-25 00:00:00 -02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|