schema 0.0.4 → 0.0.5
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 +13 -2
- data/lib/schema/schema.rb +2 -0
- data/lib/schema/version.rb +1 -1
- data/schema.gemspec +1 -1
- data/spec/schema_spec.rb +5 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -6,7 +6,14 @@ Schema is a mechanism for enforcing schemas for ruby hashes. Type conversions ar
|
|
6
6
|
|
7
7
|
Schema.transform({:float => '42', :array_of_strings => 23}, {:float => Float, :array_of_strings => [String]})
|
8
8
|
# => {:float => 42.0, :array_of_strings => ['23']}
|
9
|
+
|
10
|
+
or
|
11
|
+
|
12
|
+
Schema.include!
|
9
13
|
|
14
|
+
{:float => '42', :array_of_strings => 23}.transform({:float => Float, :array_of_strings => [String]})
|
15
|
+
# => {:float => 42.0, :array_of_strings => ['23']}
|
16
|
+
|
10
17
|
### Schemas
|
11
18
|
|
12
19
|
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.
|
@@ -16,9 +23,13 @@ A Schema is either a type that implements `#from` or a hash of schemas or an arr
|
|
16
23
|
{ :string => String } # is a schema
|
17
24
|
{ :foo => [{ :bar => DateTime }]} # is a schema
|
18
25
|
|
19
|
-
Ruby has no Boolean class, therefore we define it, so we have
|
26
|
+
Ruby has no Boolean class, therefore we define it, so we have:
|
27
|
+
|
28
|
+
"true".transform(Boolean) == true
|
29
|
+
|
30
|
+
nil is also a valid schema but it transforms everything to nil:
|
20
31
|
|
21
|
-
|
32
|
+
"string".transform(nil) == nil
|
22
33
|
|
23
34
|
In Hash-schemas keys may be optional indicated by a trailing question mark.
|
24
35
|
|
data/lib/schema/schema.rb
CHANGED
data/lib/schema/version.rb
CHANGED
data/schema.gemspec
CHANGED
data/spec/schema_spec.rb
CHANGED
@@ -4,6 +4,11 @@ Schema.include!
|
|
4
4
|
|
5
5
|
describe "Schema" do
|
6
6
|
|
7
|
+
it "should cast nil" do
|
8
|
+
42.transform(nil).should == nil
|
9
|
+
"string".transform(nil).should == nil
|
10
|
+
end
|
11
|
+
|
7
12
|
it "should cast simple types" do
|
8
13
|
42.transform(String).should == '42'
|
9
14
|
42.transform(String).should be_kind_of(String)
|