usable 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/{LICENSE.md → LICENSE.txt} +0 -0
- data/README.md +7 -6
- data/lib/usable.rb +9 -1
- data/lib/usable/config.rb +6 -2
- data/lib/usable/mod_extender.rb +2 -0
- data/lib/usable/scoped.rb +1 -1
- data/lib/usable/version.rb +1 -1
- data/usable.gemspec +3 -2
- metadata +8 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4838af9eb7ede803ba0bb91bcdde9f4500db8bf9
|
4
|
+
data.tar.gz: 68fb23ea8a2e2ad941f420aca9f797d7cba37e4c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da020287fc46e438e2c1dfad79eb90ae4e9aca2b8dcfe0c01bc01d1be91f4f661b1d88237c4765cb487d2a316e0f74350eeb535e93ca107bb21a6539127be752
|
7
|
+
data.tar.gz: 6d68fcda221aec13e6933e90612c344ec7a410b4bdeac1e59b8498d916734e2a9e55acd310566e8a4c36c66725b0205719b6e21a0fb84f856b642ac9c41de2fd
|
data/{LICENSE.md → LICENSE.txt}
RENAMED
File without changes
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
# Usable
|
1
|
+
# Usable [![Gem Version](https://badge.fury.io/rb/usable.svg)](http://badge.fury.io/rb/usable)
|
2
2
|
|
3
|
-
|
3
|
+
A simple way to mount and configure your modules. Usable gives you control over which methods are included, and the class
|
4
|
+
level config provides a safe interface for calling them.
|
4
5
|
|
5
6
|
```ruby
|
6
7
|
module VersionKit
|
@@ -26,11 +27,11 @@ end
|
|
26
27
|
=> "custom_versions"
|
27
28
|
>> Model.new.save_version
|
28
29
|
=> "Saving up to 10 versions to custom_versions"
|
29
|
-
>> Model.usable_config.available_methods[:save_version].bind(
|
30
|
+
>> Model.usable_config.available_methods[:save_version].bind(Model.new).call
|
30
31
|
=> "Saving up to 10 versions to custom_versions"
|
31
32
|
>> Model.new.respond_to? :destroy_version
|
32
33
|
=> false
|
33
|
-
>> Model.usable_config.available_methods[:destroy_version].bind(
|
34
|
+
>> Model.usable_config.available_methods[:destroy_version].bind(Model.new).call
|
34
35
|
=> nil
|
35
36
|
```
|
36
37
|
What's going on here? Well `#save_versions` is now extended onto the `Model` class, but `#destroy_version` is not!
|
@@ -40,9 +41,9 @@ What's going on here? Well `#save_versions` is now extended onto the `Model` cla
|
|
40
41
|
Yes. Well ... yes, at least on the copy of the module included in the target class. But, checking if an object responds
|
41
42
|
to a method all time doesn't produce very [confident code](http://www.confidentruby.com/). That's why it is encouraged
|
42
43
|
to reference methods through the `Model.usable_config.available_methods` hash. This way you can confidently call methods,
|
43
|
-
just don't rely on the return value
|
44
|
+
just don't rely on the return value, because methods that are removed via `:only` will return `nil`.
|
44
45
|
|
45
|
-
##
|
46
|
+
## Separate included module from configurable methods
|
46
47
|
|
47
48
|
Sometimes you want to define methods on the module but not have them be configurable. Define a module within the usable
|
48
49
|
module namespace and name it `UsableSpec`, and `Usable` will use that module to configure the available methods. Any naming
|
data/lib/usable.rb
CHANGED
@@ -26,16 +26,24 @@ module Usable
|
|
26
26
|
# @note Hides methods
|
27
27
|
# @note We include the primary mod when there is a UsableSpec set because any instance method defined on the mod are not
|
28
28
|
# configurable and should therefore takes precedence over those defined in the UsableSpec
|
29
|
+
# @return [ModExtender] containing the original and modified module
|
29
30
|
def usable(mod, options = {})
|
30
31
|
options.each { |k, v| usable_config.public_send "#{k}=", v }
|
31
32
|
yield usable_config if block_given?
|
32
33
|
mod_ext = ModExtender.new mod, usable_config
|
33
|
-
usable! mod_ext
|
34
|
+
usable! mod_ext
|
34
35
|
usable! mod if mod_ext.has_spec?
|
36
|
+
mod_ext
|
35
37
|
end
|
36
38
|
|
37
39
|
# @description Directly include a module whose methods you want made available in +usable_config.available_methods+
|
40
|
+
# Gives the module a name when including so that it shows up properly in the list of ancestors
|
38
41
|
def usable!(mod)
|
42
|
+
mod_name = mod.name.nil? ? "UsableMod#{Time.now.strftime('%s')}" : mod.name.split('::').last
|
43
|
+
const_name = "#{mod_name}Used"
|
44
|
+
mod = mod.call if mod.respond_to? :call
|
45
|
+
remove_const const_name if const_defined? const_name
|
46
|
+
const_set const_name, mod
|
39
47
|
usable_config.modules << mod
|
40
48
|
send :include, mod
|
41
49
|
end
|
data/lib/usable/config.rb
CHANGED
@@ -4,6 +4,10 @@ module Usable
|
|
4
4
|
@modules ||= []
|
5
5
|
end
|
6
6
|
|
7
|
+
def add_module(mod)
|
8
|
+
modules << mod
|
9
|
+
end
|
10
|
+
|
7
11
|
def available_methods
|
8
12
|
modules.each_with_object(Hash.new(default_method)) do |mod, result|
|
9
13
|
mod.instance_methods.each do |method_name|
|
@@ -13,11 +17,11 @@ module Usable
|
|
13
17
|
end
|
14
18
|
|
15
19
|
def default_method
|
16
|
-
Null.instance_method(:
|
20
|
+
Null.instance_method(:default_method)
|
17
21
|
end
|
18
22
|
|
19
23
|
module Null
|
20
|
-
def
|
24
|
+
def default_method(*, &block)
|
21
25
|
end
|
22
26
|
end
|
23
27
|
end
|
data/lib/usable/mod_extender.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
module Usable
|
2
2
|
class ModExtender
|
3
|
+
attr_reader :name
|
3
4
|
attr_accessor :copy, :mod, :config
|
4
5
|
|
5
6
|
def initialize(mod, config = OpenStruct.new)
|
6
7
|
@mod = mod
|
8
|
+
@name = mod.name
|
7
9
|
@copy = has_spec? ? mod.const_get(:UsableSpec).dup : mod.dup
|
8
10
|
@config = config
|
9
11
|
end
|
data/lib/usable/scoped.rb
CHANGED
data/lib/usable/version.rb
CHANGED
data/usable.gemspec
CHANGED
@@ -9,9 +9,10 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Ryan Buckley"]
|
10
10
|
spec.email = ["arebuckley@gmail.com"]
|
11
11
|
|
12
|
-
spec.summary = %q{
|
13
|
-
spec.description = %q{
|
12
|
+
spec.summary = %q{Mounts and configures modules}
|
13
|
+
spec.description = %q{A simple way to mount and configure your modules. Usable gives you control over which methods are included.}
|
14
14
|
spec.homepage = "https://github.com/ridiculous/usable"
|
15
|
+
spec.license = "MIT"
|
15
16
|
|
16
17
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
18
|
spec.bindir = "exe"
|
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: 1.
|
4
|
+
version: 1.1.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: 2016-02-
|
11
|
+
date: 2016-02-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -78,8 +78,8 @@ dependencies:
|
|
78
78
|
- - "<"
|
79
79
|
- !ruby/object:Gem::Version
|
80
80
|
version: '2'
|
81
|
-
description:
|
82
|
-
|
81
|
+
description: A simple way to mount and configure your modules. Usable gives you control
|
82
|
+
over which methods are included.
|
83
83
|
email:
|
84
84
|
- arebuckley@gmail.com
|
85
85
|
executables: []
|
@@ -91,7 +91,7 @@ files:
|
|
91
91
|
- ".ruby-version"
|
92
92
|
- ".travis.yml"
|
93
93
|
- Gemfile
|
94
|
-
- LICENSE.
|
94
|
+
- LICENSE.txt
|
95
95
|
- README.md
|
96
96
|
- Rakefile
|
97
97
|
- bin/console
|
@@ -103,7 +103,8 @@ files:
|
|
103
103
|
- lib/usable/version.rb
|
104
104
|
- usable.gemspec
|
105
105
|
homepage: https://github.com/ridiculous/usable
|
106
|
-
licenses:
|
106
|
+
licenses:
|
107
|
+
- MIT
|
107
108
|
metadata: {}
|
108
109
|
post_install_message:
|
109
110
|
rdoc_options: []
|
@@ -124,5 +125,5 @@ rubyforge_project:
|
|
124
125
|
rubygems_version: 2.5.1
|
125
126
|
signing_key:
|
126
127
|
specification_version: 4
|
127
|
-
summary:
|
128
|
+
summary: Mounts and configures modules
|
128
129
|
test_files: []
|