service_factory 0.0.1 → 0.0.2

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/README.md CHANGED
@@ -30,6 +30,11 @@ ServiceFactory.register do
30
30
  env :test, :development do
31
31
  user_info Fake::UserInfo
32
32
  end
33
+
34
+ memoize do
35
+ memoized_info { costly_operation }
36
+ memoized_class BigFatClass
37
+ end
33
38
  end
34
39
  ```
35
40
 
@@ -38,6 +43,7 @@ end
38
43
  ```ruby
39
44
  ServiceFactory.user_info("John Dou") #Same as UserInfo.new("John Dou") or Fake::UserInfo.new("John Dou")
40
45
  ServiceFactory.remote_data_service("http://service.com") #Same as RemoteData.new("http://service.com")
46
+ ServiceFactory.memoized_info #Runs only once
41
47
  ```
42
48
 
43
49
  See spec/service_factory_spec.rb for the full list of features
@@ -1,3 +1,3 @@
1
1
  module ServiceFactory
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -2,9 +2,8 @@ require "service_factory/version"
2
2
 
3
3
  module ServiceFactory
4
4
  @blocks = {}
5
-
6
5
  def register(&block)
7
- Builder.new(@blocks).instance_eval(&block)
6
+ Builder.new(@blocks).build(&block)
8
7
  end
9
8
  module_function :register
10
9
 
@@ -17,18 +16,37 @@ module ServiceFactory
17
16
  end
18
17
 
19
18
  class Builder
19
+ class Error < RuntimeError; end
20
+ class UnexpectedParams < Error; end
21
+
20
22
  def initialize(blocks)
21
23
  @blocks = blocks
24
+ @memoized_values = {}
25
+ @memoization = false
26
+ end
27
+
28
+ def build(&block)
29
+ instance_eval(&block)
30
+ @blocks
22
31
  end
23
32
 
24
33
  def method_missing(m, *args, &block)
34
+ prepared_block = args_to_block(args, &block)
35
+ if @memoization
36
+ @blocks[m] = Proc.new { @memoized_values[m] ||= prepared_block.call }
37
+ else
38
+ @blocks[m] = prepared_block
39
+ end
40
+ end
41
+
42
+ def args_to_block(args, &block)
25
43
  if block_given?
26
- @blocks[m] = block
44
+ block
27
45
  elsif args.first.is_a?(Class)
28
46
  cl = args.first
29
- @blocks[m] = Proc.new { |*class_args| cl.new(*class_args) }
47
+ Proc.new { |*class_args| cl.new(*class_args) }
30
48
  else
31
- super
49
+ raise UnexpectedParams.new("expected class or block")
32
50
  end
33
51
  end
34
52
 
@@ -37,5 +55,11 @@ module ServiceFactory
37
55
  instance_eval(&block)
38
56
  end
39
57
  end
58
+
59
+ def memoize(&block)
60
+ @memoization = true
61
+ instance_eval(&block)
62
+ @memoization = false
63
+ end
40
64
  end
41
65
  end
@@ -7,30 +7,76 @@ describe ServiceFactory do
7
7
  sample_var String
8
8
  sample_block { Object.new }
9
9
  sample_string { |s| String.new(s) }
10
+
11
+ memoize do
12
+ memo_var String
13
+ memo_block { Object.new }
14
+ end
15
+
16
+ var_after_memo { Object.new }
10
17
  end
11
18
  end
12
19
 
13
- context "when class given" do
14
- it "instantiates it every time" do
15
- ServiceFactory.sample_var.object_id.should_not == ServiceFactory.sample_var.object_id
16
- end
17
- it "instantiates it without args" do
18
- ServiceFactory.sample_var.should be_instance_of(String)
20
+ context 'when memoization is off' do
21
+ context "when class given" do
22
+ it "instantiates it every time" do
23
+ expect(ServiceFactory.sample_var.object_id).not_to eq(ServiceFactory.sample_var.object_id)
24
+ end
25
+ it "instantiates it without args" do
26
+ expect(ServiceFactory.sample_var).to be_instance_of(String)
27
+ end
28
+ it "instantiates it with args" do
29
+ expect(ServiceFactory.sample_var("sample")).to eq("sample")
30
+ end
19
31
  end
20
- it "instantiates it with args" do
21
- ServiceFactory.sample_var("sample").should == "sample"
32
+
33
+ context "when block given" do
34
+ it "executes block without args" do
35
+ expect(ServiceFactory.sample_block).to be_instance_of(Object)
36
+ end
37
+ it "executes block with args" do
38
+ expect(ServiceFactory.sample_string("sample")).to eq("sample")
39
+ end
40
+ it "executes block every time" do
41
+ expect(ServiceFactory.sample_block).not_to eq(ServiceFactory.sample_block)
42
+ end
22
43
  end
23
44
  end
24
45
 
25
- context "when block given" do
26
- it "executes block without args" do
27
- ServiceFactory.sample_block.should be_instance_of(Object)
46
+ context 'when memoization is on' do
47
+ context "when class given" do
48
+ it "instantiates it only once" do
49
+ expect(ServiceFactory.memo_var.object_id).to eq(ServiceFactory.memo_var.object_id)
50
+ end
51
+ it "instantiates it without args" do
52
+ expect(ServiceFactory.memo_var).to be_instance_of(String)
53
+ end
54
+ end
55
+
56
+ context "when block given" do
57
+ it "executes block only once" do
58
+ expect(ServiceFactory.memo_block).to eq(ServiceFactory.memo_block)
59
+ end
60
+ it "executes block without args" do
61
+ expect(ServiceFactory.memo_block).to be_instance_of(Object)
62
+ end
63
+ end
64
+
65
+ it "doesn't memoize values after the end of the block" do
66
+ expect(ServiceFactory.var_after_memo.object_id).not_to eq(ServiceFactory.var_after_memo.object_id)
28
67
  end
29
- it "executes block with args" do
30
- ServiceFactory.sample_string("sample").should == "sample"
68
+ end
69
+
70
+ context 'when register is called twice' do
71
+ before do
72
+ ServiceFactory.register do |i|
73
+ sample_var_double String
74
+ end
31
75
  end
32
- it "executes block every time" do
33
- ServiceFactory.sample_block.should_not == ServiceFactory.sample_block
76
+
77
+ it 'should answer to all defined methods' do
78
+ expect(ServiceFactory.sample_var).to eq("")
79
+ expect(ServiceFactory.sample_var_double).to eq("")
34
80
  end
35
81
  end
36
82
 
@@ -40,15 +86,16 @@ describe ServiceFactory do
40
86
  sample_var { "test2" }
41
87
  end
42
88
  end
43
- ServiceFactory.sample_var.should == "test2"
89
+ expect(ServiceFactory.sample_var).to eq("test2")
44
90
  end
45
91
 
46
92
  it "ignores data from another environment" do
47
93
  ServiceFactory.register do |i|
94
+ sample_var { "test1" }
48
95
  env :production, :development do
49
- sampleVar { "test2" }
96
+ sample_var { "test2" }
50
97
  end
51
98
  end
52
- ServiceFactory.sample_var.should_not == "test2"
99
+ expect(ServiceFactory.sample_var).not_to eq("test2")
53
100
  end
54
101
  end
data/spec/spec_helper.rb CHANGED
@@ -5,7 +5,6 @@
5
5
  #
6
6
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
7
  RSpec.configure do |config|
8
- config.treat_symbols_as_metadata_keys_with_true_values = true
9
8
  config.run_all_when_everything_filtered = true
10
9
  config.filter_run :focus
11
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: service_factory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-11 00:00:00.000000000 Z
12
+ date: 2015-06-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails