doodle 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,5 +1,8 @@
1
1
  = ChangeLog for doodle
2
2
 
3
+ == 0.0.10 / 2008-04-13
4
+ - fixed bug in class attribute initialization
5
+
3
6
  == 0.0.9 / 2008-04-12
4
7
 
5
8
  - new features:
@@ -8,8 +11,6 @@
8
11
  - use SaveBlock to distinguish between Proc and block args to :init
9
12
  - :collect now creates array by default if no :init specified (thanks to
10
13
  James Adam :)
11
- - attributes defined with collect now have :init => [] if no :init
12
- specified
13
14
  - :collect can now initialize from Enumerables by default
14
15
  - raise UnknownAttributeError if initialize called with unspecified attribute key
15
16
  - new examples:
@@ -30,7 +30,7 @@ if RUBY_VERSION < '1.8.6'
30
30
  end
31
31
 
32
32
  module Doodle
33
- VERSION = '0.0.9'
33
+ VERSION = '0.0.10'
34
34
  # where are we?
35
35
  class << self
36
36
  def context
@@ -99,6 +99,15 @@ module Doodle
99
99
  sc
100
100
  end
101
101
 
102
+ def is_class_self_defn?
103
+ defined?(ancestors) && ancestors.include?(Class)
104
+ end
105
+ def is_singleton_defn?
106
+ defined?(superclass) && superclass.ancestors.include?(Class) && !is_class_self_defn?
107
+ end
108
+ def is_instance_defn?
109
+ !is_class_self_defn? && !is_singleton_defn?
110
+ end
102
111
  end
103
112
 
104
113
  # provide an alternative inheritance chain that works for singleton
@@ -419,7 +428,7 @@ module Doodle
419
428
 
420
429
  # set an attribute by name - apply validation if defined
421
430
  def _setter(name, *args, &block)
422
- #pp [:_setter, self, self.class, name, args, block]
431
+ #pp [:_setter, self, self.class, name, args, block, caller]
423
432
  ivar = "@#{name}"
424
433
  if block_given?
425
434
  args.unshift(SaveBlock.new(block))
@@ -568,6 +577,7 @@ module Doodle
568
577
  # end
569
578
  #
570
579
  def has(*args, &block)
580
+ #what_am_i?([:has, args])
571
581
  Doodle::Debug.d { [:has, self, self.class, args] }
572
582
  name = args.shift.to_sym
573
583
  # d { [:has2, name, args] }
@@ -614,6 +624,19 @@ module Doodle
614
624
  end
615
625
  }
616
626
  end
627
+ if is_class_self_defn? or is_singleton_defn?
628
+ #pp [:args, args]
629
+ init_values = get_init_values(false)
630
+ # if init_values.size > 0
631
+ #p [:init_values, name, self, is_class_self_defn? ? :CLASS : is_singleton_defn? ? :SINGLETON : '?', init_values, methods(false)]
632
+ # #define_getter_setter(name)
633
+ # #instance_variable_set("@#{name}", init_values[name])
634
+ # end
635
+ # # singleton_class do
636
+ # #_setter(name, init_values[name])
637
+ # # end
638
+ end
639
+
617
640
  attribute
618
641
  end
619
642
 
@@ -638,6 +661,28 @@ module Doodle
638
661
  end
639
662
  end
640
663
 
664
+ def get_init_values(tf = true)
665
+ attributes(tf).select{|n, a| a.init_defined? }.inject({}) {|hash, (n, a)|
666
+ hash[n] = begin
667
+ case a.init
668
+ when NilClass, TrueClass, FalseClass, Fixnum
669
+ #p [:init, :no_clone]
670
+ a.init
671
+ when SaveBlock
672
+ #p [:init, :save_block]
673
+ instance_eval(&a.init.block)
674
+ else
675
+ #p [:init, :clone]
676
+ a.init.clone
677
+ end
678
+ rescue Exception => e
679
+ #p [:init, :rescue, e]
680
+ a.init
681
+ end
682
+ ; hash }
683
+ end
684
+ private :get_init_values
685
+
641
686
  # helper function to initialize from hash - this is safe to use
642
687
  # after initialization (validate! is called if this method is
643
688
  # called after initialization)
@@ -653,24 +698,7 @@ module Doodle
653
698
  # d { [:initialize, :arg_keywords, arg_keywords] }
654
699
 
655
700
  # set up initial values with ~clones~ of specified values (so not shared between instances)
656
- init_values = attributes.select{|n, a| a.init_defined? }.inject({}) {|hash, (n, a)|
657
- hash[n] = begin
658
- case a.init
659
- when NilClass, TrueClass, FalseClass, Fixnum
660
- #p [:init, :no_clone]
661
- a.init
662
- when SaveBlock
663
- #p [:init, :save_block]
664
- instance_eval(&a.init.block)
665
- else
666
- #p [:init, :clone]
667
- a.init.clone
668
- end
669
- rescue Exception => e
670
- #p [:init, :rescue, e]
671
- a.init
672
- end
673
- ; hash }
701
+ init_values = get_init_values
674
702
 
675
703
  # add to start of key_values array (so can be overridden by params)
676
704
  key_values.unshift(init_values)
@@ -712,11 +740,17 @@ module Doodle
712
740
  #Doodle::Debug.d { [:validate!, self] }
713
741
  #Doodle::Debug.d { [:validate!, self, __doodle__.validation_on] }
714
742
  if __doodle__.validation_on
715
- attributes.each do |name, att|
743
+ if self.class == Class
744
+ attribs = singleton_class.attributes
745
+ else
746
+ attribs = attributes
747
+ end
748
+ #pp [:validate!, self, self.class, attributes]
749
+ attribs.each do |name, att|
716
750
  # treat default as special case
717
- if att.name == :default || att.default_defined?
751
+ if [:default, :init].include?(att.name) || att.default_defined? || is_class_self_defn? || is_singleton_defn?
718
752
  # nop
719
- elsif !ivar_defined?(att.name)
753
+ elsif !ivar_defined?(att.name) && self.class != Class
720
754
  handle_error name, Doodle::ValidationError, "#{self} missing required attribute '#{name}'", [caller[-1]]
721
755
  end
722
756
  # if all == true, reset values so conversions and validations are applied to raw instance variables
@@ -1,4 +1,5 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper.rb'
2
+ require 'yaml'
2
3
 
3
4
  describe 'Doodle', 'parents' do
4
5
  temporary_constant :Foo do
@@ -78,3 +79,20 @@ describe 'Doodle', ' loading bad data from yaml' do
78
79
  end
79
80
  end
80
81
  end
82
+
83
+ describe Doodle, 'class attributes:' do
84
+ temporary_constant :Foo do
85
+ before :each do
86
+ class Foo < Doodle::Base
87
+ has :ivar
88
+ class << self
89
+ has :cvar
90
+ end
91
+ end
92
+ end
93
+
94
+ it 'should be possible to set a class var without setting an instance var' do
95
+ proc { Foo.cvar = 42 }.should_not raise_error
96
+ end
97
+ end
98
+ end
@@ -6,7 +6,7 @@ describe Doodle, 'init' do
6
6
  before(:each) do
7
7
  class Foo
8
8
  include Doodle::Helper
9
- has :name, :init => 'D1'
9
+ has :moniker, :init => 'D1'
10
10
  class << self
11
11
  has :metadata, :init => 'D2'
12
12
  end
@@ -16,12 +16,11 @@ describe Doodle, 'init' do
16
16
  has :special, :init => 'D3'
17
17
  end
18
18
  end
19
-
20
19
  it 'should have instance attribute init via class' do
21
- Foo.attributes[:name].init.should == 'D1'
20
+ Foo.attributes[:moniker].init.should == 'D1'
22
21
  end
23
22
  it 'should have instance attribute init via instance' do
24
- @foo.attributes[:name].init.should == 'D1'
23
+ @foo.attributes[:moniker].init.should == 'D1'
25
24
  end
26
25
  it 'should have class attribute init via class.singleton_class' do
27
26
  Foo.singleton_class.attributes(false)[:metadata].init.should == 'D2'
@@ -35,11 +34,11 @@ describe Doodle, 'init' do
35
34
  it 'should have singleton attribute init via instance.singleton_class' do
36
35
  @foo.singleton_class.attributes[:special].init.should == 'D3'
37
36
  end
38
- it 'should have an attribute :name from init' do
39
- @foo.name.should == 'D1'
37
+ it 'should have an attribute :moniker from init' do
38
+ @foo.moniker.should == 'D1'
40
39
  end
41
- it 'should have an instance_variable for attribute :name' do
42
- @foo.instance_variables.include?('@name').should == true
40
+ it 'should have an instance_variable for attribute :moniker' do
41
+ @foo.instance_variables.include?('@moniker').should == true
43
42
  end
44
43
  it 'should have an initialized class attribute :metadata' do
45
44
  pending 'deciding how this should work' do
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.9
4
+ version: 0.0.10
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-04-12 00:00:00 +01:00
12
+ date: 2008-04-13 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15