pharrell 0.5.2 → 0.6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2d7ddc117eb0a25d90d07260f0513d189d7d5a17
4
- data.tar.gz: a55df73b6d261a3dca3132678a866fe62c9478de
3
+ metadata.gz: 7eed3d692ced9756edd084b008e2d43df2f12f08
4
+ data.tar.gz: f01316c10483a6bb8adbe4c942bee5361db51a5f
5
5
  SHA512:
6
- metadata.gz: b64c018da3a1953b85300cc06b03fff0d1f45b05028c6cb88a133de2c1fcd4abb6d4065d81fb5348f6a2d0fa633ddc8b67670803166aea5273de593cf2fd9337
7
- data.tar.gz: d05f135ab295b4d237dbf4b527158dfbdc06b926d8c3f2cdab31543f54d1782f09529c1f56a5a78461987a1f525fb6374834e6ebddfeb7d3045b3cbf09736ef9
6
+ metadata.gz: 5b95f8ac13597c151284ab8927e058fa6244a7368b0dfdfdd8845f7046f1478fb15880177a4aa0628745bc5e16208995a4f0229a77f2b1433df6bbb3c4618e2c
7
+ data.tar.gz: 3e722194ea21d0c2c1988902a21f9b48094f8a5354eb2c46c267f19f82a44e8888ada834806a9882fc24941a40a64cb4e3bfd623de9bf60ee37c531b68aba717
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ coverage/*
data/Gemfile CHANGED
@@ -4,4 +4,5 @@ gemspec
4
4
 
5
5
  group :test do
6
6
  gem 'rake'
7
+ gem 'coveralls'
7
8
  end
data/Gemfile.lock CHANGED
@@ -1,16 +1,37 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pharrell (0.5.2)
4
+ pharrell (0.6.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ coveralls (0.7.0)
10
+ multi_json (~> 1.3)
11
+ rest-client
12
+ simplecov (>= 0.7)
13
+ term-ansicolor
14
+ thor
15
+ docile (1.1.0)
16
+ mime-types (2.0)
17
+ multi_json (1.8.2)
9
18
  rake (10.1.0)
19
+ rest-client (1.6.7)
20
+ mime-types (>= 1.16)
21
+ simplecov (0.8.2)
22
+ docile (~> 1.1.0)
23
+ multi_json
24
+ simplecov-html (~> 0.8.0)
25
+ simplecov-html (0.8.0)
26
+ term-ansicolor (1.2.2)
27
+ tins (~> 0.8)
28
+ thor (0.18.1)
29
+ tins (0.13.1)
10
30
 
11
31
  PLATFORMS
12
32
  ruby
13
33
 
14
34
  DEPENDENCIES
35
+ coveralls
15
36
  pharrell!
16
37
  rake
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # pharrell
2
2
 
3
3
  [![Build Status](https://travis-ci.org/seadowg/pharrell.png?branch=master)](https://travis-ci.org/seadowg/pharrell)
4
+ [![Coverage Status](https://coveralls.io/repos/seadowg/pharrell/badge.png?branch=master)](https://coveralls.io/r/seadowg/pharrell?branch=master)
4
5
 
5
6
  ![Pharrell](media/pharrell.jpg)
6
7
 
@@ -99,7 +100,28 @@ Pharrell.use_config(:base) do |config|
99
100
  config.bind(Hash, { :key => "value" })
100
101
  end
101
102
 
102
- Pharrell.use_config(:more, :extend => :base) do |config|
103
+ Pharrell.use_config(:more, :extends => :base) do |config|
103
104
  config.bind(String, "This string instead")
104
105
  end
105
106
  ```
107
+ ### Using with Rails
108
+
109
+ Pharrell is really easy to set up with Rails. Here's an example similar to the `Time` example we used before:
110
+
111
+ **config/application.rb**
112
+
113
+ ```ruby
114
+ class Application < Rails::Application
115
+ config.to_prepare do
116
+ Pharrell.config do |config|
117
+ config.bind(Time, Time)
118
+ end
119
+
120
+ Pharrell.use_config(:base)
121
+ end
122
+ end
123
+ ```
124
+
125
+ Pharrell should be configured in the Rails Application's `#to_prepare` method as this runs at startup
126
+ in production and before each request in development. You can then attach your test configuration for Pharrell to
127
+ whatever test framework you use (exactly as in the first example).
data/Rakefile CHANGED
@@ -3,7 +3,7 @@ require 'rake/testtask'
3
3
  task :default => :test
4
4
 
5
5
  Rake::TestTask.new do |t|
6
- t.libs.push "lib"
6
+ t.libs << 'spec'
7
7
  t.test_files = FileList['spec/*_spec.rb']
8
8
  t.verbose = true
9
9
  end
@@ -2,11 +2,16 @@ module Pharrell
2
2
  class Config
3
3
  def initialize(definition)
4
4
  @definition = definition
5
- rebuild!
6
5
  end
7
6
 
8
7
  def instance_for(klass)
9
- @bindings[klass].call
8
+ binding = @bindings[klass]
9
+
10
+ if binding
11
+ binding.call(self)
12
+ else
13
+ raise BindingNotFoundError
14
+ end
10
15
  end
11
16
 
12
17
  def rebuild!
@@ -38,7 +43,7 @@ module Pharrell
38
43
  @bindings[klass] = blk
39
44
  else
40
45
  obj_block = if arg.kind_of?(Class)
41
- Proc.new { arg.new }
46
+ Proc.new { |config| build_instance(arg, config)}
42
47
  else
43
48
  Proc.new { arg }
44
49
  end
@@ -46,6 +51,20 @@ module Pharrell
46
51
  @bindings[klass] = obj_block
47
52
  end
48
53
  end
54
+
55
+ private
56
+
57
+ def build_instance(target_klass, config)
58
+ if target_klass.respond_to?(:__pharrell_constructor_classes)
59
+ args = target_klass.__pharrell_constructor_classes.map do |arg_klass|
60
+ config.instance_for(arg_klass)
61
+ end
62
+
63
+ target_klass.new(*args)
64
+ else
65
+ target_klass.new
66
+ end
67
+ end
49
68
  end
50
69
  end
51
70
  end
@@ -0,0 +1,19 @@
1
+ module Pharrell
2
+ module Constructor
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ @@constructor = []
9
+
10
+ def constructor(*klasses)
11
+ @@constructor = klasses
12
+ end
13
+
14
+ def __pharrell_constructor_classes
15
+ @@constructor
16
+ end
17
+ end
18
+ end
19
+ end
data/lib/pharrell.rb CHANGED
@@ -1,13 +1,38 @@
1
1
  require 'pharrell/config'
2
2
  require 'pharrell/injectable'
3
+ require 'pharrell/constructor'
3
4
 
4
5
  module Pharrell
6
+ class BindingNotFoundError < Exception
7
+ def message
8
+ "Binding could not be found!"
9
+ end
10
+ end
11
+
12
+ class ConfigNotDefinedError < Exception
13
+ def message
14
+ "Config has not been defined!"
15
+ end
16
+ end
17
+
18
+ class InvalidOptionsError < Exception
19
+ def initialize(opts)
20
+ @opts = opts
21
+ end
22
+
23
+ def message
24
+ "Invalid options: #{@opts.join(" ")}"
25
+ end
26
+ end
27
+
5
28
  @@configs = {}
6
29
  @@config = nil
7
30
 
8
31
  def self.config(name, opts = {}, &blk)
32
+ check_options([:extends], opts)
33
+
9
34
  if opts[:extends]
10
- @@configs[name] = @@configs[opts[:extends]].extend(blk)
35
+ @@configs[name] = fetch_config(opts[:extends]).extend(blk)
11
36
  else
12
37
  @@configs[name] = Config.new(blk)
13
38
  end
@@ -25,10 +50,35 @@ module Pharrell
25
50
  def self.rebuild!
26
51
  current_config.rebuild!
27
52
  end
53
+
54
+ def self.reset!
55
+ @@configs = {}
56
+ @@config = nil
57
+ end
28
58
 
29
59
  private
30
60
 
31
61
  def self.current_config
32
- @@configs[@@config]
62
+ fetch_config(@@config)
63
+ end
64
+
65
+ def self.fetch_config(name)
66
+ config = @@configs[name]
67
+
68
+ if config
69
+ config
70
+ else
71
+ raise ConfigNotDefinedError
72
+ end
73
+ end
74
+
75
+ def self.check_options(opts, opt_hash)
76
+ extra_keys = opt_hash.keys.reject { |key| opts.include?(key) }
77
+
78
+ if extra_keys.empty?
79
+ true
80
+ else
81
+ raise InvalidOptionsError.new(extra_keys)
82
+ end
33
83
  end
34
84
  end
data/pharrell.gemspec CHANGED
@@ -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.5.2"
6
+ s.version = "0.6.1"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Callum Stott"]
9
9
  s.email = ["callum@seadowg.com"]
data/spec/config_spec.rb CHANGED
@@ -1,13 +1,19 @@
1
+ require 'spec_helper'
1
2
  require 'minitest/autorun'
2
3
  require 'pharrell'
3
4
 
4
5
  describe "Pharrell::Config" do
6
+ before do
7
+ Pharrell.reset!
8
+ end
9
+
5
10
  describe "#instance_for" do
6
11
  it "returns an instance bound to a class" do
7
12
  config = Pharrell::Config.new(Proc.new { |c|
8
13
  c.bind(Time, Time.at(0))
9
14
  })
10
-
15
+
16
+ config.rebuild!
11
17
  assert_equal(config.instance_for(Time), Time.at(0))
12
18
  end
13
19
 
@@ -17,6 +23,7 @@ describe "Pharrell::Config" do
17
23
  c.bind(String, my_class)
18
24
  })
19
25
 
26
+ config.rebuild!
20
27
  assert_equal(config.instance_for(String).class, my_class)
21
28
  end
22
29
 
@@ -25,6 +32,7 @@ describe "Pharrell::Config" do
25
32
  c.bind(Object) { Object.new }
26
33
  })
27
34
 
35
+ config.rebuild!
28
36
  assert(config.instance_for(Object) != config.instance_for(Object))
29
37
  end
30
38
  end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+ require 'minitest/autorun'
3
+ require 'pharrell'
4
+
5
+ describe "Constructor" do
6
+ before do
7
+ Pharrell.reset!
8
+ end
9
+
10
+ describe ".contructor" do
11
+ it "allows a class to be lazily created by Pharrell with the defined types" do
12
+ klass = Class.new {
13
+ include Pharrell::Constructor
14
+
15
+ attr_reader :object, :string
16
+ constructor Object, String
17
+
18
+ def initialize(object, string)
19
+ @object = object
20
+ @string = string
21
+ end
22
+ }
23
+
24
+ Pharrell.config(:base) { |c|
25
+ c.bind(String, "Hello")
26
+ c.bind(Object, 5)
27
+ c.bind(klass, klass)
28
+ }
29
+
30
+ Pharrell.use_config(:base)
31
+
32
+ assert_equal(Pharrell.instance_for(klass).object, 5)
33
+ assert_equal(Pharrell.instance_for(klass).string, "Hello")
34
+ end
35
+ end
36
+ end
@@ -1,7 +1,12 @@
1
+ require 'spec_helper'
1
2
  require 'minitest/autorun'
2
3
  require 'pharrell'
3
4
 
4
5
  describe "Injectable" do
6
+ before do
7
+ Pharrell.reset!
8
+ end
9
+
5
10
  describe ".inject" do
6
11
  it "defines a method to retrieve that class from Pharrell" do
7
12
  Pharrell.config(:base) { |c| c.bind(String, "Injected") }
@@ -1,7 +1,12 @@
1
+ require 'spec_helper'
1
2
  require 'minitest/autorun'
2
3
  require 'pharrell'
3
4
 
4
5
  describe "Pharrell" do
6
+ before do
7
+ Pharrell.reset!
8
+ end
9
+
5
10
  describe ".use_config" do
6
11
  it "switches Pharrell to use the specified config" do
7
12
  Pharrell.config(:test) do |config|
@@ -32,6 +37,15 @@ describe "Pharrell" do
32
37
 
33
38
  assert(first_instance != second_instance)
34
39
  end
40
+
41
+ it "raises a ConfigNotDefinedError if the config does not exist" do
42
+ begin
43
+ Pharrell.use_config(:main)
44
+ flunk(".use_config did not raise an error!")
45
+ rescue Pharrell::ConfigNotDefinedError => e
46
+ assert_equal("Config has not been defined!", e.message)
47
+ end
48
+ end
35
49
  end
36
50
 
37
51
  describe ".config" do
@@ -49,6 +63,24 @@ describe "Pharrell" do
49
63
  assert_equal(Pharrell.instance_for(Object), "Object")
50
64
  assert_equal(Pharrell.instance_for(String), "Hello, Test")
51
65
  end
66
+
67
+ it "raises a ConfigNotDefinedError if the extended config does not exist" do
68
+ begin
69
+ Pharrell.config(:sub, :extends => :base)
70
+ flunk(".config did not raise an error!")
71
+ rescue Pharrell::ConfigNotDefinedError => e
72
+ assert_equal("Config has not been defined!", e.message)
73
+ end
74
+ end
75
+
76
+ it "raises an InvalidOptionsError for invalid options" do
77
+ begin
78
+ Pharrell.config(:base, :wrong_opt => "Value", :other_opt => "Value") { |config| }
79
+ flunk(".config did not raise an error!")
80
+ rescue Pharrell::InvalidOptionsError => e
81
+ assert_equal("Invalid options: wrong_opt other_opt", e.message)
82
+ end
83
+ end
52
84
  end
53
85
 
54
86
  describe ".rebuild!" do
@@ -64,4 +96,18 @@ describe "Pharrell" do
64
96
  assert(Pharrell.instance_for(Object) != original)
65
97
  end
66
98
  end
99
+
100
+ describe ".instance_for" do
101
+ it "raises a BindingNotFoundError if there is no binding for the instance" do
102
+ Pharrell.config(:main) { |config| }
103
+ Pharrell.use_config(:main)
104
+
105
+ begin
106
+ Pharrell.instance_for(Object)
107
+ flunk(".instance_for did not raise an error!")
108
+ rescue Pharrell::BindingNotFoundError => e
109
+ assert_equal("Binding could not be found!", e.message)
110
+ end
111
+ end
112
+ end
67
113
  end
@@ -0,0 +1,9 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter do |source_file|
4
+ source_file.filename =~ /spec/
5
+ end
6
+ end
7
+
8
+ require 'coveralls'
9
+ Coveralls.wear!
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.5.2
4
+ version: 0.6.1
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-11-13 00:00:00.000000000 Z
11
+ date: 2013-11-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -17,6 +17,7 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - .gitignore
20
21
  - .travis.yml
21
22
  - Gemfile
22
23
  - Gemfile.lock
@@ -25,12 +26,15 @@ files:
25
26
  - Rakefile
26
27
  - lib/pharrell.rb
27
28
  - lib/pharrell/config.rb
29
+ - lib/pharrell/constructor.rb
28
30
  - lib/pharrell/injectable.rb
29
31
  - media/pharrell.jpg
30
32
  - pharrell.gemspec
31
33
  - spec/config_spec.rb
34
+ - spec/constructor_spec.rb
32
35
  - spec/injectable_spec.rb
33
36
  - spec/pharrell_spec.rb
37
+ - spec/spec_helper.rb
34
38
  homepage: http://github.com/seadowg/pharrell
35
39
  licenses:
36
40
  - MIT