bluff 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +4 -0
- data/README.md +3 -1
- data/lib/bluff/builder.rb +3 -3
- data/lib/bluff/support/{backend → persistence}/active_record.rb +5 -5
- data/lib/bluff/support/persistence/base.rb +31 -0
- data/lib/bluff/support/support.rb +1 -1
- data/lib/bluff/version.rb +1 -1
- data/notes.md +36 -0
- data/spec/models/api_spec.rb +1 -1
- data/spec/support/bluffable_class.rb +6 -6
- metadata +11 -10
- data/lib/bluff/support/backend.rb +0 -21
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
## Jan 16 2012 - v0.1.0
|
2
|
+
|
3
|
+
* Migrated over to new persistence/adapter terminology
|
4
|
+
|
1
5
|
## Jan 12 2012 - v0.0.4
|
2
6
|
|
3
7
|
* Bluff calls are stricter now to give us more control in the bluff definitions. A bluff call accepts a single hash argument and default block values can no longer be defined in the block signatures.
|
data/README.md
CHANGED
@@ -86,12 +86,14 @@ Feel free to use them or huck em to the curb and just work directly with the att
|
|
86
86
|
|
87
87
|
```
|
88
88
|
User.bluff # returns an unsaved instance -- could also use Bluff.user
|
89
|
-
User.bluff! # returns a saved instance
|
89
|
+
User.bluff! # the "bang bluff" -- returns a persisted/saved instance, could also use Bluff.user!
|
90
90
|
User.bluff({:name => 'Ryan'}) # supply overrides/defaults in the attributes hash
|
91
91
|
```
|
92
92
|
|
93
93
|
### METHOD BLUFFS (MOCKS / STUBS)
|
94
94
|
|
95
|
+
** NOT IMPLEMENTED YET **
|
96
|
+
|
95
97
|
All objects and classes are given a `bluff()` method, which is
|
96
98
|
used for bluffing both models and methods. If given a symbol, the
|
97
99
|
`bluff()` method can be used to define method stubs as follows:
|
data/lib/bluff/builder.rb
CHANGED
@@ -3,8 +3,8 @@ require 'active_support/core_ext/array/wrap'
|
|
3
3
|
require 'active_support/core_ext/array/conversions'
|
4
4
|
require 'active_support/core_ext/hash/indifferent_access'
|
5
5
|
|
6
|
-
require 'bluff/support/
|
7
|
-
require 'bluff/support/
|
6
|
+
require 'bluff/support/persistence/base'
|
7
|
+
require 'bluff/support/persistence/active_record'
|
8
8
|
|
9
9
|
module Bluff
|
10
10
|
module Builder
|
@@ -97,7 +97,7 @@ module Bluff
|
|
97
97
|
def define_bluff_bang(field)
|
98
98
|
define_singleton_method "#{field}!" do |*arguments|
|
99
99
|
send(field, *arguments).tap do |record|
|
100
|
-
Bluff::Support
|
100
|
+
Bluff::Support.persist(record, {:as => field})
|
101
101
|
end
|
102
102
|
end
|
103
103
|
end
|
@@ -3,14 +3,14 @@ module ActiveRecord
|
|
3
3
|
end
|
4
4
|
end
|
5
5
|
|
6
|
-
module Bluff::Support::
|
7
|
-
class
|
6
|
+
module Bluff::Support::Persistence
|
7
|
+
class ActiveRecordAdapter
|
8
8
|
class << self
|
9
|
-
def
|
9
|
+
def persists?(record)
|
10
10
|
record.is_a?(ActiveRecord::Base)
|
11
11
|
end
|
12
12
|
|
13
|
-
def
|
13
|
+
def persist(record)
|
14
14
|
ActiveRecord::Base.transaction do
|
15
15
|
record.class.reflect_on_all_associations(:belongs_to).each do |reflection|
|
16
16
|
association = record.send(reflection.name)
|
@@ -22,6 +22,6 @@ module Bluff::Support::ActiveRecord
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
Bluff::Support::
|
25
|
+
Bluff::Support::Persistence::Base.register(self)
|
26
26
|
end
|
27
27
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Bluff
|
2
|
+
module Support
|
3
|
+
module Persistence
|
4
|
+
class Base
|
5
|
+
@@adapters = []
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def register(adapter)
|
9
|
+
@@adapters << adapter
|
10
|
+
end
|
11
|
+
|
12
|
+
def persist(record, options = {})
|
13
|
+
adapters = @@adapters.select {|adapter| adapter.persists?(record)}
|
14
|
+
|
15
|
+
if adapters.empty?
|
16
|
+
raise "#{options[:as] || record.class} is not persistable (#{record})"
|
17
|
+
else
|
18
|
+
# should first try to persist new associations
|
19
|
+
adapters.first.persist(record)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# just looks a little cleaner in use
|
27
|
+
def self.persist(record, options = {})
|
28
|
+
Persistence::Base.persist(record, options)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/bluff/version.rb
CHANGED
data/notes.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Notes
|
2
|
+
|
3
|
+
These notes are intended simply for my own use.
|
4
|
+
|
5
|
+
## Bang bluffs
|
6
|
+
|
7
|
+
If a bang bluff is called for a bluff that isn't persistable an error should be thrown.
|
8
|
+
|
9
|
+
If the bluff is persistable, all persistable associations within the bluffed object should
|
10
|
+
be persisted within a transaction (persisting from the inside out).
|
11
|
+
|
12
|
+
|
13
|
+
## Block argument reflection
|
14
|
+
|
15
|
+
Can use code from merb to reflect on expected block arguments and default values
|
16
|
+
|
17
|
+
```
|
18
|
+
require 'rubygems'
|
19
|
+
require 'merb'
|
20
|
+
|
21
|
+
include GetArgs
|
22
|
+
|
23
|
+
def foo(bar, zed=42)
|
24
|
+
end
|
25
|
+
|
26
|
+
method(:foo).get_args # => [[[:bar], [:zed, 42]], [:zed]]
|
27
|
+
```
|
28
|
+
|
29
|
+
Sourcify is another option but doesn't seem to support blocks with default arguments
|
30
|
+
|
31
|
+
Also have Proc#parameters but it doesn't look like it tells you what the default argument value is reported.
|
32
|
+
|
33
|
+
|
34
|
+
If we can find a way to do this we can loosen up the single-argument bluff restriction. Only bluffs without
|
35
|
+
predefined arguments and bluffs with a hash as the first argument will be given special treatment.
|
36
|
+
|
data/spec/models/api_spec.rb
CHANGED
@@ -86,7 +86,7 @@ describe Bluff do
|
|
86
86
|
end
|
87
87
|
|
88
88
|
Bluff.for(:transient_class) { TransientClass.new }
|
89
|
-
lambda{ Bluff.transient_class! }.should raise_error(RuntimeError, /
|
89
|
+
lambda{ Bluff.transient_class! }.should raise_error(RuntimeError, /transient_class is not persistable/)
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
@@ -12,18 +12,18 @@ class BluffableClass
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
module Bluff::Support::
|
16
|
-
class
|
15
|
+
module Bluff::Support::Persistence
|
16
|
+
class BluffableClassAdapter
|
17
17
|
class << self
|
18
|
-
def
|
18
|
+
def persists?(record)
|
19
19
|
record.is_a?(BluffableClass)
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
22
|
+
def persist(record)
|
23
23
|
record.save
|
24
24
|
end
|
25
25
|
end
|
26
|
-
|
27
|
-
Bluff::Support::
|
26
|
+
|
27
|
+
Bluff::Support::Persistence::Base.register(self)
|
28
28
|
end
|
29
29
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bluff
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-01-
|
12
|
+
date: 2012-01-16 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &2156611420 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2156611420
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &2156610620 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '2.8'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2156610620
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: faker
|
38
|
-
requirement: &
|
38
|
+
requirement: &2156609620 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '1.0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2156609620
|
47
47
|
description: ''
|
48
48
|
email:
|
49
49
|
- ryan.mohr@gmail.com
|
@@ -61,10 +61,11 @@ files:
|
|
61
61
|
- lib/bluff/bluffs/data_bluffs.rb
|
62
62
|
- lib/bluff/builder.rb
|
63
63
|
- lib/bluff/configuration.rb
|
64
|
-
- lib/bluff/support/
|
65
|
-
- lib/bluff/support/
|
64
|
+
- lib/bluff/support/persistence/active_record.rb
|
65
|
+
- lib/bluff/support/persistence/base.rb
|
66
66
|
- lib/bluff/support/support.rb
|
67
67
|
- lib/bluff/version.rb
|
68
|
+
- notes.md
|
68
69
|
- spec/models/api_spec.rb
|
69
70
|
- spec/models/data_bluffs_spec.rb
|
70
71
|
- spec/models/extensions_spec.rb
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module Bluff::Support
|
2
|
-
class Backend
|
3
|
-
@@handlers = []
|
4
|
-
|
5
|
-
class << self
|
6
|
-
def register(handler)
|
7
|
-
@@handlers << handler
|
8
|
-
end
|
9
|
-
|
10
|
-
def save!(record, name)
|
11
|
-
handlers = @@handlers.select {|handler| handler.accepts?(record)}
|
12
|
-
|
13
|
-
if handlers.empty?
|
14
|
-
raise "#{name} cannot be bang bluffed (#{record})"
|
15
|
-
else
|
16
|
-
handlers.first.save!(record)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|