schema 0.0.2 → 0.0.3
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.md +15 -1
- data/lib/schema/schema.rb +6 -1
- data/lib/schema/version.rb +1 -1
- data/schema.gemspec +2 -2
- data/spec/schema_spec.rb +29 -0
- metadata +3 -3
data/README.md
CHANGED
@@ -9,13 +9,27 @@ Schema is a mechanism for enforcing schemas for ruby hashes. Type conversions ar
|
|
9
9
|
|
10
10
|
### Schemas
|
11
11
|
|
12
|
-
A Schema is either a type that implements `#from` or a hash of schemas or an array with a schema as it's
|
12
|
+
A Schema is either a type that implements `#from` or a hash of schemas or an array with a schema as it's single element.
|
13
13
|
|
14
14
|
Float # is a schema
|
15
15
|
[Float] # is a schema
|
16
16
|
{ :string => String } # is a schema
|
17
17
|
{ :foo => [{ :bar => DateTime }]} # is a schema
|
18
18
|
|
19
|
+
In Hash-schemas keys may be optional indicated by a trailing question mark.
|
20
|
+
|
21
|
+
{ :optional => '42' }.transform({ :optional? => Float }) == { :optional => 42.0 }
|
22
|
+
{}.transform({ :optional? => Float }) == {}
|
23
|
+
|
24
|
+
This also enables circular schemas (for eg. trees)
|
25
|
+
|
26
|
+
schema = { :value => Float }
|
27
|
+
schema.update { :children? => [schema] }
|
28
|
+
|
29
|
+
### Issues
|
30
|
+
|
31
|
+
- Works only for symbol keys. Not yet sure if this is a bug or a feature...
|
32
|
+
|
19
33
|
### Note on Patches/Pull Requests
|
20
34
|
|
21
35
|
* Fork the project.
|
data/lib/schema/schema.rb
CHANGED
@@ -20,7 +20,12 @@ module Schema
|
|
20
20
|
when Hash
|
21
21
|
raise ArgumentError, "A #{schema.class} is no valid schema for a #{object.class}." unless object.is_a?(Hash)
|
22
22
|
schema.inject({}) do |h, (key, subschema)|
|
23
|
-
|
23
|
+
if key.to_s =~ /^(.*)\?$/
|
24
|
+
optionalkey = $1.to_sym
|
25
|
+
h.update(object[optionalkey] ? { optionalkey => object[optionalkey].transform(subschema) } : {})
|
26
|
+
else
|
27
|
+
h.update(key => object[key].transform(subschema))
|
28
|
+
end
|
24
29
|
end
|
25
30
|
else
|
26
31
|
schema.from(object)
|
data/lib/schema/version.rb
CHANGED
data/schema.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{schema}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Daniel Kirsch"]
|
12
|
-
s.date = %q{2010-12-
|
12
|
+
s.date = %q{2010-12-06}
|
13
13
|
s.description = %q{Deep type conversion through schemas for hashes}
|
14
14
|
s.email = %q{danishkirel@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/spec/schema_spec.rb
CHANGED
@@ -48,6 +48,35 @@ describe "Schema" do
|
|
48
48
|
{ :nested => [{ :hash => 42 }] }
|
49
49
|
end
|
50
50
|
|
51
|
+
### optional keys in schemas
|
52
|
+
|
53
|
+
it "should allow optional keys in schemas" do
|
54
|
+
schema = { :optional? => Float }
|
55
|
+
{ :optional => '42' }.transform(schema).should == { :optional => 42.0 }
|
56
|
+
{}.transform(schema).should == {}
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should cast circular schemas" do
|
60
|
+
schema = { :float => Float }
|
61
|
+
schema.update :circular? => schema # circular schemas _MUST_ be optional
|
62
|
+
|
63
|
+
{ :float => '42', :circular => { :float => '17' } }.transform(schema).should ==
|
64
|
+
{ :float => 42.0, :circular => { :float => 17.0 } }
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should cast circular schemas with arrays" do
|
68
|
+
schema = { :float => Float }
|
69
|
+
schema.update :circular? => [schema] # circular schemas _MUST_ be optional
|
70
|
+
|
71
|
+
{ :float => '42', :circular => { :float => '17' } }.transform(schema).should ==
|
72
|
+
{ :float => 42.0, :circular => [{ :float => 17.0 }] }
|
73
|
+
|
74
|
+
{ :float => '42', :circular => [{ :float => '17' }, { :float => '23' }] }.transform(schema).should ==
|
75
|
+
{ :float => 42.0, :circular => [{ :float => 17.0 }, { :float => 23.0 }] }
|
76
|
+
end
|
77
|
+
|
78
|
+
### how to cope with non-present keys
|
79
|
+
|
51
80
|
it "should erase keys not present in the schema" do
|
52
81
|
{:not => 'present'}.transform({}).should == {}
|
53
82
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Daniel Kirsch
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-12-
|
17
|
+
date: 2010-12-06 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|