usable 3.5.0 → 3.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/CHANGELOG.md +6 -0
- data/README.md +25 -2
- data/lib/usable/config.rb +9 -2
- data/lib/usable/railtie.rb +3 -4
- data/lib/usable/struct.rb +0 -4
- data/lib/usable/version.rb +1 -1
- data/lib/usable.rb +14 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: acf388167647552fa9165b44809ff9bdd6476656
|
4
|
+
data.tar.gz: 8bb5bec7ddaab0870a58559b856da4bf833f749d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 15ddd01992f892bbd75ba9c21996a24aa36c2de2800fbca78d15c8a737b6718151a5bf0d5b94476353f0a5c3707cd708d1818a4233917366549b7a39e3e9db0c
|
7
|
+
data.tar.gz: 6bfeeabc1b77aef3abf2f2d59c0886106adc7ec0eff2878146da405a9197a5f2b3dee4a15b7a586dbe6f01f079199cfcaab64c723af31f95a2c5d2d88f437f74
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.
|
1
|
+
ruby-2.4.0
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -130,7 +130,15 @@ Mixin => MixinUsed
|
|
130
130
|
|
131
131
|
## Tips and Tricks
|
132
132
|
|
133
|
-
####
|
133
|
+
#### __since 3.6__
|
134
|
+
|
135
|
+
Eager-load and freeze usables in production Rails environments with the `frozen` setting:
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
config.usable_config.frozen = true
|
139
|
+
```
|
140
|
+
|
141
|
+
#### __since 3.4__
|
134
142
|
|
135
143
|
Import just a module's constants:
|
136
144
|
|
@@ -140,7 +148,7 @@ usable ExampleMod, only: :constants
|
|
140
148
|
|
141
149
|
Currently works with `usable ExampleMod, only: []` since version 2.0
|
142
150
|
|
143
|
-
#### __since
|
151
|
+
#### __since 3.3__ _- (not required)_
|
144
152
|
The `Usable::Struct` function is available for creating value objects with defaults. If you `require "usable/struct"` the
|
145
153
|
class function is available to create classes:
|
146
154
|
|
@@ -183,6 +191,21 @@ User.usables.human.speak # => "Hello"
|
|
183
191
|
User.usables.robot.speak # => "beep bop"
|
184
192
|
```
|
185
193
|
|
194
|
+
## Production
|
195
|
+
|
196
|
+
When running in production you may want to eager-load any lazily defined attributes and freeze them, ensuring thread safety.
|
197
|
+
Usable provides a [railtie](http://edgeguides.rubyonrails.org/configuring.html) that can be configured to freeze Usable and
|
198
|
+
and constants extended with Usable.
|
199
|
+
|
200
|
+
Enable the frozen setting:
|
201
|
+
```ruby
|
202
|
+
# config/production.rb
|
203
|
+
Acme::Application.configure do
|
204
|
+
# Freeze all +usables+ after initialize, eager-loading any lazily defined attributes
|
205
|
+
config.usable_config.frozen = true
|
206
|
+
end
|
207
|
+
```
|
208
|
+
|
186
209
|
## Development
|
187
210
|
|
188
211
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/usable/config.rb
CHANGED
@@ -37,6 +37,7 @@ module Usable
|
|
37
37
|
|
38
38
|
alias to_hash to_h
|
39
39
|
alias marshal_dump to_h
|
40
|
+
alias marshal_load initialize
|
40
41
|
|
41
42
|
def merge(other)
|
42
43
|
to_h.merge(other)
|
@@ -77,10 +78,16 @@ module Usable
|
|
77
78
|
super
|
78
79
|
end
|
79
80
|
|
80
|
-
def
|
81
|
-
|
81
|
+
def inspect
|
82
|
+
nested, locals = @spec.to_h.partition { |_, value| value.is_a?(Usable::Config) }
|
83
|
+
nested.map! { |key, _| [key, '{...}'] }
|
84
|
+
locals.concat nested
|
85
|
+
locals.map! { |key, v| %(@#{key}=#{v.inspect}) }
|
86
|
+
"<Usable::Config:0x00#{(object_id << 1).to_s(16)} #{locals.join(', ')}>"
|
82
87
|
end
|
83
88
|
|
89
|
+
alias to_s inspect
|
90
|
+
|
84
91
|
private
|
85
92
|
|
86
93
|
# @note Handles the case where the value may be defined with a block, in which case it's a method
|
data/lib/usable/railtie.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
class Usable::Railtie < Rails::Railtie
|
2
|
-
config.
|
3
|
-
|
4
|
-
|
5
|
-
end
|
2
|
+
config.usable_config = Struct.new(:frozen).new(false)
|
3
|
+
config.after_initialize do |app|
|
4
|
+
Usable.freeze if app.config.usable_config.frozen
|
6
5
|
end
|
7
6
|
end
|
data/lib/usable/struct.rb
CHANGED
@@ -5,10 +5,6 @@ module Usable
|
|
5
5
|
|
6
6
|
self.usables = Usable::Config.new(attributes)
|
7
7
|
|
8
|
-
define_singleton_method(:inherited) do |child|
|
9
|
-
child.usables = usables.clone
|
10
|
-
end
|
11
|
-
|
12
8
|
attributes.keys.each do |key|
|
13
9
|
define_method(key) { @attrs[key] }
|
14
10
|
define_method("#{key}=") { |new_val| @attrs[key] = new_val }
|
data/lib/usable/version.rb
CHANGED
data/lib/usable.rb
CHANGED
@@ -11,6 +11,12 @@ module Usable
|
|
11
11
|
@extended_constants ||= Set.new
|
12
12
|
end
|
13
13
|
|
14
|
+
def self.freeze
|
15
|
+
extended_constants.each { |const| const.usables.freeze }
|
16
|
+
extended_constants.freeze
|
17
|
+
super
|
18
|
+
end
|
19
|
+
|
14
20
|
def self.extended(base)
|
15
21
|
if base.is_a? Class
|
16
22
|
# Define an instance level version of +usables+
|
@@ -39,6 +45,12 @@ module Usable
|
|
39
45
|
extended_constants << base
|
40
46
|
end
|
41
47
|
|
48
|
+
def inherited(base)
|
49
|
+
base.usables += usables
|
50
|
+
Usable.extended_constants << base
|
51
|
+
super
|
52
|
+
end
|
53
|
+
|
42
54
|
def usables
|
43
55
|
@usables ||= Config.new
|
44
56
|
end
|
@@ -98,3 +110,5 @@ module Usable
|
|
98
110
|
usables.available_methods[method_name].bind(context)
|
99
111
|
end
|
100
112
|
end
|
113
|
+
|
114
|
+
require 'usable/railtie' if defined?(Rails::Railtie)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: usable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Buckley
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -106,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
106
|
version: '0'
|
107
107
|
requirements: []
|
108
108
|
rubyforge_project:
|
109
|
-
rubygems_version: 2.
|
109
|
+
rubygems_version: 2.6.8
|
110
110
|
signing_key:
|
111
111
|
specification_version: 4
|
112
112
|
summary: Mounts and configures modules
|