constructor 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,4 +3,6 @@ Manifest.txt
3
3
  README.txt
4
4
  Rakefile
5
5
  lib/constructor.rb
6
+ lib/constructor_struct.rb
6
7
  specs/constructor_spec.rb
8
+ specs/constructor_struct_spec.rb
@@ -1,4 +1,4 @@
1
- CONSTRUCTOR_VERSION = '1.0.2' #:nodoc:#
1
+ CONSTRUCTOR_VERSION = '1.0.3' #:nodoc:#
2
2
 
3
3
  class Class #:nodoc:#
4
4
  def constructor(*attrs, &block)
@@ -0,0 +1,33 @@
1
+ class ConstructorStruct
2
+ def self.new(*accessors, &block)
3
+ defaults = {:accessors => true, :strict => false}
4
+
5
+ accessor_names = accessors.dup
6
+ if accessors.last.is_a? Hash
7
+ accessor_names.pop
8
+ user_opts = accessors.last
9
+ user_opts.delete(:accessors)
10
+ defaults.each do |k,v|
11
+ user_opts[k] ||= v
12
+ end
13
+ else
14
+ accessors << defaults
15
+ end
16
+
17
+ Class.new do
18
+ constructor *accessors
19
+
20
+ class_eval(&block) if block
21
+
22
+ comparator_code = accessor_names.map { |fname| "self.#{fname} == o.#{fname}" }.join(" && ")
23
+ eval %|
24
+ def ==(o)
25
+ (self.class == o.class) && #{comparator_code}
26
+ end
27
+ def eql?(o)
28
+ (self.class == o.class) && #{comparator_code}
29
+ end
30
+ |
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,84 @@
1
+ require File.dirname(__FILE__) + '/../lib/constructor_struct'
2
+
3
+ describe ConstructorStruct, "#new" do
4
+ def struct(*accessors)
5
+ ConstructorStruct.new(*accessors)
6
+ end
7
+
8
+ def instance_of(clazz, args=nil)
9
+ args = [args] || []
10
+ clazz.new(*args)
11
+ end
12
+
13
+ before do
14
+ AClass = struct(:hello, :world) unless defined?(AClass)
15
+ end
16
+
17
+ it "creates a new class with accessors given a set of symbols or strings" do
18
+ instance_of(AClass, {:hello => "foo", :world => "bar"}).hello.should == "foo"
19
+ instance_of(AClass, {:hello => "foo", :world => "bar"}).world.should == "bar"
20
+ end
21
+
22
+ it "creates a real class" do
23
+ instance_of(AClass).class.should == AClass
24
+ end
25
+
26
+ it "has the option of creating a strict accessors" do
27
+ lambda { instance_of(struct(:foo, :strict => true)) }.should raise_error
28
+ end
29
+
30
+ it "does not have the option of not creating accessors" do
31
+ instance_of(struct(:foo, :accessors => false), :foo => "bar").foo.should == "bar"
32
+ end
33
+
34
+ describe "equivalence" do
35
+ before do
36
+ @hello = "Hello"
37
+ @world = "World"
38
+ @args = { :hello => @hello, :world => @world }
39
+ @target = AClass.new(@args)
40
+ end
41
+
42
+ it "uses all accessors" do
43
+ [ nil, :hello, :world ].each do |field_to_alter|
44
+ alt = AClass.new(:hello => @hello, :world => @world)
45
+
46
+ unless field_to_alter
47
+ # Base case: they should be equal
48
+ @target.should == alt
49
+ @target.eql?(alt).should be_true #should eql(alt)
50
+ else
51
+ # Change 1 field and see not equal
52
+ alt.send("#{field_to_alter}=", "other data")
53
+ @target.should_not == alt
54
+ @target.should_not eql(alt)
55
+ end
56
+ end
57
+ end
58
+
59
+ it "will not compare to another class with same fields" do
60
+ BClass = ConstructorStruct.new(:hello, :world)
61
+ alt = BClass.new(:hello => @hello, :world => @world)
62
+ @target.should_not == alt
63
+ @target.should_not eql(alt)
64
+ end
65
+ end
66
+
67
+ describe "extra method definitions" do
68
+ NightTrain = ConstructorStruct.new(:beer, :conductor) do
69
+ def setup
70
+ @conductor ||= "Bill"
71
+ end
72
+ end
73
+
74
+ it "lets you declare instance methods within a block" do
75
+ night_train = NightTrain.new(:beer => "Founders")
76
+ night_train.beer.should == "Founders"
77
+ night_train.conductor.should == "Bill"
78
+
79
+ other_train = NightTrain.new(:beer => "Bells", :conductor => "Dave")
80
+ other_train.conductor.should == "Dave"
81
+ end
82
+ end
83
+
84
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: constructor
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Atomic Object
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-01 01:00:00 -04:00
12
+ date: 2009-11-30 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -42,7 +42,9 @@ files:
42
42
  - README.txt
43
43
  - Rakefile
44
44
  - lib/constructor.rb
45
+ - lib/constructor_struct.rb
45
46
  - specs/constructor_spec.rb
47
+ - specs/constructor_struct_spec.rb
46
48
  has_rdoc: true
47
49
  homepage: http://atomicobjectrb.rubyforge.org/constructor
48
50
  licenses: []