bluff 0.0.2 → 0.0.3
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/CHANGELOG.md +6 -1
- data/lib/bluff/builder.rb +6 -11
- data/lib/bluff/support/active_record.rb +25 -0
- data/lib/bluff/support/backend.rb +21 -0
- data/lib/bluff/support/support.rb +16 -0
- data/lib/bluff/version.rb +1 -1
- data/spec/models/api_spec.rb +26 -0
- data/spec/models/extensions_spec.rb +12 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/support/bluffable_class.rb +29 -0
- data/spec/support/matchers/be_bluffable.rb +19 -0
- data/spec/support/matchers/{bluff.rb → bluff_for.rb} +0 -0
- metadata +18 -9
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
## Jan 11 2012 - v0.0.3
|
2
|
+
|
3
|
+
* Added architecture for multiple backend/orm support (only activerecord provided so far)
|
4
|
+
* Fixed bug with bluff extensions not locating existing classes correctly
|
5
|
+
|
1
6
|
## Jan 11 2012
|
2
7
|
|
3
|
-
* Added initial implementation with a small set of built-in data bluffs
|
8
|
+
* Added initial implementation with a small set of built-in data bluffs
|
data/lib/bluff/builder.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'bluff/support/backend'
|
2
|
+
|
1
3
|
module Bluff
|
2
4
|
module Builder
|
3
5
|
module ClassMethods
|
@@ -36,21 +38,14 @@ module Bluff
|
|
36
38
|
def define_bluff_bang(field)
|
37
39
|
define_singleton_method "#{field}!" do |*args|
|
38
40
|
send(field, *args).tap do |record|
|
39
|
-
|
40
|
-
record.class.reflect_on_all_associations(:belongs_to).each do |reflection|
|
41
|
-
association = record.send(reflection.name)
|
42
|
-
association.save! if association && association.new_record?
|
43
|
-
end
|
44
|
-
|
45
|
-
record.save!
|
46
|
-
end
|
41
|
+
Bluff::Support::Backend.save!(record, field)
|
47
42
|
end
|
48
43
|
end
|
49
44
|
end
|
50
|
-
|
51
|
-
def extend_target(field, options)
|
52
|
-
class_name = options[:class_name] || field.to_s.classify
|
53
45
|
|
46
|
+
def extend_target(field, options)
|
47
|
+
class_name = options[:class_name] || field.to_s.camelize
|
48
|
+
|
54
49
|
if Kernel.const_defined?(class_name)
|
55
50
|
klass = Kernel.const_get(class_name)
|
56
51
|
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class ActiveRecord::Base
|
2
|
+
end
|
3
|
+
|
4
|
+
module Bluff::Support::ActiveRecord
|
5
|
+
class Backend
|
6
|
+
class << self
|
7
|
+
def accepts?(record)
|
8
|
+
record.is_a?(ActiveRecord::Base)
|
9
|
+
end
|
10
|
+
|
11
|
+
def save!(record)
|
12
|
+
ActiveRecord::Base.transaction do
|
13
|
+
record.class.reflect_on_all_associations(:belongs_to).each do |reflection|
|
14
|
+
association = record.send(reflection.name)
|
15
|
+
association.save! if association && association.new_record?
|
16
|
+
end
|
17
|
+
|
18
|
+
record.save!
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
Bluff::Support::Backend.register(self)
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,21 @@
|
|
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
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Here's how fabricator is autoloading fabrications
|
2
|
+
#
|
3
|
+
# def find_definitions
|
4
|
+
# path_prefix = defined?(Rails) ? Rails.root : "."
|
5
|
+
# Fabrication::Config.fabricator_dir.each do |folder|
|
6
|
+
# Dir.glob(File.join(path_prefix, folder, '**', '*fabricator.rb')).sort.each do |file|
|
7
|
+
# load file
|
8
|
+
# end
|
9
|
+
# end
|
10
|
+
# end
|
11
|
+
|
12
|
+
module Bluff
|
13
|
+
module Support
|
14
|
+
# autoload
|
15
|
+
end
|
16
|
+
end
|
data/lib/bluff/version.rb
CHANGED
data/spec/models/api_spec.rb
CHANGED
@@ -11,6 +11,32 @@ describe Bluff do
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
+
describe '.bluff' do
|
15
|
+
it 'returns an unsaved instance of the bluff' do
|
16
|
+
Bluff.for(:bluffable_class) { BluffableClass.new }
|
17
|
+
Bluff.bluffable_class.new_record?.should be_true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '.bluff!' do
|
22
|
+
context 'when the bluffed object is not saveable' do
|
23
|
+
it 'throws an error when called' do
|
24
|
+
class TransientClass
|
25
|
+
end
|
26
|
+
|
27
|
+
Bluff.for(:transient_class) { TransientClass.new }
|
28
|
+
lambda{ Bluff.transient_class! }.should raise_error(RuntimeError, /cannot be bang bluffed/)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'when the bluffed object is saveable' do
|
33
|
+
it 'returns the bluffed object' do
|
34
|
+
Bluff.for(:bluffable_class) { BluffableClass.new }
|
35
|
+
Bluff.bluffable_class!.new_record?.should be_false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
14
40
|
describe 'data' do
|
15
41
|
context 'when requesting a defined data bluff' do
|
16
42
|
bluffs = [
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Bluff do
|
4
|
+
describe '.for' do
|
5
|
+
context 'with an existing class' do
|
6
|
+
Bluff.for(:bluffable_class) { BluffableClass.new }
|
7
|
+
|
8
|
+
specify { BluffableClass.should be_bluffable }
|
9
|
+
specify { BluffableClass.should be_bluffable! }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -4,6 +4,7 @@ SPEC_ROOT = File.expand_path(File.dirname(__FILE__)) # ENV["RAILS_ENV"] ||= 'tes
|
|
4
4
|
# require 'rspec/rails'
|
5
5
|
|
6
6
|
require_relative '../lib/bluff.rb'
|
7
|
+
require_relative 'support/bluffable_class.rb'
|
7
8
|
|
8
9
|
# Requires supporting ruby files with custom matchers and macros, etc,
|
9
10
|
# in spec/support/ and its subdirectories.
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class BluffableClass
|
2
|
+
def initialize
|
3
|
+
@new_record = true
|
4
|
+
end
|
5
|
+
|
6
|
+
def save
|
7
|
+
@new_record = false
|
8
|
+
end
|
9
|
+
|
10
|
+
def new_record?
|
11
|
+
@new_record
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module Bluff::Support::BluffableClass
|
16
|
+
class Backend
|
17
|
+
class << self
|
18
|
+
def accepts?(record)
|
19
|
+
record.is_a?(BluffableClass)
|
20
|
+
end
|
21
|
+
|
22
|
+
def save!(record)
|
23
|
+
record.save
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
Bluff::Support::Backend.register(self)
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
RSpec::Matchers.define :be_bluffable do |expected|
|
2
|
+
match do |source|
|
3
|
+
begin
|
4
|
+
source.bluff
|
5
|
+
rescue
|
6
|
+
false
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
RSpec::Matchers.define :be_bluffable! do |expected|
|
12
|
+
match do |source|
|
13
|
+
begin
|
14
|
+
source.bluff!
|
15
|
+
rescue
|
16
|
+
false
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
File without changes
|
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.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-01-12 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &2164883740 !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: *2164883740
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &2164880180 !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: *2164880180
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: faker
|
38
|
-
requirement: &
|
38
|
+
requirement: &2164878820 !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: *2164878820
|
47
47
|
description: ''
|
48
48
|
email:
|
49
49
|
- ryan.mohr@gmail.com
|
@@ -61,11 +61,17 @@ 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/active_record.rb
|
65
|
+
- lib/bluff/support/backend.rb
|
66
|
+
- lib/bluff/support/support.rb
|
64
67
|
- lib/bluff/version.rb
|
65
68
|
- spec/models/api_spec.rb
|
66
69
|
- spec/models/data_bluffs_spec.rb
|
70
|
+
- spec/models/extensions_spec.rb
|
67
71
|
- spec/spec_helper.rb
|
68
|
-
- spec/support/
|
72
|
+
- spec/support/bluffable_class.rb
|
73
|
+
- spec/support/matchers/be_bluffable.rb
|
74
|
+
- spec/support/matchers/bluff_for.rb
|
69
75
|
- spec/support/matchers/bluff_instance_of.rb
|
70
76
|
homepage: https://github.com/islandr/bluff
|
71
77
|
licenses: []
|
@@ -94,6 +100,9 @@ summary: A single source of lies for all your testing needs
|
|
94
100
|
test_files:
|
95
101
|
- spec/models/api_spec.rb
|
96
102
|
- spec/models/data_bluffs_spec.rb
|
103
|
+
- spec/models/extensions_spec.rb
|
97
104
|
- spec/spec_helper.rb
|
98
|
-
- spec/support/
|
105
|
+
- spec/support/bluffable_class.rb
|
106
|
+
- spec/support/matchers/be_bluffable.rb
|
107
|
+
- spec/support/matchers/bluff_for.rb
|
99
108
|
- spec/support/matchers/bluff_instance_of.rb
|