yaml-model 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/yaml-model.rb CHANGED
@@ -33,21 +33,35 @@ class YAML_Model
33
33
  assert( [types].flatten.inject(false){|result,type|result||=(type===variable)}, "Invalid type: `#{variable.class.name}`" )
34
34
  end
35
35
 
36
- def self.type attribute, types, &block
37
- define_method attribute do
38
- instance_eval "@#{attribute}"
36
+ def self.type attr, types, &block
37
+ define_method attr do
38
+ instance_eval "@#{attr}"
39
39
  end
40
- define_method "#{attribute}=".to_sym do |value|
40
+ define_method "#{attr}=".to_sym do |value|
41
41
  assert_type value, types
42
42
  instance_exec( value, &block ) if block_given?
43
- instance_eval "@#{attribute} = value"
43
+ instance_eval "@#{attr} = value"
44
+ end
45
+ define_method "__assert_type__#{attr}" do
46
+ assert_type( instance_eval( "@#{attr}" ), types )
47
+ end
48
+ end
49
+
50
+ def initialize
51
+ self.methods.select{|n|n.to_s=~/^__assert_type__/}.map{|n|n.to_s.gsub(/^__assert_type__(.+)$/,'\1').to_sym}.each do |attribute|
52
+ self.send( "__assert_type__#{attribute}".to_sym )
44
53
  end
45
54
  end
46
55
 
47
56
  def self.init *attributes, &block
48
57
  define_method :initialize do |*args|
58
+ raise ArgumentError.new( "wrong number of arguments (#{args.size} for #{attributes.size}" ) unless args.size == attributes.size
59
+ (self.methods.select{|n|n.to_s=~/^__assert_type__/}.map{|n|n.to_s.gsub(/^__assert_type__(.+)$/,'\1').to_sym}-attributes).each do |attribute|
60
+ self.send( "__assert_type__#{attribute}".to_sym )
61
+ end
49
62
  attributes.each do |attribute|
50
- self.send( "#{attribute}=".to_sym, args.shift )
63
+ value = args.shift
64
+ self.send( "#{attribute}=".to_sym, value )
51
65
  end
52
66
  self.instance_eval( &block ) if block_given?
53
67
  end
@@ -133,14 +147,13 @@ class YAML_Model
133
147
  this_attribute_singular = this_class_name.downcase.to_sym
134
148
  that_attribute_singular = that_class_name.downcase.to_sym
135
149
  via_class_name = [ this_class_name, that_class_name ].sort.map{|n|n.capitalize}.join('')
136
- via_class = eval( "#{via_class_name}||=Class.new(YAML_Model)" )
150
+ via_class = eval( "#{via_class_name}||=Class.new(YAML_Model) do
151
+ type :#{this_attribute_singular}, #{this_class}
152
+ type :#{that_attribute_singular}, #{that_class}
153
+ init #{[this_attribute_singular,that_attribute_singular].sort.map{|n|":#{n}"}.join(',')}
154
+ end" )
137
155
  via_attribute_plural = ( via_class_name.downcase + "s" ).to_sym
138
156
 
139
- if via_class.instance_variables.empty?
140
- via_class.type this_attribute_singular, this_class
141
- via_class.type that_attribute_singular, that_class
142
- end
143
-
144
157
  this_class.has via_attribute_plural, via_class
145
158
 
146
159
  define_method that_attribute_plural do
@@ -150,7 +163,7 @@ class YAML_Model
150
163
  end
151
164
 
152
165
  define_method "add_#{that_attribute_singular}".to_sym do |that_instance|
153
- via_instance = via_class.create
166
+ via_instance = ( ( this_attribute_singular < that_attribute_singular ) ? via_class.create( self, that_instance ) : via_class.create( that_instance, self ) )
154
167
  via_instance.send( "#{this_attribute_singular}=".to_sym, self )
155
168
  via_instance.send( "#{that_attribute_singular}=".to_sym, that_instance )
156
169
  end
@@ -1,3 +1,3 @@
1
1
  class YAML_Model
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
data/test/create.rb ADDED
@@ -0,0 +1,32 @@
1
+ require 'yaml-model'
2
+
3
+ describe YAML_Model, "::create" do
4
+
5
+ class BadPerson < YAML_Model
6
+ type :name, String
7
+ end
8
+
9
+ class Person < YAML_Model
10
+ type :name, String
11
+ init :name
12
+ end
13
+
14
+ before( :each ) do
15
+ YAML_Model.reset!
16
+ end
17
+
18
+ it "ensures attribute types on creation" do
19
+ lambda{ BadPerson.create }.should raise_error( YAML_Model::Error )
20
+ lambda{ Person.create( "Bob" ) }.should_not raise_error( YAML_Model::Error )
21
+ end
22
+
23
+ it "ensures correct amount of arguments given" do
24
+ lambda{ BadPerson.create( "Bob" ) }.should raise_error( ArgumentError )
25
+ lambda{ Person.create( "Bob" ) }.should_not raise_error( ArgumentError )
26
+ lambda{ BadPerson.create }.should_not raise_error( ArgumentError )
27
+ lambda{ Person.create }.should raise_error( ArgumentError )
28
+ lambda{ Person.create( "Bob", "Smith" ) }.should raise_error( ArgumentError )
29
+ end
30
+
31
+ end
32
+
data/test/has.rb CHANGED
@@ -2,29 +2,33 @@ require 'yaml-model'
2
2
 
3
3
  describe YAML_Model, "::has" do
4
4
 
5
+ User = Class.new( YAML_Model )
6
+ Post = Class.new( YAML_Model )
7
+ Tag = Class.new( YAML_Model )
8
+
9
+ class Post < YAML_Model
10
+ type :user, User
11
+ init :user
12
+ has :tags, Tag, :many_to_many
13
+ end
14
+
15
+ class User < YAML_Model
16
+ has :posts, Post
17
+ end
18
+
19
+ class Tag < YAML_Model
20
+ has :posts, Post, :many_to_many
21
+ end
22
+
5
23
  before( :each ) do
6
24
  YAML_Model.reset!
7
25
  end
8
26
 
9
27
  it "creates a method of the correct attribute name" do
10
- User = Class.new( YAML_Model )
11
- class User < YAML_Model
12
- has :posts, Class.new( YAML_Model )
13
- end
14
28
  User.instance_methods.index( :posts ).should_not == nil
15
29
  end
16
30
 
17
31
  it "correctly references items that belong to it" do
18
- User = Class.new( YAML_Model )
19
- Post = Class.new( YAML_Model )
20
- class Post < YAML_Model
21
- type :user, User
22
- init :user
23
- end
24
- class User < YAML_Model
25
- has :posts, Post
26
- end
27
-
28
32
  user_a = User.create
29
33
  user_b = User.create
30
34
  user_c = User.create
@@ -38,24 +42,16 @@ describe YAML_Model, "::has" do
38
42
  end
39
43
 
40
44
  it "adds an add_ method when the relationship is many_to_many" do
41
- Tag = Class.new( YAML_Model )
42
- Post = Class.new( YAML_Model )
43
- class Post < YAML_Model
44
- has :tags, Tag, :many_to_many
45
- end
46
45
  Post.instance_methods.index( :add_tag ).should_not == nil
47
- Tag.instance_methods.index( :add_post ).should == nil
46
+ Tag.instance_methods.index( :add_post ).should_not == nil
48
47
  end
49
48
 
50
49
  it "handles many to many relationships seamlessly" do
51
- Tag = Class.new( YAML_Model )
52
- Post = Class.new( YAML_Model )
53
- Post.has :tags, Tag, :many_to_many
54
- Tag.has :posts, Post, :many_to_many
55
-
56
- post_a = Post.create
57
- post_b = Post.create
58
- post_c = Post.create
50
+ dummy_user = User.create
51
+
52
+ post_a = Post.create( dummy_user )
53
+ post_b = Post.create( dummy_user )
54
+ post_c = Post.create( dummy_user )
59
55
 
60
56
  tag_a = Tag.create
61
57
  tag_b = Tag.create
data/test/init.rb CHANGED
@@ -2,32 +2,32 @@ require 'yaml-model'
2
2
 
3
3
  describe YAML_Model, "::init" do
4
4
 
5
+ class RunsTheBlockItsGiven < YAML_Model
6
+ init do
7
+ $ran_it = true
8
+ end
9
+ end
10
+
11
+ class CanSetInstanceVariables < YAML_Model
12
+ type :name, String
13
+ init :name do
14
+ @name = "Bar"
15
+ end
16
+ end
17
+
5
18
  before( :each ) do
6
19
  YAML_Model.reset!
7
20
  end
8
21
 
9
22
  it "runs the block it's given" do
10
23
  $ran_it = false
11
- Test = Class.new( YAML_Model )
12
- class Test < YAML_Model
13
- init do
14
- $ran_it = true
15
- end
16
- end
17
24
  $ran_it.should == false
18
- Test.create
25
+ RunsTheBlockItsGiven.create
19
26
  $ran_it.should == true
20
27
  end
21
28
 
22
29
  it "can set instance varibles inside blocks it's given" do
23
- Test = Class.new( YAML_Model )
24
- class Test < YAML_Model
25
- type :name, String
26
- init :name do
27
- @name = "Bar"
28
- end
29
- end
30
- test = Test.create( "Foo" )
30
+ test = CanSetInstanceVariables.create( "Foo" )
31
31
  test.name.should == "Bar"
32
32
  end
33
33
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 1
9
- version: 1.0.1
8
+ - 2
9
+ version: 1.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Clive Crous
@@ -77,6 +77,7 @@ files:
77
77
  - Rakefile
78
78
  - lib/yaml-model.rb
79
79
  - lib/yaml-model/version.rb
80
+ - test/create.rb
80
81
  - test/has.rb
81
82
  - test/init.rb
82
83
  - yaml-model.gemspec
@@ -94,7 +95,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
95
  requirements:
95
96
  - - ">="
96
97
  - !ruby/object:Gem::Version
97
- hash: -961508909
98
+ hash: 321677229
98
99
  segments:
99
100
  - 0
100
101
  version: "0"
@@ -103,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
104
  requirements:
104
105
  - - ">="
105
106
  - !ruby/object:Gem::Version
106
- hash: -961508909
107
+ hash: 321677229
107
108
  segments:
108
109
  - 0
109
110
  version: "0"