machinist 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/machinist.rb +5 -1
- data/lib/machinist/active_record.rb +1 -0
- data/lib/machinist/sequel.rb +62 -0
- metadata +3 -2
data/lib/machinist.rb
CHANGED
@@ -38,7 +38,7 @@ module Machinist
|
|
38
38
|
if attribute_assigned?(symbol)
|
39
39
|
# If we've already assigned the attribute, return that.
|
40
40
|
@object.send(symbol)
|
41
|
-
elsif @adapter.has_association?(@object, symbol) &&
|
41
|
+
elsif @adapter.has_association?(@object, symbol) && !nil_or_empty?(@object.send(symbol))
|
42
42
|
# If the attribute is an association and is already assigned, return that.
|
43
43
|
@object.send(symbol)
|
44
44
|
else
|
@@ -58,6 +58,10 @@ module Machinist
|
|
58
58
|
|
59
59
|
private
|
60
60
|
|
61
|
+
def nil_or_empty?(object)
|
62
|
+
object.respond_to?(:empty?) ? object.empty? : object.nil?
|
63
|
+
end
|
64
|
+
|
61
65
|
def assign_attribute(key, value)
|
62
66
|
assigned_attributes[key.to_sym] = value
|
63
67
|
@object.send("#{key}=", value)
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'machinist'
|
2
|
+
require 'machinist/blueprints'
|
3
|
+
require 'sequel'
|
4
|
+
|
5
|
+
module Machinist
|
6
|
+
class SequelAdapter
|
7
|
+
def self.has_association?(object, attribute)
|
8
|
+
object.class.associations.include?(attribute)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.class_for_association(object, attribute)
|
12
|
+
object.class.association_reflection(attribute).associated_class
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.assigned_attributes_without_associations(lathe)
|
16
|
+
attributes = {}
|
17
|
+
lathe.assigned_attributes.each_pair do |attribute, value|
|
18
|
+
association = lathe.object.class.association_reflection(attribute)
|
19
|
+
if association && association[:type] == :many_to_one
|
20
|
+
key = association[:key] || association.default_key
|
21
|
+
attributes[key] = value.send(association.primary_key)
|
22
|
+
else
|
23
|
+
attributes[attribute] = value
|
24
|
+
end
|
25
|
+
end
|
26
|
+
attributes
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module SequelExtensions
|
31
|
+
def self.included(base)
|
32
|
+
base.extend(ClassMethods)
|
33
|
+
end
|
34
|
+
|
35
|
+
module ClassMethods
|
36
|
+
def make(*args, &block)
|
37
|
+
lathe = Lathe.run(Machinist::SequelAdapter, self.new, *args)
|
38
|
+
unless Machinist.nerfed?
|
39
|
+
lathe.object.save
|
40
|
+
lathe.object.refresh
|
41
|
+
end
|
42
|
+
lathe.object(&block)
|
43
|
+
end
|
44
|
+
|
45
|
+
def make_unsaved(*args)
|
46
|
+
returning(Machinist.with_save_nerfed { make(*args) }) do |object|
|
47
|
+
yield object if block_given?
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def plan(*args)
|
52
|
+
lathe = Lathe.run(Machinist::SequelAdapter, self.new, *args)
|
53
|
+
Machinist::SequelAdapter.assigned_attributes_without_associations(lathe)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class Sequel::Model
|
60
|
+
include Machinist::Blueprints
|
61
|
+
include Machinist::SequelExtensions
|
62
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: machinist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pete Yandell
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-10-
|
12
|
+
date: 2009-10-20 00:00:00 +11:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -28,6 +28,7 @@ files:
|
|
28
28
|
- lib/machinist/object.rb
|
29
29
|
- lib/machinist/active_record.rb
|
30
30
|
- lib/machinist/data_mapper.rb
|
31
|
+
- lib/machinist/sequel.rb
|
31
32
|
has_rdoc: true
|
32
33
|
homepage: http://github.com/notahat/machinist
|
33
34
|
licenses: []
|