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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5f574e1ba4e6bbec95f013b9cb5bc11d28c7a330
4
- data.tar.gz: 0b64c6fd5912373b0ff3815609e8430c715871f4
3
+ metadata.gz: cb4bf66bc2f1c6dc17040e0b81c9bbed25ad9341
4
+ data.tar.gz: e363140c7471601cb9ae3b608266bb18abc8c4f6
5
5
  SHA512:
6
- metadata.gz: e24bbe057e2a1722b81d4481f250fa3eb0f30f077f18fa82e5b5696038f01765a7f829fbffa1e7c248f4c2d784d6e262eea043ef756365b45510cb97491477d5
7
- data.tar.gz: 45482427f1dda47e6911736e58bea8c1954bcf1391fcf9bf8d544c93e5113f228e5be58a664a1be9964ce03592116054e5844acddb8d8fc4383d71dad3f60fbb
6
+ metadata.gz: 95ee5a87bbad1cceab0c63b0e7ab4cb9597ba42d9230b4a948eb111d4252323f7c628511c9e69051436f07901c01809a5880923bd08529c1ee15c71312789dc7
7
+ data.tar.gz: 78b2c79462d36dd6961f07f9df22f06367f1258069016d839446d16e9e68a3e309941933f0edee60e7a9b19a82550c2d8248b35a3a10ce6caf4c051844e0b7c8
data/README.md CHANGED
@@ -1,7 +1,66 @@
1
- # Pharrell
1
+ # pharrell
2
+
3
+ [![Build Status](https://travis-ci.org/seadowg/pharrell.png?branch=master)](https://travis-ci.org/seadowg/pharrell)
2
4
 
3
5
  ![Pharrell](media/pharrell.jpg)
4
6
 
5
- ## Description
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
- I'm a provider gurl.
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
+ ```
@@ -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
- @@configs[@@config].rebuild!
21
+ current_config.rebuild!
22
+ end
23
+
24
+ private
25
+
26
+ def self.current_config
27
+ @@configs[@@config]
23
28
  end
24
29
  end
@@ -1,56 +1,42 @@
1
1
  module Pharrell
2
2
  class Config
3
- def initialize
4
- @map = {}
5
- end
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
- instance_or_class = @map[klass]
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
- @map.each do |key, value|
30
- if value.is_a? ObjectGenerator
31
- value.invalidate!
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 ObjectGenerator
39
- def initialize(options = {}, &blk)
40
- @blk = blk
41
- @options = options
20
+ class Binder
21
+ attr_reader :bindings
22
+
23
+ def initialize
24
+ @bindings = {}
42
25
  end
43
26
 
44
- def fetch
45
- if @options[:cache]
46
- @value ||= @blk.call
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
- @blk.call
49
- end
50
- end
32
+ obj_block = if arg.kind_of?(Class)
33
+ Proc.new { arg.new }
34
+ else
35
+ Proc.new { arg }
36
+ end
51
37
 
52
- def invalidate!
53
- @value = nil
38
+ @bindings[klass] = obj_block
39
+ end
54
40
  end
55
41
  end
56
42
  end
@@ -3,7 +3,7 @@ $:.unshift lib unless $:.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "pharrell"
6
- s.version = "0.2.0"
6
+ s.version = "0.3.0"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Callum Stott"]
9
9
  s.email = ["callum@seadowg.com"]
@@ -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
- config.bind(Time, Time.at(0))
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
- config.bind(Object) { Object.new }
23
- assert(config.instance_for(Object) != config.instance_for(Object))
24
- end
24
+ config = Pharrell::Config.new(Proc.new { |c|
25
+ c.bind(Object) { Object.new }
26
+ })
25
27
 
26
- it "can cache block generated instances" do
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
@@ -21,9 +21,9 @@ describe "Pharrell" do
21
21
  end
22
22
 
23
23
  describe ".rebuild!" do
24
- it "forces all lazy values to be rebuilt" do
24
+ it "forces all bound values to be rebuilt" do
25
25
  Pharrell.config(:main) do |config|
26
- config.bind(Object, :cache => true) { Object.new }
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.2.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-02 00:00:00.000000000 Z
11
+ date: 2013-08-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: