doodle 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -41,8 +41,12 @@ puts str
41
41
  loaded_event = YAML::load(str)
42
42
  pp loaded_event
43
43
 
44
- another_event = YAML::load(DATA.read)
45
- another_event.validate!(true)
44
+ str =<<EOS
45
+ --- !ruby/object:Event
46
+ name: Glastonbury
47
+ date: 2000-07-01
48
+ EOS
49
+ event = YAML::load(str).validate! # will raise Doodle::ValidationError
46
50
 
47
51
  __END__
48
52
  --- !ruby/object:Event
@@ -696,14 +696,15 @@ module Doodle
696
696
  if att.name == :default || att.default_defined?
697
697
  # nop
698
698
  elsif !ivar_defined?(att.name)
699
- handle_error name, ArgumentError, "#{self} missing required attribute '#{name}'", [caller[-1]]
699
+ handle_error name, Doodle::ValidationError, "#{self} missing required attribute '#{name}'", [caller[-1]]
700
700
  end
701
- # if all == true, validate all attributes - e.g. when loaded from YAML
701
+ # if all == true, reset values so conversions and validations are applied to raw instance variables
702
+ # e.g. when loaded from YAML
702
703
  if all
703
- att.validate(send(att.name))
704
+ send(att.name, instance_variable_get("@#{att.name}"))
704
705
  end
705
706
  end
706
-
707
+ # now apply instance level validations
707
708
  validations.each do |v|
708
709
  #Doodle::Debug.d { [:validate!, self, v ] }
709
710
  if !instance_eval(&v.block)
@@ -776,7 +777,10 @@ module Doodle
776
777
  mklass = class << klass; self; end
777
778
  #p [:names, klass, mklass]
778
779
  #eval src = "def #{ names.join('::') }::#{name}(*args, &block); #{ names.join('::') }::#{name}.new(*args, &block); end"
779
- klass.class_eval src = "def self.#{name}(*args, &block); #{name}.new(*args, &block); end"
780
+ # TODO: check how many times this is being called
781
+ if !klass.respond_to?(name)
782
+ klass.class_eval(src = "def self.#{name}(*args, &block); #{name}.new(*args, &block); end")
783
+ end
780
784
  end
781
785
  #p [:factory, mklass, klass, src]
782
786
  end
@@ -18,4 +18,63 @@ describe 'Doodle', 'parents' do
18
18
  foo.validations.size.should == 1
19
19
  end
20
20
  end
21
+ end
22
+
23
+ describe 'Doodle', ' loading good data from yaml' do
24
+ temporary_constant :Foo do
25
+ before :each do
26
+ class Foo < Doodle::Base
27
+ has :date, :kind => Date do
28
+ from String do |s|
29
+ Date.parse(s)
30
+ end
31
+ end
32
+ end
33
+ @str = %[
34
+ --- !ruby/object:Foo
35
+ date: "2000-7-01"
36
+ ]
37
+
38
+ end
39
+
40
+ it 'should succeed without validation' do
41
+ proc { foo = YAML::load(@str)}.should_not raise_error
42
+ end
43
+
44
+ it 'should validate ok' do
45
+ proc { foo = YAML::load(@str).validate! }.should_not raise_error
46
+ end
47
+
48
+ it 'should apply conversions' do
49
+ foo = YAML::load(@str).validate!
50
+ foo.date.should == Date.new(2000, 7, 1)
51
+ foo.date.class.should == Date
52
+ end
53
+ end
54
+ end
55
+
56
+ describe 'Doodle', ' loading bad data from yaml' do
57
+ temporary_constant :Foo do
58
+ before :each do
59
+ class Foo < Doodle::Base
60
+ has :date, :kind => Date do
61
+ from String do |s|
62
+ Date.parse(s)
63
+ end
64
+ end
65
+ end
66
+ @str = %[
67
+ --- !ruby/object:Foo
68
+ date: "2000"
69
+ ]
70
+ end
71
+
72
+ it 'should succeed without validation' do
73
+ proc { foo = YAML::load(@str)}.should_not raise_error
74
+ end
75
+
76
+ it 'should fail with ConversionError when it cannot convert' do
77
+ proc { foo = YAML::load(@str).validate! }.should raise_error(Doodle::ConversionError)
78
+ end
79
+ end
21
80
  end
@@ -67,8 +67,8 @@ describe Doodle, 'defaults which have not been set' do
67
67
  end
68
68
  end
69
69
 
70
- it 'should raise error if required attributes not passed to new' do
71
- proc { foo = Foo.new }.should raise_error(ArgumentError)
70
+ it 'should raise Doodle::ValidationError if required attributes not passed to new' do
71
+ proc { foo = Foo.new }.should raise_error(Doodle::ValidationError)
72
72
  end
73
73
 
74
74
  it 'should not raise error if required attributes passed to new' do
@@ -124,8 +124,8 @@ describe Doodle, "overriding inherited defaults" do
124
124
  text.value.should == 'any'
125
125
  end
126
126
 
127
- it 'should raise ArgumentError if initialized without all required values' do
128
- proc { KeyValue.new(:value => 'Enter name:') }.should raise_error(ArgumentError)
127
+ it 'should raise Doodle::ValidationError if initialized without all required values' do
128
+ proc { KeyValue.new(:value => 'Enter name:') }.should raise_error(Doodle::ValidationError)
129
129
  end
130
130
 
131
131
  it 'should allow initialization using inherited defaults' do
@@ -9,8 +9,8 @@ describe Doodle, 'defaults which have not been set' do
9
9
  end
10
10
  end
11
11
 
12
- it 'should have raise error if required value not set' do
13
- proc { Foo.new }.should raise_error(ArgumentError)
12
+ it 'should raise Doodle::ValidationError if required value not set' do
13
+ proc { Foo.new }.should raise_error(Doodle::ValidationError)
14
14
  end
15
15
  end
16
16
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: doodle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean O'Halpin
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-03-24 00:00:00 +00:00
12
+ date: 2008-03-25 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies: []
15
15