usable 0.3.0 → 1.0.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 +39 -17
- data/bin/console +2 -2
- data/lib/usable/config.rb +10 -1
- data/lib/usable/mod_extender.rb +7 -7
- data/lib/usable/version.rb +1 -1
- data/lib/usable.rb +3 -3
- 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: 5dd9e5a4f0ef7f2d80c5e58c0465af4215842155
|
4
|
+
data.tar.gz: 26c44236196a65f58ee95aaf5609f54c6f90017e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c479a93b7bea7ec97fe3bcff61cf5d9ca19ea6f07082b78f0c28e017193be670dcdabb5b48c1d5664a8e320d530028bf0436e14ff22ce2e3d334cc2b309cd645
|
7
|
+
data.tar.gz: c0c5ef4f78fa3c6713b0f23baa3b6d1cc7e11209d61c3cb1c2c5b36127d372f9283d3c34dd421fc544e64d494a2fdd5a9758fca7a95c5dc043228efb3c887fb4
|
data/README.md
CHANGED
@@ -5,11 +5,11 @@ Rack style mixins for Ruby objects. Mount your modules like you mean it!
|
|
5
5
|
```ruby
|
6
6
|
module VersionKit
|
7
7
|
def save_version
|
8
|
-
"Saving up to #{self.class.
|
8
|
+
"Saving up to #{self.class.usable_config.max_versions} versions to #{self.class.usable_config.table_name}"
|
9
9
|
end
|
10
10
|
|
11
11
|
def destroy_version
|
12
|
-
"Deleting versions from #{self.class.
|
12
|
+
"Deleting versions from #{self.class.usable_config.table_name}"
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
@@ -22,26 +22,48 @@ class Model
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
>> Model.
|
25
|
+
>> Model.usable_config.table_name
|
26
26
|
=> "custom_versions"
|
27
|
-
>> Model.new.
|
27
|
+
>> Model.new.save_version
|
28
28
|
=> "Saving up to 10 versions to custom_versions"
|
29
|
-
>> Model.
|
29
|
+
>> Model.usable_config.available_methods[:save_version].bind(self).call
|
30
|
+
=> "Saving up to 10 versions to custom_versions"
|
31
|
+
>> Model.new.respond_to? :destroy_version
|
32
|
+
=> false
|
33
|
+
>> Model.usable_config.available_methods[:destroy_version].bind(self).call
|
30
34
|
=> nil
|
31
35
|
```
|
32
|
-
|
33
|
-
|
36
|
+
What's going on here? Well `#save_versions` is now extended onto the `Model` class, but `#destroy_version` is not!
|
37
|
+
|
38
|
+
## But wait, you undefined my methods?
|
39
|
+
|
40
|
+
Yes. Well ... yes, at least on the copy of the module included in the target class. But, checking if an object responds
|
41
|
+
to a method all time doesn't produce very [confident code](http://www.confidentruby.com/). That's why it is encouraged
|
42
|
+
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! Methods that are removed via `:only` will return `nil`.
|
44
|
+
|
45
|
+
## Seperate included module from configurable methods
|
46
|
+
|
47
|
+
Sometimes you want to define methods on the module but not have them be configurable. Define a module within the usable
|
48
|
+
module namespace and name it `UsableSpec`, and `Usable` will use that module to configure the available methods. Any naming
|
49
|
+
conflicts will be resolved by giving precedence to the parent module.
|
50
|
+
|
51
|
+
For example:
|
34
52
|
|
35
53
|
```ruby
|
36
54
|
module VersionKit
|
37
|
-
module
|
55
|
+
module UsableSpec
|
38
56
|
def version
|
39
|
-
"
|
57
|
+
"yo"
|
58
|
+
end
|
59
|
+
|
60
|
+
def name
|
61
|
+
"nope"
|
40
62
|
end
|
41
63
|
end
|
42
64
|
|
43
|
-
def
|
44
|
-
"
|
65
|
+
def name
|
66
|
+
"yup"
|
45
67
|
end
|
46
68
|
|
47
69
|
def self.included(base)
|
@@ -52,8 +74,12 @@ end
|
|
52
74
|
>> Example = Class.new.extend Usable
|
53
75
|
=> Example
|
54
76
|
>> Example.usable VersionKit
|
55
|
-
|
77
|
+
yo
|
56
78
|
=> Example
|
79
|
+
>> Example.new.version
|
80
|
+
=> "yo"
|
81
|
+
>> Example.new.name
|
82
|
+
=> "yup"
|
57
83
|
```
|
58
84
|
|
59
85
|
## Installation
|
@@ -64,10 +90,6 @@ Add this line to your application's Gemfile:
|
|
64
90
|
gem 'usable'
|
65
91
|
```
|
66
92
|
|
67
|
-
Or install it yourself as:
|
68
|
-
|
69
|
-
$ gem install usable
|
70
|
-
|
71
93
|
## Development
|
72
94
|
|
73
95
|
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.
|
@@ -76,5 +98,5 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
76
98
|
|
77
99
|
## Contributing
|
78
100
|
|
79
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
101
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ridiculous/usable.
|
80
102
|
|
data/bin/console
CHANGED
@@ -12,11 +12,11 @@ require "usable"
|
|
12
12
|
|
13
13
|
module VersionKit
|
14
14
|
def save_version
|
15
|
-
"Saving up to #{self.class.
|
15
|
+
"Saving up to #{self.class.usable_config.max_versions} versions to #{self.class.usable_config.table_name}"
|
16
16
|
end
|
17
17
|
|
18
18
|
def destroy_version
|
19
|
-
"Deleting versions from #{self.class.
|
19
|
+
"Deleting versions from #{self.class.usable_config.table_name}"
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
data/lib/usable/config.rb
CHANGED
@@ -5,11 +5,20 @@ module Usable
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def available_methods
|
8
|
-
modules.each_with_object(
|
8
|
+
modules.each_with_object(Hash.new(default_method)) do |mod, result|
|
9
9
|
mod.instance_methods.each do |method_name|
|
10
10
|
result[method_name] = mod.instance_method method_name
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
14
|
+
|
15
|
+
def default_method
|
16
|
+
Null.instance_method(:null)
|
17
|
+
end
|
18
|
+
|
19
|
+
module Null
|
20
|
+
def null(*, &block)
|
21
|
+
end
|
22
|
+
end
|
14
23
|
end
|
15
24
|
end
|
data/lib/usable/mod_extender.rb
CHANGED
@@ -4,25 +4,25 @@ module Usable
|
|
4
4
|
|
5
5
|
def initialize(mod, config = OpenStruct.new)
|
6
6
|
@mod = mod
|
7
|
-
@copy = has_spec? ? mod.const_get(:
|
7
|
+
@copy = has_spec? ? mod.const_get(:UsableSpec).dup : mod.dup
|
8
8
|
@config = config
|
9
9
|
end
|
10
10
|
|
11
|
+
# @note Destructive
|
11
12
|
def call
|
12
|
-
|
13
|
+
override
|
14
|
+
copy
|
13
15
|
end
|
14
16
|
|
15
17
|
def override
|
16
18
|
unwanted = config.only ? copy.instance_methods - Array(config.only) : []
|
17
|
-
|
18
|
-
|
19
|
-
define_method(method_name) { |*| }
|
20
|
-
end
|
19
|
+
unwanted.each do |method_name|
|
20
|
+
copy.send :remove_method, method_name
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
24
|
def has_spec?
|
25
|
-
mod.const_defined?(:
|
25
|
+
mod.const_defined?(:UsableSpec)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
data/lib/usable/version.rb
CHANGED
data/lib/usable.rb
CHANGED
@@ -13,7 +13,7 @@ module Usable
|
|
13
13
|
attr_writer :usable_config
|
14
14
|
|
15
15
|
# @description Configures the +available_methods+ of a module using the given options or block and then includes it on
|
16
|
-
# the target class. Checks if there is a module named
|
16
|
+
# the target class. Checks if there is a module named UsableSpec within the given mods namespace and uses the instance of
|
17
17
|
# methods of that as the +available_methods+
|
18
18
|
#
|
19
19
|
# @example
|
@@ -24,8 +24,8 @@ module Usable
|
|
24
24
|
# end
|
25
25
|
#
|
26
26
|
# @note Hides methods
|
27
|
-
# @note We include the primary mod when there is a
|
28
|
-
# configurable and should therefore takes precedence over those defined in the
|
27
|
+
# @note We include the primary mod when there is a UsableSpec set because any instance method defined on the mod are not
|
28
|
+
# configurable and should therefore takes precedence over those defined in the UsableSpec
|
29
29
|
def usable(mod, options = {})
|
30
30
|
options.each { |k, v| usable_config.public_send "#{k}=", v }
|
31
31
|
yield usable_config if block_given?
|
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: 0.
|
4
|
+
version: 1.0.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-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|