yaml-model 1.0.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/yaml-model.rb CHANGED
@@ -3,6 +3,13 @@ require 'yaml-model/version'
3
3
 
4
4
  class YAML_Model
5
5
 
6
+ def self.inherited( child )
7
+ child.instance_eval('@__attributes = {}')
8
+ child.define_singleton_method( :attributes ) do
9
+ @__attributes
10
+ end
11
+ end
12
+
6
13
  class Error < Exception
7
14
  end
8
15
 
@@ -25,45 +32,53 @@ class YAML_Model
25
32
  @@database[ :data ][ self.name ] ||= []
26
33
  end
27
34
 
28
- def assert( assertion, info )
35
+ def self.assert( assertion, info )
29
36
  raise Error.new( info.inspect ) unless assertion
30
37
  end
31
38
 
32
- def assert_type( variable, types )
39
+ def self.assert_type( variable, types )
33
40
  assert( [types].flatten.inject(false){|result,type|result||=(type===variable)}, "Invalid type: `#{variable.class.name}`" )
34
41
  end
35
42
 
36
- def self.type attr, types, &block
43
+ def self.type attr, types, options = {}, &block
44
+ @__attributes[ attr ] = [ types, options ]
37
45
  define_method attr do
38
46
  instance_eval "@#{attr}"
39
47
  end
40
48
  define_method "#{attr}=".to_sym do |value|
41
- assert_type value, types
49
+ YAML_Model.assert_type value, types
42
50
  instance_exec( value, &block ) if block_given?
43
51
  instance_eval "@#{attr} = value"
44
52
  end
45
- define_method "__assert_type__#{attr}" do
46
- assert_type( instance_eval( "@#{attr}" ), types )
53
+ define_singleton_method "__#{attr}__default".to_sym do
54
+ options[ :default ]
55
+ end
56
+ define_method "__#{attr}__assert_type" do
57
+ YAML_Model.assert_type( instance_eval( "@#{attr}" ), types )
47
58
  end
48
59
  end
49
60
 
50
61
  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 )
62
+ self.class.attributes.keys.each do |attribute|
63
+ instance_eval( "@#{attribute} ||= self.class.__#{attribute}__default" )
64
+ instance_eval( "__#{attribute}__assert_type" )
53
65
  end
54
66
  end
55
67
 
56
68
  def self.init *attributes, &block
57
69
  define_method :initialize do |*args|
58
70
  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
71
+
62
72
  attributes.each do |attribute|
63
73
  value = args.shift
64
74
  self.send( "#{attribute}=".to_sym, value )
65
75
  end
76
+
77
+ # FIXME For some reason commands within this block to set attribute values
78
+ # require the `self.` prefix or they just don't work
66
79
  self.instance_eval( &block ) if block_given?
80
+
81
+ super()
67
82
  end
68
83
  end
69
84
 
@@ -1,3 +1,3 @@
1
1
  class YAML_Model
2
- VERSION = "1.0.2"
2
+ VERSION = "1.2.0"
3
3
  end
data/test/init.rb CHANGED
@@ -11,7 +11,14 @@ describe YAML_Model, "::init" do
11
11
  class CanSetInstanceVariables < YAML_Model
12
12
  type :name, String
13
13
  init :name do
14
- @name = "Bar"
14
+ self.name = "Bar"
15
+ end
16
+ end
17
+
18
+ class SettingOfDefaultValues < YAML_Model
19
+ type :name, String
20
+ init do
21
+ self.name = "Bob"
15
22
  end
16
23
  end
17
24
 
@@ -26,9 +33,14 @@ describe YAML_Model, "::init" do
26
33
  $ran_it.should == true
27
34
  end
28
35
 
29
- it "can set instance varibles inside blocks it's given" do
36
+ it "can set instance variables inside blocks it's given" do
30
37
  test = CanSetInstanceVariables.create( "Foo" )
31
38
  test.name.should == "Bar"
32
39
  end
33
40
 
41
+ it "allows setting of default values" do
42
+ test = SettingOfDefaultValues.create
43
+ test.name.should == "Bob"
44
+ end
45
+
34
46
  end
data/test/type.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'yaml-model'
2
+
3
+ describe YAML_Model, "::type" do
4
+
5
+ before( :each ) do
6
+ YAML_Model.reset!
7
+ end
8
+
9
+ class ShouldCreateClassMethodsForAttributeDefaultValues_nil < YAML_Model
10
+ type :name, String
11
+ end
12
+
13
+ class ShouldCreateClassMethodsForAttributeDefaultValues_Bob < YAML_Model
14
+ type :name, String, :default => 'Bob'
15
+ end
16
+
17
+ it "should create class methods for attribute default values" do
18
+ ShouldCreateClassMethodsForAttributeDefaultValues_nil.respond_to?( :__name__default ).should == true
19
+ ShouldCreateClassMethodsForAttributeDefaultValues_nil.__name__default.should == nil
20
+ lambda{ShouldCreateClassMethodsForAttributeDefaultValues_nil.create}.should raise_error YAML_Model::Error
21
+ ShouldCreateClassMethodsForAttributeDefaultValues_Bob.respond_to?( :__name__default ).should == true
22
+ ShouldCreateClassMethodsForAttributeDefaultValues_Bob.__name__default.should == 'Bob'
23
+ lambda{ShouldCreateClassMethodsForAttributeDefaultValues_Bob.create}.should_not raise_error YAML_Model::Error
24
+ ShouldCreateClassMethodsForAttributeDefaultValues_Bob.create.name.should == ShouldCreateClassMethodsForAttributeDefaultValues_Bob.__name__default
25
+ end
26
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
- - 0
8
7
  - 2
9
- version: 1.0.2
8
+ - 0
9
+ version: 1.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Clive Crous
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-27 00:00:00 +02:00
17
+ date: 2011-01-31 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -80,6 +80,7 @@ files:
80
80
  - test/create.rb
81
81
  - test/has.rb
82
82
  - test/init.rb
83
+ - test/type.rb
83
84
  - yaml-model.gemspec
84
85
  has_rdoc: true
85
86
  homepage: http://www.darkarts.co.za/yaml-model
@@ -95,7 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
96
  requirements:
96
97
  - - ">="
97
98
  - !ruby/object:Gem::Version
98
- hash: 321677229
99
+ hash: -869954371
99
100
  segments:
100
101
  - 0
101
102
  version: "0"
@@ -104,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
105
  requirements:
105
106
  - - ">="
106
107
  - !ruby/object:Gem::Version
107
- hash: 321677229
108
+ hash: -869954371
108
109
  segments:
109
110
  - 0
110
111
  version: "0"