nakajima-fixjour 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/core_ext/hash.rb +1 -1
- data/lib/core_ext/object.rb +1 -1
- data/lib/fixjour.rb +0 -1
- data/lib/fixjour/definitions.rb +28 -4
- data/lib/fixjour/deprecation.rb +1 -1
- data/lib/fixjour/errors.rb +8 -8
- data/lib/fixjour/generator.rb +5 -5
- data/lib/fixjour/merging_proxy.rb +8 -8
- data/lib/fixjour/overrides_hash.rb +3 -3
- metadata +21 -1
data/lib/core_ext/hash.rb
CHANGED
data/lib/core_ext/object.rb
CHANGED
data/lib/fixjour.rb
CHANGED
data/lib/fixjour/definitions.rb
CHANGED
@@ -6,7 +6,7 @@ module Fixjour
|
|
6
6
|
Generator.new(builder.klass, block).call(self, args.extract_options!.symbolize_keys!)
|
7
7
|
end
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
# Defines the create_* method
|
11
11
|
def define_create(builder)
|
12
12
|
define_method("create_#{builder.name}") do |*args|
|
@@ -15,16 +15,40 @@ module Fixjour
|
|
15
15
|
model
|
16
16
|
end
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
# Defines the valid_*_attributes method
|
20
20
|
def define_valid_attributes(builder)
|
21
21
|
define_method("valid_#{builder.name}_attributes") do |*args|
|
22
|
-
|
22
|
+
record = send("new_#{builder.name}", *args)
|
23
|
+
valid_attributes = record.attributes
|
23
24
|
valid_attributes.delete_if { |key, value| value.nil? }
|
25
|
+
|
26
|
+
transfer_singular_ids = proc { |reflection|
|
27
|
+
if associated = record.send(reflection.name)
|
28
|
+
associated.new_record? && associated.save!
|
29
|
+
key = reflection.options[:foreign_key] || reflection.name.to_s + '_id'
|
30
|
+
valid_attributes[key] = associated.id
|
31
|
+
end
|
32
|
+
}
|
33
|
+
|
34
|
+
record.class.reflect_on_all_associations(:has_one).each(&transfer_singular_ids)
|
35
|
+
record.class.reflect_on_all_associations(:belongs_to).each(&transfer_singular_ids)
|
36
|
+
|
37
|
+
transfer_plural_ids = proc { |reflection|
|
38
|
+
if associated = record.send(reflection.name)
|
39
|
+
associated.each { |rec| rec.new_record? && rec.save! }
|
40
|
+
key = reflection.options[:foreign_key] || reflection.name.to_s + '_id'
|
41
|
+
valid_attributes[key.to_s + 's'] = associated.map(&:id)
|
42
|
+
valid_attributes.delete(reflection.name)
|
43
|
+
end
|
44
|
+
}
|
45
|
+
|
46
|
+
record.class.reflect_on_all_associations(:has_many).each(&transfer_plural_ids)
|
47
|
+
|
24
48
|
valid_attributes.stringify_keys!
|
25
49
|
valid_attributes.make_indifferent!
|
26
50
|
valid_attributes
|
27
51
|
end
|
28
52
|
end
|
29
53
|
end
|
30
|
-
end
|
54
|
+
end
|
data/lib/fixjour/deprecation.rb
CHANGED
data/lib/fixjour/errors.rb
CHANGED
@@ -1,33 +1,33 @@
|
|
1
1
|
module Fixjour
|
2
2
|
# Raised when a builder returns an invalid object.
|
3
3
|
class InvalidBuilder < StandardError; end
|
4
|
-
|
4
|
+
|
5
5
|
# Raised when a builder will return an invalid object
|
6
6
|
# due to a validates_uniqueness_of validation.
|
7
7
|
class DangerousBuilder < StandardError; end
|
8
|
-
|
8
|
+
|
9
9
|
# Raised when a builder returns an object that cannot
|
10
10
|
# be saved the database.
|
11
11
|
class UnsavableBuilder < StandardError; end
|
12
|
-
|
12
|
+
|
13
13
|
# Raised when a builder returns an object of the wrong type
|
14
14
|
class WrongBuilderType < StandardError; end
|
15
|
-
|
15
|
+
|
16
16
|
# Raised when a builder block saves the object.
|
17
17
|
class BuilderSavedRecord < StandardError; end
|
18
|
-
|
18
|
+
|
19
19
|
# Raised when a Fixjour creation method is called in
|
20
20
|
# the wrong context.
|
21
21
|
class NonBlockBuilderReference < StandardError; end
|
22
|
-
|
22
|
+
|
23
23
|
# Raised when a builder is defined for a class that already
|
24
24
|
# has one.
|
25
25
|
class RedundantBuilder < StandardError; end
|
26
|
-
|
26
|
+
|
27
27
|
# Raised when a builder is defined with one block argument and
|
28
28
|
# the user assumes that it's the overrides hash. This used to
|
29
29
|
# be the standard behavior, but now blocks with one argument
|
30
30
|
# are passed the class proxy, and getting access to the overrides
|
31
31
|
# hash requires you pass two block arguments.
|
32
32
|
class DeprecatedMergeAttempt < StandardError; end
|
33
|
-
end
|
33
|
+
end
|
data/lib/fixjour/generator.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
module Fixjour
|
2
|
-
# This generates a new instance of a model object for
|
2
|
+
# This generates a new instance of a model object for
|
3
3
|
# the new_[model] method.
|
4
4
|
class Generator
|
5
5
|
attr_reader :klass, :block
|
6
|
-
|
6
|
+
|
7
7
|
def initialize(klass, block)
|
8
8
|
@klass, @block = klass, block
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
def call(context, overrides={})
|
12
12
|
overrides = OverridesHash.new(overrides)
|
13
13
|
result = block.bind(context).call(*args(overrides))
|
@@ -16,7 +16,7 @@ module Fixjour
|
|
16
16
|
else result
|
17
17
|
end
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def args(overrides)
|
21
21
|
case block.arity
|
22
22
|
when 1 then [MergingProxy.new(klass, overrides)]
|
@@ -24,4 +24,4 @@ module Fixjour
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
27
|
-
end
|
27
|
+
end
|
@@ -4,36 +4,36 @@ module Fixjour
|
|
4
4
|
# the #new method is called.
|
5
5
|
class MergingProxy
|
6
6
|
instance_methods.each { |m| undef_method(m) unless m =~ /__|inspect/ }
|
7
|
-
|
7
|
+
|
8
8
|
def initialize(klass, overrides)
|
9
9
|
@klass = klass
|
10
10
|
@overrides = overrides
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def protected(*attrs)
|
14
14
|
attrs = attrs.empty? ?
|
15
15
|
@protected :
|
16
16
|
@protected = attrs
|
17
17
|
Set.new(attrs)
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def new(defaults={})
|
21
21
|
attrs = defaults.merge(@overrides)
|
22
22
|
accessible, inaccessible = partition(attrs)
|
23
|
-
|
23
|
+
|
24
24
|
returning @klass.new(accessible) do |instance|
|
25
25
|
inaccessible.each do |key,val|
|
26
26
|
instance.send("#{key}=", val)
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
def method_missing(sym, *args, &block)
|
32
32
|
@klass.respond_to?(sym) ? @klass.send(sym, *args, &block) : super
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
private
|
36
|
-
|
36
|
+
|
37
37
|
def partition(attrs)
|
38
38
|
accessible = attrs.keys.inject({ }) do |m, key|
|
39
39
|
next m if protected.include?(key)
|
@@ -43,4 +43,4 @@ module Fixjour
|
|
43
43
|
[accessible, attrs]
|
44
44
|
end
|
45
45
|
end
|
46
|
-
end
|
46
|
+
end
|
@@ -4,11 +4,11 @@ module Fixjour
|
|
4
4
|
# method private and add the ability to process.
|
5
5
|
class OverridesHash < Hash
|
6
6
|
private :delete
|
7
|
-
|
7
|
+
|
8
8
|
def initialize(hash)
|
9
9
|
replace(hash)
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
# Allows for processing of the overrides hash. Deletes
|
13
13
|
# the option when it's present, then yields the value.
|
14
14
|
def process(option)
|
@@ -17,4 +17,4 @@ module Fixjour
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
20
|
-
end
|
20
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nakajima-fixjour
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pat Nakajima
|
@@ -22,6 +22,26 @@ dependencies:
|
|
22
22
|
- !ruby/object:Gem::Version
|
23
23
|
version: "0"
|
24
24
|
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: faker
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: acts_as_fu
|
37
|
+
type: :development
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
25
45
|
description:
|
26
46
|
email: patnakajima@gmail.com
|
27
47
|
executables: []
|