fixjour 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,7 @@ module Fixjour
3
3
  # Defines the new_* method
4
4
  def define_new(klass, &block)
5
5
  define_method("new_#{name_for(klass)}") do |*args|
6
- Generator.new(klass, block).call(self, args.extract_options!)
6
+ Generator.new(klass, block).call(self, args.extract_options!.symbolize_keys!)
7
7
  end
8
8
  end
9
9
 
@@ -19,14 +19,10 @@ module Fixjour
19
19
  # Defines the valid_*_attributes method
20
20
  def define_valid_attributes(name)
21
21
  define_method("valid_#{name}_attributes") do |*args|
22
- if instance_variable_get("@__valid_#{name}_attrs").nil?
23
- valid_attributes = send("new_#{name}").attributes
24
- valid_attributes.delete_if { |key, value| value.nil? }
25
- instance_variable_set("@__valid_#{name}_attrs", valid_attributes)
26
- end
27
-
22
+ valid_attributes = send("new_#{name}").attributes
23
+ valid_attributes.delete_if { |key, value| value.nil? }
28
24
  overrides = args.extract_options!
29
- attrs = instance_variable_get("@__valid_#{name}_attrs").merge(overrides)
25
+ attrs = valid_attributes.merge(overrides)
30
26
  attrs.stringify_keys!
31
27
  attrs.make_indifferent!
32
28
  attrs
@@ -0,0 +1,24 @@
1
+ module Fixjour
2
+ module Deprecation
3
+ module MergingProxy
4
+ def process(*args)
5
+ raise DeprecatedMergeAttempt.new(<<-END
6
+ You are attempting to call process on the class proxy.
7
+ This behavior was recently deprecated. In order to process
8
+ the overrides hash, pass two block arguments:
9
+
10
+ define_builder(Foo) do |klass, overrides|
11
+ overrides.process(:bar) do |bar|
12
+ overrides[:bar] = 'overridden'
13
+ end
14
+
15
+ klass.new(:name => 'fizz')
16
+ end
17
+ END
18
+ )
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ Fixjour::MergingProxy.send :include, Fixjour::Deprecation::MergingProxy
@@ -23,4 +23,11 @@ module Fixjour
23
23
  # Raised when a builder is defined for a class that already
24
24
  # has one.
25
25
  class RedundantBuilder < StandardError; end
26
+
27
+ # Raised when a builder is defined with one block argument and
28
+ # the user assumes that it's the overrides hash. This used to
29
+ # be the standard behavior, but now blocks with one argument
30
+ # are passed the class proxy, and getting access to the overrides
31
+ # hash requires you pass two block arguments.
32
+ class DeprecatedMergeAttempt < StandardError; end
26
33
  end
@@ -19,7 +19,7 @@ module Fixjour
19
19
 
20
20
  def args(overrides)
21
21
  case block.arity
22
- when 1 then [overrides]
22
+ when 1 then [MergingProxy.new(klass, overrides)]
23
23
  when 2 then [MergingProxy.new(klass, overrides), overrides]
24
24
  end
25
25
  end
@@ -20,6 +20,7 @@ module Fixjour
20
20
  def new(defaults={})
21
21
  attrs = defaults.merge(@overrides)
22
22
  accessible, inaccessible = partition(attrs)
23
+
23
24
  returning @klass.new(accessible) do |instance|
24
25
  inaccessible.each do |key,val|
25
26
  instance.send("#{key}=", val)
data/lib/fixjour.rb CHANGED
@@ -12,6 +12,7 @@ require 'fixjour/errors'
12
12
  require 'fixjour/generator'
13
13
  require 'fixjour/definitions'
14
14
  require 'fixjour/builders'
15
+ require 'fixjour/deprecation'
15
16
 
16
17
  # This method is just for prettiness
17
18
  def Fixjour(options={}, &block)
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.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pat Nakajima
@@ -37,6 +37,7 @@ files:
37
37
  - lib/fixjour
38
38
  - lib/fixjour/builders.rb
39
39
  - lib/fixjour/definitions.rb
40
+ - lib/fixjour/deprecation.rb
40
41
  - lib/fixjour/errors.rb
41
42
  - lib/fixjour/generator.rb
42
43
  - lib/fixjour/merging_proxy.rb