pharrell 0.2.0 → 0.3.0
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.
- checksums.yaml +4 -4
- data/README.md +62 -3
- data/lib/pharrell.rb +12 -7
- data/lib/pharrell/config.rb +23 -37
- data/pharrell.gemspec +1 -1
- data/spec/config_spec.rb +11 -12
- data/spec/pharrell_spec.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: cb4bf66bc2f1c6dc17040e0b81c9bbed25ad9341
|
|
4
|
+
data.tar.gz: e363140c7471601cb9ae3b608266bb18abc8c4f6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 95ee5a87bbad1cceab0c63b0e7ab4cb9597ba42d9230b4a948eb111d4252323f7c628511c9e69051436f07901c01809a5880923bd08529c1ee15c71312789dc7
|
|
7
|
+
data.tar.gz: 78b2c79462d36dd6961f07f9df22f06367f1258069016d839446d16e9e68a3e309941933f0edee60e7a9b19a82550c2d8248b35a3a10ce6caf4c051844e0b7c8
|
data/README.md
CHANGED
|
@@ -1,7 +1,66 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pharrell
|
|
2
|
+
|
|
3
|
+
[](https://travis-ci.org/seadowg/pharrell)
|
|
2
4
|
|
|
3
5
|

|
|
4
6
|
|
|
5
|
-
##
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Either include in your Gemfile:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'pharrell'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Or, install for your system:
|
|
16
|
+
|
|
17
|
+
> gem install pharrell
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
You can inject dependencies into classes like so:
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
class CurrentTime
|
|
25
|
+
include Pharrell::Injectible
|
|
26
|
+
|
|
27
|
+
inject :current_time, Time
|
|
28
|
+
|
|
29
|
+
def to_s
|
|
30
|
+
hour = current_time.hour % 12
|
|
31
|
+
"The time is #{hour == 0 ? 12 : hour} o'clock"
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The values injected are dependent on the current conifguration:
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
Pharrell.config(:base) do |config|
|
|
40
|
+
config.bind(Time) { Time.now }
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
Pharrell.use_config(:base)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
You can use configurations to change the injected values in different
|
|
47
|
+
scenarios. For instance, we can use Pharrell to help us test `CurrentTime` class
|
|
48
|
+
defined above:
|
|
49
|
+
|
|
50
|
+
```ruby
|
|
51
|
+
require 'minitest/autorun'
|
|
52
|
+
|
|
53
|
+
Pharrell.config(:test) do |config|
|
|
54
|
+
config.bind(Time, Time.at(0))
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
describe "CurrentTime" do
|
|
58
|
+
before do
|
|
59
|
+
Pharrell.use_config(:test)
|
|
60
|
+
end
|
|
6
61
|
|
|
7
|
-
|
|
62
|
+
it "displays the time of day" do
|
|
63
|
+
assert_equal(CurrentTime.new.to_s, "The time is 12 o'clock")
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
```
|
data/lib/pharrell.rb
CHANGED
|
@@ -5,20 +5,25 @@ module Pharrell
|
|
|
5
5
|
@@configs = {}
|
|
6
6
|
@@config = nil
|
|
7
7
|
|
|
8
|
-
def self.instance_for(klass)
|
|
9
|
-
@@configs[@@config].instance_for(klass)
|
|
10
|
-
end
|
|
11
|
-
|
|
12
8
|
def self.config(name, &blk)
|
|
13
|
-
@@configs[name] = Config.new
|
|
14
|
-
blk.call(@@configs[name])
|
|
9
|
+
@@configs[name] = Config.new(blk)
|
|
15
10
|
end
|
|
16
11
|
|
|
17
12
|
def self.use_config(name)
|
|
18
13
|
@@config = name
|
|
19
14
|
end
|
|
20
15
|
|
|
16
|
+
def self.instance_for(klass)
|
|
17
|
+
current_config.instance_for(klass)
|
|
18
|
+
end
|
|
19
|
+
|
|
21
20
|
def self.rebuild!
|
|
22
|
-
|
|
21
|
+
current_config.rebuild!
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def self.current_config
|
|
27
|
+
@@configs[@@config]
|
|
23
28
|
end
|
|
24
29
|
end
|
data/lib/pharrell/config.rb
CHANGED
|
@@ -1,56 +1,42 @@
|
|
|
1
1
|
module Pharrell
|
|
2
2
|
class Config
|
|
3
|
-
def initialize
|
|
4
|
-
@
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
def bind(klass, arg = nil, &blk)
|
|
8
|
-
if blk
|
|
9
|
-
options = arg.kind_of?(Hash) ? arg : {}
|
|
10
|
-
@map[klass] = ObjectGenerator.new(options, &blk)
|
|
11
|
-
else
|
|
12
|
-
@map[klass] = arg
|
|
13
|
-
end
|
|
3
|
+
def initialize(definition)
|
|
4
|
+
@definition = definition
|
|
5
|
+
rebuild!
|
|
14
6
|
end
|
|
15
7
|
|
|
16
8
|
def instance_for(klass)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if instance_or_class.is_a? Class
|
|
20
|
-
instance_or_class.new
|
|
21
|
-
elsif instance_or_class.is_a? ObjectGenerator
|
|
22
|
-
instance_or_class.fetch
|
|
23
|
-
else
|
|
24
|
-
instance_or_class
|
|
25
|
-
end
|
|
9
|
+
@bindings[klass].call
|
|
26
10
|
end
|
|
27
11
|
|
|
28
12
|
def rebuild!
|
|
29
|
-
@
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
end
|
|
33
|
-
end
|
|
13
|
+
@bindings = Binder.new.tap { |b|
|
|
14
|
+
@definition.call(b)
|
|
15
|
+
}.bindings
|
|
34
16
|
end
|
|
35
17
|
|
|
36
18
|
private
|
|
37
19
|
|
|
38
|
-
class
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
20
|
+
class Binder
|
|
21
|
+
attr_reader :bindings
|
|
22
|
+
|
|
23
|
+
def initialize
|
|
24
|
+
@bindings = {}
|
|
42
25
|
end
|
|
43
26
|
|
|
44
|
-
def
|
|
45
|
-
if
|
|
46
|
-
|
|
27
|
+
def bind(klass, arg = nil, &blk)
|
|
28
|
+
if blk
|
|
29
|
+
options = arg.kind_of?(Hash) ? arg : {}
|
|
30
|
+
@bindings[klass] = blk
|
|
47
31
|
else
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
32
|
+
obj_block = if arg.kind_of?(Class)
|
|
33
|
+
Proc.new { arg.new }
|
|
34
|
+
else
|
|
35
|
+
Proc.new { arg }
|
|
36
|
+
end
|
|
51
37
|
|
|
52
|
-
|
|
53
|
-
|
|
38
|
+
@bindings[klass] = obj_block
|
|
39
|
+
end
|
|
54
40
|
end
|
|
55
41
|
end
|
|
56
42
|
end
|
data/pharrell.gemspec
CHANGED
data/spec/config_spec.rb
CHANGED
|
@@ -4,29 +4,28 @@ require 'pharrell'
|
|
|
4
4
|
describe "Pharrell::Config" do
|
|
5
5
|
describe "#instance_for" do
|
|
6
6
|
it "returns an instance bound to a class" do
|
|
7
|
-
config = Pharrell::Config.new
|
|
8
|
-
|
|
7
|
+
config = Pharrell::Config.new(Proc.new { |c|
|
|
8
|
+
c.bind(Time, Time.at(0))
|
|
9
|
+
})
|
|
10
|
+
|
|
9
11
|
assert_equal(config.instance_for(Time), Time.at(0))
|
|
10
12
|
end
|
|
11
13
|
|
|
12
14
|
it "returns an instance created from a class bound to a class" do
|
|
13
|
-
config = Pharrell::Config.new
|
|
14
15
|
my_class = Class.new
|
|
16
|
+
config = Pharrell::Config.new(Proc.new { |c|
|
|
17
|
+
c.bind(String, my_class)
|
|
18
|
+
})
|
|
15
19
|
|
|
16
|
-
config.bind(String, my_class)
|
|
17
20
|
assert_equal(config.instance_for(String).class, my_class)
|
|
18
21
|
end
|
|
19
22
|
|
|
20
23
|
it "allows a lazy instance to be passed as a block" do
|
|
21
|
-
config = Pharrell::Config.new
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
end
|
|
24
|
+
config = Pharrell::Config.new(Proc.new { |c|
|
|
25
|
+
c.bind(Object) { Object.new }
|
|
26
|
+
})
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
config = Pharrell::Config.new
|
|
28
|
-
config.bind(Object, :cache => true) { Object.new }
|
|
29
|
-
assert_equal(config.instance_for(Object), config.instance_for(Object))
|
|
28
|
+
assert(config.instance_for(Object) != config.instance_for(Object))
|
|
30
29
|
end
|
|
31
30
|
end
|
|
32
31
|
end
|
data/spec/pharrell_spec.rb
CHANGED
|
@@ -21,9 +21,9 @@ describe "Pharrell" do
|
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
describe ".rebuild!" do
|
|
24
|
-
it "forces all
|
|
24
|
+
it "forces all bound values to be rebuilt" do
|
|
25
25
|
Pharrell.config(:main) do |config|
|
|
26
|
-
config.bind(Object,
|
|
26
|
+
config.bind(Object, Object.new)
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
Pharrell.use_config(:main)
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: pharrell
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Callum Stott
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2013-08-
|
|
11
|
+
date: 2013-08-07 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description:
|
|
14
14
|
email:
|