fixjour 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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.module_eval(&block)
13
+ Fixjour.evaluate(&block)
14
+ Fixjour.verify! if options[:verify]
14
15
  end
@@ -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
- define_new(name, block)
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
@@ -11,4 +11,6 @@ module Fixjour
11
11
 
12
12
  # Raised when a builder block saves the object.
13
13
  class BuilderSavedRecord < StandardError; end
14
+
15
+ class NonBlockBuilderReference < StandardError; end
14
16
  end
@@ -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 = validity_checker.send("new_#{name_for(klass)}")
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fixjour
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pat Nakajima