usable 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +50 -43
- data/bin/console +32 -2
- data/lib/usable/version.rb +1 -1
- data/lib/usable.rb +5 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e995dd364713419052b4643dcc4c3fd32ad3f65
|
4
|
+
data.tar.gz: 133be5883cae3533d8bbd483f63afa53aa950b62
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa11820e33b0cf6ffdf9eeb83772dd70de4794296e7ca85039e96885d57cdf21edaef12ac7271b906ca7dba666eb2ad0ee531d11fa8ac8e86ed1d12de6d4a70a
|
7
|
+
data.tar.gz: d5c339c6300345698e10b7cac58a827f06caa55e1f683691520de0785738fdba9c592b766a2ed361843b575ee87c734b74ff2729f808ff8af16e3ef6e8b5b499
|
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# Usable [![Gem Version](https://badge.fury.io/rb/usable.svg)](http://badge.fury.io/rb/usable)
|
2
2
|
|
3
|
-
A simple way to mount and configure your modules. Usable gives you control over which methods are included, and
|
4
|
-
|
3
|
+
A simple way to mount and configure your modules. Usable gives you control over which methods are included, and a simple
|
4
|
+
interface to help you call dynamic methods with confidence.
|
5
5
|
|
6
6
|
```ruby
|
7
|
-
module
|
7
|
+
module VersionMixin
|
8
8
|
def save_version
|
9
9
|
"Saving up to #{self.class.usable_config.max_versions} versions to #{self.class.usable_config.table_name}"
|
10
10
|
end
|
@@ -13,35 +13,38 @@ module VersionKit
|
|
13
13
|
"Deleting versions from #{self.class.usable_config.table_name}"
|
14
14
|
end
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
class Model
|
18
18
|
extend Usable
|
19
19
|
|
20
|
-
usable
|
20
|
+
usable VersionMixin, only: :save_version do |config|
|
21
21
|
config.max_versions = 10
|
22
22
|
config.table_name = 'custom_versions'
|
23
23
|
end
|
24
|
+
|
25
|
+
def save
|
26
|
+
self.class.usable_method(self, :save_version).call
|
27
|
+
end
|
24
28
|
end
|
25
29
|
|
26
|
-
|
27
|
-
=> "custom_versions"
|
28
|
-
|
29
|
-
=> "Saving up to 10 versions to custom_versions"
|
30
|
-
>> Model.usable_config.available_methods[:save_version].bind(Model.new).call
|
31
|
-
=> "Saving up to 10 versions to custom_versions"
|
32
|
-
>> Model.new.respond_to? :destroy_version
|
33
|
-
=> false
|
34
|
-
>> Model.usable_config.available_methods[:destroy_version].bind(Model.new).call
|
35
|
-
=> nil
|
30
|
+
model = Model.new
|
31
|
+
model.save_version # => "Saving up to 10 versions to custom_versions"
|
32
|
+
model.destroy_version # => NoMethodError: undefined method `destroy_version' for #<Model:...
|
36
33
|
```
|
37
|
-
|
34
|
+
You'll notice that `#save_versions` is now included on `Model`, but `#destroy_version` isn't defined.
|
35
|
+
|
36
|
+
## Confidently calling methods
|
37
|
+
|
38
|
+
We should all be writing [confident code](http://www.confidentruby.com/). That's why it is encouraged
|
39
|
+
to reference methods through the `usable_method` class level function. Methods passed in with the `:only` option
|
40
|
+
will always return `nil` when called.
|
38
41
|
|
39
|
-
|
42
|
+
Here's the same example as above, rewritten to call methods through the Usable interface:
|
40
43
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
44
|
+
```ruby
|
45
|
+
Model.usable_method(model, :save_version).call # => "Saving up to 10 versions to custom_versions"
|
46
|
+
Model.usable_method(model, :destroy_version).call # => nil
|
47
|
+
```
|
45
48
|
|
46
49
|
## Separate included module from configurable methods
|
47
50
|
|
@@ -52,37 +55,41 @@ conflicts will be resolved by giving precedence to the parent module.
|
|
52
55
|
For example:
|
53
56
|
|
54
57
|
```ruby
|
55
|
-
module
|
58
|
+
module Mixin
|
59
|
+
def name
|
60
|
+
"defined by Mixin"
|
61
|
+
end
|
62
|
+
|
63
|
+
def from_mixin
|
64
|
+
"always here"
|
65
|
+
end
|
66
|
+
|
67
|
+
# @description Usable will apply the :only to just the methods defined by this module
|
56
68
|
module UsableSpec
|
57
|
-
def
|
58
|
-
"
|
69
|
+
def from_spec
|
70
|
+
"can be excluded"
|
59
71
|
end
|
60
|
-
|
72
|
+
|
61
73
|
def name
|
62
|
-
"
|
74
|
+
"defined by UsableSpec"
|
63
75
|
end
|
64
76
|
end
|
65
|
-
|
66
|
-
def name
|
67
|
-
"yup"
|
68
|
-
end
|
69
|
-
|
70
|
-
def self.included(base)
|
71
|
-
puts base.usable_config.available_methods[:version].bind(self).call
|
72
|
-
end
|
73
77
|
end
|
74
78
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
=> "
|
82
|
-
|
83
|
-
=>
|
79
|
+
class Example
|
80
|
+
extend Usable
|
81
|
+
usable Mixin, only: [:name, :from_spec]
|
82
|
+
end
|
83
|
+
|
84
|
+
Example.new.from_spec # => "can be excluded"
|
85
|
+
Example.new.from_mixin # => "always here"
|
86
|
+
Example.new.name # => "defined by Mixin"
|
87
|
+
Example.ancestors # => [Example, Mixin, Example::MixinUsableSpecUsed, Object, Kernel, BasicObject] (ruby -v 2.3.0)
|
84
88
|
```
|
85
89
|
|
90
|
+
Noticed that Usable assigns the modified module to a constant with the same name as the given module, but with "Used" appended.
|
91
|
+
The main module and the spec were both included, but `Mixin` was not modified, so it didn't need a new name.
|
92
|
+
|
86
93
|
## Installation
|
87
94
|
|
88
95
|
Add this line to your application's Gemfile:
|
data/bin/console
CHANGED
@@ -10,7 +10,7 @@ require "usable"
|
|
10
10
|
# require "pry"
|
11
11
|
# Pry.start
|
12
12
|
|
13
|
-
module
|
13
|
+
module VersionMixin
|
14
14
|
def save_version
|
15
15
|
"Saving up to #{self.class.usable_config.max_versions} versions to #{self.class.usable_config.table_name}"
|
16
16
|
end
|
@@ -20,13 +20,43 @@ module VersionKit
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
+
module Mixin
|
24
|
+
def name
|
25
|
+
"defined by Mixin"
|
26
|
+
end
|
27
|
+
|
28
|
+
def from_mixin
|
29
|
+
"always here"
|
30
|
+
end
|
31
|
+
|
32
|
+
# @description Usable will apply the :only to just the methods defined by this module
|
33
|
+
module UsableSpec
|
34
|
+
def from_spec
|
35
|
+
"can be excluded"
|
36
|
+
end
|
37
|
+
|
38
|
+
def name
|
39
|
+
"defined by UsableSpec"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
23
44
|
class Model
|
24
45
|
extend Usable
|
25
46
|
|
26
|
-
usable
|
47
|
+
usable VersionMixin, only: :save_version do |config|
|
27
48
|
config.max_versions = 10
|
28
49
|
config.table_name = 'custom_versions'
|
29
50
|
end
|
51
|
+
|
52
|
+
def save
|
53
|
+
self.class.usable_method(self, :save_version).call
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class Example
|
58
|
+
extend Usable
|
59
|
+
usable Mixin, only: [:name, :from_spec]
|
30
60
|
end
|
31
61
|
|
32
62
|
require "irb"
|
data/lib/usable/version.rb
CHANGED
data/lib/usable.rb
CHANGED
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.2.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-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|