fixjour 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/fixjour.rb +3 -2
- data/lib/fixjour/builders.rb +35 -6
- data/lib/fixjour/errors.rb +2 -0
- data/lib/fixjour/verify.rb +1 -11
- metadata +1 -1
data/lib/fixjour.rb
CHANGED
@@ -8,7 +8,8 @@ require 'fixjour/errors'
|
|
8
8
|
require 'fixjour/builders'
|
9
9
|
|
10
10
|
# This method is just for prettiness
|
11
|
-
def Fixjour(&block)
|
11
|
+
def Fixjour(options={}, &block)
|
12
12
|
return Fixjour unless block_given?
|
13
|
-
Fixjour.
|
13
|
+
Fixjour.evaluate(&block)
|
14
|
+
Fixjour.verify! if options[:verify]
|
14
15
|
end
|
data/lib/fixjour/builders.rb
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
module Fixjour
|
2
2
|
class << self
|
3
|
+
def evaluate(&block)
|
4
|
+
begin
|
5
|
+
module_eval(&block)
|
6
|
+
rescue NameError => e
|
7
|
+
if evaluator.respond_to?(e.name)
|
8
|
+
raise NonBlockBuilderReference.new("You must use a builder block in order to reference other Fixjour creation methods.")
|
9
|
+
else
|
10
|
+
raise e
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
3
15
|
# The list of classes that have builders defined.
|
4
16
|
def builders
|
5
17
|
@builders ||= Set.new
|
@@ -16,12 +28,19 @@ module Fixjour
|
|
16
28
|
|
17
29
|
# This method should always return a valid instance of
|
18
30
|
# a model object.
|
19
|
-
def define_builder(klass, &block)
|
31
|
+
def define_builder(klass, options={}, &block)
|
20
32
|
add_builder(klass)
|
21
33
|
|
22
34
|
name = name_for(klass)
|
23
|
-
|
24
|
-
|
35
|
+
|
36
|
+
if block_given?
|
37
|
+
define_new(name, &block)
|
38
|
+
else
|
39
|
+
define_new(name) do |overrides|
|
40
|
+
klass.new(options.merge(overrides))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
25
44
|
define_create(name)
|
26
45
|
define_valid_attributes(name)
|
27
46
|
end
|
@@ -33,10 +52,11 @@ module Fixjour
|
|
33
52
|
end
|
34
53
|
|
35
54
|
# Defines the new_* method
|
36
|
-
def define_new(name, block)
|
55
|
+
def define_new(name, &block)
|
37
56
|
define_method("new_#{name}") do |*args|
|
38
57
|
overrides = args.first || { }
|
39
|
-
block.bind(self).call(overrides)
|
58
|
+
result = block.bind(self).call(overrides)
|
59
|
+
result
|
40
60
|
end
|
41
61
|
end
|
42
62
|
|
@@ -60,9 +80,18 @@ module Fixjour
|
|
60
80
|
|
61
81
|
overrides = args.extract_options!
|
62
82
|
attrs = instance_variable_get("@__valid_#{name}_attrs").merge(overrides)
|
83
|
+
attrs.stringify_keys!
|
63
84
|
attrs.make_indifferent!
|
64
85
|
attrs
|
65
86
|
end
|
66
87
|
end
|
88
|
+
|
89
|
+
def evaluator
|
90
|
+
@evaluator ||= begin
|
91
|
+
klass = Class.new
|
92
|
+
klass.send :include, self
|
93
|
+
klass.new
|
94
|
+
end
|
95
|
+
end
|
67
96
|
end
|
68
|
-
end
|
97
|
+
end
|
data/lib/fixjour/errors.rb
CHANGED
data/lib/fixjour/verify.rb
CHANGED
@@ -7,7 +7,7 @@ module Fixjour
|
|
7
7
|
# * The creation methods return objects of the proper type
|
8
8
|
def verify!
|
9
9
|
builders.each do |klass|
|
10
|
-
result =
|
10
|
+
result = evaluator.send("new_#{name_for(klass)}")
|
11
11
|
|
12
12
|
unless result.valid?
|
13
13
|
raise InvalidBuilder.new("The builder for #{klass} returns an invalid object")
|
@@ -22,15 +22,5 @@ module Fixjour
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
def validity_checker
|
29
|
-
@evaluator ||= begin
|
30
|
-
klass = Class.new
|
31
|
-
klass.send :include, self
|
32
|
-
klass.new
|
33
|
-
end
|
34
|
-
end
|
35
25
|
end
|
36
26
|
end
|