schema 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
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
- Boolean # is a schema
32
+ "string".transform(nil) == nil
22
33
 
23
34
  In Hash-schemas keys may be optional indicated by a trailing question mark.
24
35
 
@@ -27,6 +27,8 @@ module Schema
27
27
  h.update(key => object[key].transform(subschema))
28
28
  end
29
29
  end
30
+ when nil
31
+ nil
30
32
  else
31
33
  schema.from(object)
32
34
  end
@@ -1,3 +1,3 @@
1
1
  module Schema
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{schema}
8
- s.version = "0.0.4"
8
+ s.version = "0.0.5"
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"]
@@ -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)
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Daniel Kirsch