usable 1.3.1 → 1.4.0

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: 25b327952ba8a139e5c81f640b5d7100f6daabf5
4
- data.tar.gz: 234c99e407ef28df46bb024a8656e5a1bce5c19b
3
+ metadata.gz: d621f72fd2963cd0ed5e82ca9a52458a412ac982
4
+ data.tar.gz: 870b80be3fa3fb74e178ad5204b806a78c37a3a8
5
5
  SHA512:
6
- metadata.gz: ed4caf302df5b5f543f31ab9e15bae983d6366ee4c07a71eed728551e6bc22a5d96a1d25eaef5f54e9c513c0195b6773f72f2c044ee1fc49ae2fd3db81e8300d
7
- data.tar.gz: b0232a418c5b063d47671ce3944ae6bb4dbbda93d3c278f32474e4595620bdb7ec9ebd8ea3c49cc11738e27d1865e96e068d6a6018c798f99435a9bb0c1f83db
6
+ metadata.gz: 15e90259cd4528fbedd199d8b2024622d8b93ef4f534e65de55b5aae5ef11e694cdb30ed0d73a542fff333f5cba796dae6488b34118f22bae25e591d952a5ced
7
+ data.tar.gz: 9bfb0cbe38beb7b8e5b7d76cbb8395cd02f1f552a8abe0417d908546b1df38ae186537888dfc9a9ebd78a63495ea4c97128423421091e4cb7e1a3ce8ba49db71
data/README.md CHANGED
@@ -60,11 +60,14 @@ Model.usable_method(model, :save_version).call # => "Saving up to 10 versions
60
60
  Model.usable_method(model, :destroy_version).call # => nil
61
61
  ```
62
62
 
63
- ## Separate the _included_ module from the _configurable_ methods
63
+ ## Module Naming Conventions
64
64
 
65
- Sometimes you want to define methods on a module and have them always be included. To do this, define a module named
66
- `UsableSpec` in the scope of the module you are mounting. `Usable` will detect this and use he "spec" module to configure
67
- the available methods. Any naming conflicts will be resolved by giving precedence to the parent module.
65
+ Modules with the following names found within the target module's namespace will be automatically used.
66
+
67
+ `ClassMethods` - extended onto the target module.
68
+
69
+ `UsableSpec` - tells usable which methods are configurable via the `:only` option. Any naming conflicts will be resolved by
70
+ giving precedence to the parent module.
68
71
 
69
72
  For example:
70
73
 
@@ -92,13 +95,13 @@ end
92
95
 
93
96
  class Example
94
97
  extend Usable
95
- usable Mixin
98
+ usable Mixin, only: :from_spec
96
99
  end
97
100
 
98
101
  Example.new.from_spec # => "can be excluded"
99
102
  Example.new.from_mixin # => "always here"
100
103
  Example.new.name # => "defined by Mixin"
101
- Example.ancestors # => [Example, Mixin, Mixin::UsableSpec, Object, Kernel, BasicObject] (ruby -v 2.3.0)
104
+ Example.ancestors # => [Example, Mixin, Example::MixinUsableSpecUsed, Object, Kernel, BasicObject] (ruby -v 2.3.0)
102
105
  ```
103
106
 
104
107
  ## Notes
@@ -1,5 +1,8 @@
1
1
  module Usable
2
2
  class ModExtender
3
+ SPEC = :UsableSpec
4
+ CLASS_MODULE = :ClassMethods
5
+
3
6
  attr_reader :name
4
7
  attr_accessor :copy, :mod, :options, :unwanted
5
8
 
@@ -8,7 +11,7 @@ module Usable
8
11
  @options = options
9
12
  @options[:method] ||= :include
10
13
  if has_spec?
11
- @copy = mod.const_get(:UsableSpec)
14
+ @copy = mod.const_get SPEC
12
15
  @name = "#{mod.name}UsableSpec"
13
16
  else
14
17
  @copy = mod
@@ -40,15 +43,21 @@ module Usable
40
43
  target.send options[:method], copy
41
44
  end
42
45
 
43
- # @description Sends the method to the target with the original module
46
+ # @description Includes or prepends the original module onto the target
44
47
  def use_original!(target)
45
48
  return unless has_spec?
46
49
  target.usable_config.modules << mod
47
50
  target.send options[:method], mod
48
51
  end
49
52
 
53
+ # @description Extends the target with the module's ClassMethod mod
54
+ def use_class_methods!(target)
55
+ return unless mod.const_defined? CLASS_MODULE
56
+ target.extend mod.const_get CLASS_MODULE
57
+ end
58
+
50
59
  def has_spec?
51
- mod.const_defined?(:UsableSpec)
60
+ mod.const_defined? SPEC
52
61
  end
53
62
 
54
63
  def mod_name
@@ -1,3 +1,3 @@
1
1
  module Usable
2
- VERSION = "1.3.1".freeze
2
+ VERSION = "1.4.0".freeze
3
3
  end
data/lib/usable.rb CHANGED
@@ -34,11 +34,15 @@ module Usable
34
34
  # @return [ModExtender] containing the original and modified module
35
35
  def usable(mod, options = {})
36
36
  usable_options = { only: options.delete(:only), method: options.delete(:method) }
37
+ if mod.respond_to? :usable_config
38
+ mod.usable_config.to_h.each { |k, v| usable_config.public_send "#{k}=", v }
39
+ end
37
40
  options.each { |k, v| usable_config.public_send "#{k}=", v }
38
41
  yield usable_config if block_given?
39
42
  mod_ext = ModExtender.new mod, usable_options
40
43
  mod_ext.use! self
41
44
  mod_ext.use_original! self
45
+ mod_ext.use_class_methods! self
42
46
  mod_ext
43
47
  end
44
48
 
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.3.1
4
+ version: 1.4.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-03-10 00:00:00.000000000 Z
11
+ date: 2016-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler