doodle 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,12 @@
1
+ 2008-03-12 seano <sean.ohalpin@gmail.com>
2
+
3
+ * added init_spec
4
+
5
+ * tweaked Rakefile (don't run profile by default, fix dcov task)
6
+
7
+ * allow :collect => :name form for collecting raw
8
+ objects (i.e. don't do a klass.new(*args) on values)
9
+
10
+ * use snake_case (from facets) when generating name from class for
11
+ collect
12
+
@@ -29,6 +29,10 @@ module Doodle
29
29
  def self.flatten_first_level(enum)
30
30
  enum.inject([]) {|arr, i| if i.kind_of? Array then arr.push(*i) else arr.push(i) end }
31
31
  end
32
+ # from facets/string/case.rb, line 80
33
+ def self.snake_case(camel_cased_word)
34
+ camel_cased_word.gsub(/([A-Z]+)([A-Z])/,'\1_\2').gsub(/([a-z])([A-Z])/,'\1_\2').downcase
35
+ end
32
36
  end
33
37
 
34
38
  # internal error raised when a default was expected but not found
@@ -506,9 +510,13 @@ module Doodle
506
510
 
507
511
  # define a collector
508
512
  # - collection should provide a :<< method
509
- def define_collector(collection, klass, name, &block)
513
+ def define_collector(collection, name, klass = nil, &block)
510
514
  # need to use string eval because passing block
511
- module_eval "def #{name}(*args, &block); #{collection} << #{klass}.new(*args, &block); end"
515
+ if klass.nil?
516
+ module_eval "def #{name}(*args, &block); args.unshift(block) if block_given?; #{collection}.<<(*args); end"
517
+ else
518
+ module_eval "def #{name}(*args, &block); #{collection} << #{klass}.new(*args, &block); end"
519
+ end
512
520
  end
513
521
  private :define_collector
514
522
 
@@ -549,19 +557,20 @@ module Doodle
549
557
  if collector.kind_of?(Hash)
550
558
  collector_name, klass = collector.to_a[0]
551
559
  else
560
+ # if Capitalized word given, treat as classname
561
+ # and create collector for specific class
552
562
  klass = collector.to_s
553
- collector_name = klass.downcase
563
+ collector_name = Utils.snake_case(klass)
564
+ if klass !~ /^[A-Z]/
565
+ klass = nil
566
+ end
554
567
  end
555
- define_collector name, klass, collector_name
568
+ define_collector name, collector_name, klass
556
569
  end
557
570
 
558
571
  # d { [:has_args, :params, params] }
559
- # fixme[this is a little fragile - depends on order of local_attributes in Attribute - should convert to hash args]
560
- # self_class.local_attributes[name] = attribute = Attribute.new(params, &block)
561
572
  local_attributes[name] = attribute = Attribute.new(params, &block)
562
573
  define_getter_setter name, *args, &block
563
-
564
- #super(*args, &block) if defined?(super)
565
574
  attribute
566
575
  end
567
576
 
@@ -22,63 +22,62 @@ describe Doodle::Attribute, 'basics' do
22
22
  end
23
23
  end
24
24
 
25
- @goo = Foo.new
26
- @baz = Bar.new :info => 'Hi'
25
+ @foo = Foo.new
26
+ @bar = Bar.new :info => 'Hi'
27
27
  end
28
28
 
29
- it 'should have default name' do
30
- #pending 'making it work'
31
- #p [:name, :default, @goo.attributes[:name].default]
32
- @goo.attributes[:name].default.should == 'Hello'
29
+ it 'should have attribute :name with default defined' do
30
+ @foo.attributes[:name].default.should == 'Hello'
33
31
  end
34
32
 
35
33
  it 'should have default name' do
36
- #pending 'making it work'
37
- @goo.name.should == 'Hello'
34
+ @foo.name.should == 'Hello'
35
+ end
36
+
37
+ it 'should not have an instance variable for a default' do
38
+ @foo.instance_variables.include?('@name').should == false
38
39
  end
39
40
 
40
41
  it 'should have name required == false (because has default)' do
41
- #pending 'to do required/optional'
42
- @goo.attributes[:name].required?.should == false
42
+ @foo.attributes[:name].required?.should == false
43
43
  end
44
44
 
45
45
  it 'should have info required == true' do
46
- #pending 'to do required/optional'
47
- @baz.attributes[:info].required?.should == true
46
+ @bar.attributes[:info].required?.should == true
48
47
  end
49
48
 
50
49
  it 'should have name.optional? == true (because has default)' do
51
- #pending 'to do required/optional'
52
- @goo.attributes[:name].optional?.should == true
50
+ @foo.attributes[:name].optional?.should == true
51
+ end
52
+
53
+ it 'should inherit attribute from parent' do
54
+ @bar.attributes[:name].should == @foo.attributes[:name]
53
55
  end
54
56
 
55
57
  it 'should have info.optional? == false' do
56
- #pending 'to do required/optional'
57
- @baz.attributes[:info].optional?.should == false
58
+ @bar.attributes[:info].optional?.should == false
58
59
  end
59
60
 
60
- it "should have parents in order" do
61
+ it "should have parents in correct order" do
61
62
  Bar.parents.should == [Foo, Object]
62
63
  end
63
64
 
64
- it "should have Bar's meta parents in reverse order of definition" do
65
- @baz.meta.parents.should == [Bar.singleton_class.singleton_class, Bar.singleton_class, Foo.singleton_class]
65
+ it "should have Bar's singleton parents in reverse order of definition" do
66
+ @bar.singleton_class.parents.should == [Bar.singleton_class.singleton_class, Bar.singleton_class, Foo.singleton_class]
66
67
  end
67
68
 
68
- it 'should have inherited meta local_attributes in order of definition' do
69
- @baz.meta.class_eval { collect_inherited(:local_attributes).map { |x| x[0]} }.should == [:metadata, :doc]
69
+ it 'should have inherited singleton local_attributes in order of definition' do
70
+ @bar.singleton_class.class_eval { collect_inherited(:local_attributes).map { |x| x[0]} }.should == [:metadata, :doc]
70
71
  end
71
72
 
72
- it 'should have inherited meta attributes in order of definition' do
73
- @baz.meta.attributes.keys.should == [:metadata, :doc]
73
+ it 'should have inherited singleton attributes in order of definition' do
74
+ @bar.singleton_class.attributes.keys.should == [:metadata, :doc]
74
75
  end
75
76
  end
76
77
 
77
78
  describe Doodle::Attribute, 'attribute order' do
78
79
  before :each do
79
- undefine_const(:A)
80
- undefine_const(:B)
81
- undefine_const(:C)
80
+ raise_if_defined :A, :B, :C
82
81
 
83
82
  class A < Doodle::Base
84
83
  has :a
@@ -91,9 +90,14 @@ describe Doodle::Attribute, 'attribute order' do
91
90
  class C < B
92
91
  has :c
93
92
  end
94
-
95
93
  end
96
94
 
95
+ after :each do
96
+ undefine_const(:A)
97
+ undefine_const(:B)
98
+ undefine_const(:C)
99
+ end
100
+
97
101
  it 'should keep order of inherited attributes' do
98
102
  C.parents.should == [B, A, Doodle::Base, Object]
99
103
  end
@@ -0,0 +1,57 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '../.'))
2
+ require 'lib/spec_helper'
3
+
4
+ describe Doodle, 'init' do
5
+
6
+ before(:each) do
7
+ raise_if_defined :Foo
8
+ class Foo
9
+ include Doodle::Helper
10
+ has :name, :init => 'D1'
11
+ class << self
12
+ has :metadata, :init => 'D2'
13
+ end
14
+ end
15
+ @foo = Foo.new
16
+ class << @foo
17
+ has :special, :init => 'D3'
18
+ end
19
+ end
20
+ after :each do
21
+ undefine_const :Foo
22
+ end
23
+
24
+ it 'should have instance attribute init via class' do
25
+ Foo.attributes[:name].init.should == 'D1'
26
+ end
27
+ it 'should have instance attribute init via instance' do
28
+ @foo.attributes[:name].init.should == 'D1'
29
+ end
30
+ it 'should have class attribute init via class.singleton_class' do
31
+ Foo.singleton_class.attributes(false)[:metadata].init.should == 'D2'
32
+ end
33
+ it 'should have class attribute init via class.singleton_class' do
34
+ Foo.singleton_class.attributes[:metadata].init.should == 'D2'
35
+ end
36
+ it 'should have singleton attribute init via instance.singleton_class' do
37
+ @foo.singleton_class.attributes(false)[:special].init.should == 'D3'
38
+ end
39
+ it 'should have singleton attribute init via instance.singleton_class' do
40
+ @foo.singleton_class.attributes[:special].init.should == 'D3'
41
+ end
42
+ it 'should have an attribute :name from init' do
43
+ @foo.name.should == 'D1'
44
+ end
45
+ it 'should have an instance_variable for attribute :name' do
46
+ @foo.instance_variables.include?('@name').should == true
47
+ end
48
+ it 'should have an initialized class attribute :metadata' do
49
+ pending 'deciding how this should work'
50
+ Foo.metadata.should == 'D2'
51
+ end
52
+ it 'should have an initialized singleton attribute :special' do
53
+ pending 'deciding how this should work'
54
+ @foo.special.should == 'D3'
55
+ end
56
+ end
57
+ raise_if_defined :Foo
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.2
4
+ version: 0.0.3
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-08 00:00:00 +00:00
12
+ date: 2008-03-12 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -22,6 +22,7 @@ extensions: []
22
22
  extra_rdoc_files:
23
23
  - README
24
24
  - COPYING
25
+ - ChangeLog
25
26
  files:
26
27
  - lib/doodle.rb
27
28
  - lib/molic_orderedhash.rb
@@ -37,6 +38,7 @@ files:
37
38
  - examples/foo.rb
38
39
  - README
39
40
  - COPYING
41
+ - ChangeLog
40
42
  has_rdoc: true
41
43
  homepage: http://doodle.rubyforge.org/
42
44
  post_install_message:
@@ -62,7 +64,7 @@ rubyforge_project: doodle
62
64
  rubygems_version: 1.0.1
63
65
  signing_key:
64
66
  specification_version: 2
65
- summary: Declarative attribute definitions and validations
67
+ summary: Declarative attribute definitions, validations and conversions
66
68
  test_files:
67
69
  - spec/arg_order_spec.rb
68
70
  - spec/attributes_spec.rb
@@ -71,6 +73,7 @@ test_files:
71
73
  - spec/defaults_spec.rb
72
74
  - spec/doodle_spec.rb
73
75
  - spec/flatten_first_level_spec.rb
76
+ - spec/init_spec.rb
74
77
  - spec/required_spec.rb
75
78
  - spec/superclass_spec.rb
76
79
  - spec/validation_spec.rb