hassox-machinist 0.1.7 → 0.1.7.1

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/machinist.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'active_support'
2
2
  require 'sham'
3
- require File.join(File.dirname(__FILE__), "adapters", "abstract")
3
+ require File.join(File.dirname(__FILE__), "machinist", "adapters", "abstract")
4
4
 
5
5
  module Machinist
6
6
  # :api: private
@@ -85,4 +85,4 @@ module Machinist
85
85
  end
86
86
  end
87
87
 
88
- require File.join(File.dirname(__FILE__), 'adapters/active_record') if defined?(ActiveRecord)
88
+ require File.join(File.dirname(__FILE__), 'machinist/adapters/active_record') if defined?(ActiveRecord)
@@ -0,0 +1,36 @@
1
+ module Machinist
2
+ class AbstractAdapter
3
+ # :api: private
4
+ def self.inherited(klass)
5
+ Machinist.add_adapter klass
6
+ end
7
+
8
+ # Use this to set the priority of your adapter
9
+ # If you have a very specific test to see if this adapter should be used in
10
+ # use_for_class? then the priority should be set at 1.0
11
+ # but if it is ambigious, then it should be set low.
12
+ # A test for a plain ruby object will return true always for example, so
13
+ # the priority should be set lower to give the other adapters a chance to match
14
+ # :api: plugin
15
+ def self.priority(num = nil)
16
+ if num
17
+ @priority = num.to_f
18
+ else
19
+ @priority ||= 0.0
20
+ end
21
+ end
22
+
23
+ # Put code to save the object in here. For PORO (plain old ruby objects) this
24
+ # can just be blank
25
+ # :api: overwritable
26
+ def self.save(obj)
27
+ end
28
+
29
+ # A test to see if this adapter should be used for the specified class.
30
+ # If the test is ambiguous, then it should be given a low priority
31
+ # :api: overwritable
32
+ def self.use_for_class?(obj)
33
+ raise "Need to add a use_for_class? method in your #{self.class} adapter"
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,20 @@
1
+ require 'active_record'
2
+
3
+ module Machinist
4
+ class ActiveRecordAdapter < AbstractAdapter
5
+ priority 0.5 # Medium Priority
6
+ def self.use_for_class?(klass)
7
+ klass.ancestors.include?(ActiveRecord::Base)
8
+ end
9
+
10
+ def self.save(obj)
11
+ obj.save!
12
+ obj.reload
13
+ obj
14
+ end
15
+ end
16
+ end
17
+
18
+ class ActiveRecord::Base
19
+ include Machinist::Extensions
20
+ end
@@ -0,0 +1,17 @@
1
+ require 'dm-core'
2
+ module Machinist
3
+ class DataMapperAdapter < AbstractAdapter
4
+ priority 0.5 # Medium Priority
5
+ def self.use_for_class?(klass)
6
+ !!(DataMapper::Resource > klass)
7
+ end
8
+
9
+ def self.save(obj)
10
+ obj.save
11
+ obj.reload
12
+ obj
13
+ end
14
+ end
15
+ end
16
+
17
+ DataMapper::Model.append_extensions(Machinist::Extensions::ClassMethods) if defined?(DataMapper)
@@ -0,0 +1,12 @@
1
+ module Machinist
2
+ class ActiveRecordAdapter < AbstractAdapter
3
+ priority 0 # Least priority
4
+
5
+ def self.use_for_class?(klass)
6
+ true
7
+ end
8
+
9
+ def self.save(obj)
10
+ end
11
+ end
12
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hassox-machinist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pete Yandell
@@ -24,6 +24,10 @@ extra_rdoc_files: []
24
24
  files:
25
25
  - lib/machinist.rb
26
26
  - lib/sham.rb
27
+ - lib/machinist/adapters/abstract.rb
28
+ - lib/machinist/adapters/active_record.rb
29
+ - lib/machinist/adapters/datamapper.rb
30
+ - lib/machinist/adapters/poro.rb
27
31
  has_rdoc: false
28
32
  homepage: http://github.com/notahat/machinist
29
33
  post_install_message: