manioc 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5ae0a8881a567e6949babdf116daf2750b2b39c5
4
- data.tar.gz: 7b66abce35066e8cded1810693715f6522291ccd
3
+ metadata.gz: dcde3cb2d574036ff699dd0c8883d33bf1aea1ad
4
+ data.tar.gz: 7e117373e063b7bcbd5794fba2144042fca3b47b
5
5
  SHA512:
6
- metadata.gz: dbc1974df4940e51a88d1b6a7b4e20c4b0b5d2fb93310506986e2dcbb5b9a089d9bdf3181dda3826a5ea780a3f5ef49d2a44e87820cdda848672090fcbbec73a
7
- data.tar.gz: 2ad4e931946f817dfd5e930fc36f2d33c5e8867d040e3a2f502592755d5dc902863c4b5c4fa71f05713180cef669cf529666944a57ec475525e52e3f17e877a6
6
+ metadata.gz: c2f2737c1d0762eb76a4bba9666c820a4a207220a6e117b4fd29dbb66bc2310a8e348ec11b0651d078d06dca3afb82d95f7c6bf8bd81c92d0471586bc503d5d8
7
+ data.tar.gz: 4be8a31b032d705231c2848916301418eb60464df6df3e83697ccc65a2c1f8b013207163cffdf1d7fba2a88b059445e0cc5a4fc2976ce5e998379a0842388e7c
data/README.md CHANGED
@@ -36,6 +36,32 @@ prod = dev.with do
36
36
  end
37
37
  ```
38
38
 
39
+ ### Rails
40
+
41
+ Manioc includes a few convenience methods for the conventional use case of a Rails application with one container, customized per-environment:
42
+
43
+ ```ruby
44
+ # config/application.rb
45
+ module MyApp
46
+ class Applicaton
47
+ container do
48
+ foo { 0 }
49
+ bar { foo + 1 }
50
+ end
51
+ end
52
+ end
53
+
54
+ # config/environments/test.rb
55
+ Rails.application.configure do
56
+ container do
57
+ foo { 1000 }
58
+ end
59
+ end
60
+
61
+ # in e.g. a spec file
62
+ Rails.configuration.container.bar # => 1001
63
+ ```
64
+
39
65
  ## Installation
40
66
 
41
67
  Add this line to your application's Gemfile:
@@ -1,6 +1,6 @@
1
1
  module Manioc
2
2
  class Container
3
- class DSL
3
+ class DSL < BasicObject
4
4
  def initialize &block
5
5
  @fields = {}
6
6
  instance_exec(&block)
@@ -16,33 +16,30 @@ module Manioc
16
16
  end
17
17
  end
18
18
 
19
- def initialize cache: true, preload: false, constructors: {}, &block
20
- @constructors = constructors
21
- @cache = cache ? {} : nil
22
- @preload = preload
19
+ def initialize constructors: {}, &block
20
+ @constructors, @cache = constructors, {}
23
21
 
24
22
  register(&block) if block
25
23
  finalize
26
24
  end
27
25
 
28
26
  def with &block
29
- self.class.new \
30
- constructors: @constructors.dup,
31
- cache: !@cache.nil?,
32
- preload: @preload,
33
- &block
27
+ self.class.new constructors: @constructors.dup, &block
34
28
  end
35
29
 
36
30
  def clone
37
31
  with
38
32
  end
39
33
 
40
- def reset key=nil
41
- return unless @cache
42
- keys = key ? [key] : @cache.keys
34
+ def reset! *keys
35
+ keys = @cache.keys if keys.empty?
43
36
  keys.each { |k| @cache.delete k }
44
37
  end
45
38
 
39
+ def preload!
40
+ @constructors.keys.each { |key| resolve key }
41
+ end
42
+
46
43
  def inspect
47
44
  # :nocov:
48
45
  %|<#{self.class.name}(#{@constructors.keys.join(', ')})>|
@@ -56,21 +53,13 @@ module Manioc
56
53
  end
57
54
 
58
55
  def resolve key
59
- instance_exec(&@constructors[key])
56
+ @cache[key] ||= instance_exec(&@constructors[key])
60
57
  end
61
58
 
62
59
  def finalize
63
60
  @constructors.freeze
64
- @constructors.each do |key,_|
65
- if @cache
66
- define_singleton_method(key) { @cache[key] ||= resolve key }
67
- else
68
- define_singleton_method(key) { resolve key }
69
- end
70
-
71
- if @preload
72
- public_send key
73
- end
61
+ @constructors.keys.each do |key|
62
+ define_singleton_method(key) { resolve key }
74
63
  end
75
64
  end
76
65
  end
@@ -0,0 +1,31 @@
1
+ module Manioc
2
+ class Rails::Application
3
+ def self.container &setup
4
+ if setup
5
+ config.container = config.container.with(&setup)
6
+ else
7
+ config.container
8
+ end
9
+ end
10
+
11
+ def container &setup
12
+ self.class.container(&setup)
13
+ end
14
+ end
15
+
16
+ class Railtie < ::Rails::Railtie
17
+ _app = nil
18
+ config.before_configuration do |app|
19
+ _app = app
20
+ _app.config.container = Manioc::Container.new
21
+ end
22
+
23
+ config.to_prepare do
24
+ if _app.config.eager_load
25
+ _app.config.container.preload!
26
+ else
27
+ _app.config.container.reset!
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module Manioc
2
- VERSION = '0.1.3'
2
+ VERSION = '0.1.4'
3
3
  end
data/lib/manioc.rb CHANGED
@@ -2,5 +2,7 @@ require 'manioc/container'
2
2
  require 'manioc/struct'
3
3
  require 'manioc/version'
4
4
 
5
+ require 'manioc/railtie' if defined?(Rails::Railtie)
6
+
5
7
  module Manioc
6
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: manioc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Dabbs
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-04 00:00:00.000000000 Z
11
+ date: 2017-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -85,6 +85,7 @@ files:
85
85
  - bin/setup
86
86
  - lib/manioc.rb
87
87
  - lib/manioc/container.rb
88
+ - lib/manioc/railtie.rb
88
89
  - lib/manioc/struct.rb
89
90
  - lib/manioc/version.rb
90
91
  - manioc.gemspec