usable 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e6ce8a1897bd2183446ed294714d2dbb6bd4f1b3
4
- data.tar.gz: 24de43ed69870b8a62906f04a1125db2ba1e84ff
3
+ metadata.gz: 012d85e8c84454e2b546f05763299d4157d768ac
4
+ data.tar.gz: 44c5faec64db7d6dcc843c97dbf04601b6900039
5
5
  SHA512:
6
- metadata.gz: c2d8bd54e08e2c2b46450cb1dc916df6d5915bb526f52f7b50de6d6dcdc6940b19e7380160adc1cf70ec820f6b18110eff225069424dd8f3c815582041b42542
7
- data.tar.gz: 663d86bd5095d61a06f639fcb7cf31aa35398c9239353f0418f6d3324c12ada5f8795daee2a89c5baa8ff44459b5a5522616caa251d363a10ac7a13000e61934
6
+ metadata.gz: e5b18af041a6be1df266076067657767c6dcdbb050c0a71e08cfcc348077966d0a86ffd6607d42ab6cab85e526b7fa1f23f65bc99925c49242f8d9031f39ae7d
7
+ data.tar.gz: 4297b9a8d18efe624a92a8a03859930f37a937a812f9cb2f662df7260c3036f385206431c28e322a753eb6dee34a26730beebe1ff7725d902937405267929218
data/.gitignore CHANGED
@@ -7,4 +7,5 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
- *.gem
10
+ *.gem
11
+ .byebug_history
data/Gemfile CHANGED
@@ -2,3 +2,6 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in usable.gemspec
4
4
  gemspec
5
+
6
+ gem 'object_tracker'
7
+ gem 'byebug'
@@ -0,0 +1,15 @@
1
+ module Usable
2
+ class Config < OpenStruct
3
+ def modules
4
+ @modules ||= []
5
+ end
6
+
7
+ def available_methods
8
+ modules.each_with_object({}) do |mod, result|
9
+ mod.instance_methods.each do |method_name|
10
+ result[method_name] = mod.instance_method method_name
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,28 @@
1
+ module Usable
2
+ class ModExtender
3
+ attr_accessor :copy, :mod, :config
4
+
5
+ def initialize(mod, config = OpenStruct.new)
6
+ @mod = mod
7
+ @copy = has_spec? ? mod.const_get(:Spec).dup : mod.dup
8
+ @config = config
9
+ end
10
+
11
+ def call
12
+ copy.prepend override
13
+ end
14
+
15
+ def override
16
+ unwanted = config.only ? copy.instance_methods - Array(config.only) : []
17
+ Module.new do
18
+ unwanted.each do |method_name|
19
+ define_method(method_name) { |*| }
20
+ end
21
+ end
22
+ end
23
+
24
+ def has_spec?
25
+ mod.const_defined?(:Spec)
26
+ end
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module Usable
2
- VERSION = "0.2.0".freeze
2
+ VERSION = "0.3.0".freeze
3
3
  end
data/lib/usable.rb CHANGED
@@ -1,61 +1,42 @@
1
1
  require "ostruct"
2
+ require "delegate"
2
3
  require "usable/version"
3
4
 
4
5
  module Usable
6
+
7
+ autoload :ModExtender, 'usable/mod_extender'
8
+ autoload :Config, 'usable/config'
9
+
5
10
  def usable_config
6
11
  @usable_config ||= Config.new
7
12
  end
8
-
9
- alias_method :config, :usable_config unless method_defined?(:config)
10
-
13
+ attr_writer :usable_config
14
+
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 Spec within the given mods namespace and uses the instance of
17
+ # methods of that as the +available_methods+
18
+ #
19
+ # @example
20
+ #
21
+ # class Example
22
+ # extend Usable
23
+ # usable VersionKit, only: :save_version
24
+ # end
25
+ #
26
+ # @note Hides methods
27
+ # @note We include the primary mod when there is a Spec set because any instance method defined on the mod are not
28
+ # configurable and should therefore takes precedence over those defined in the Spec
11
29
  def usable(mod, options = {})
12
30
  options.each { |k, v| usable_config.public_send "#{k}=", v }
13
- if block_given?
14
- yield usable_config
15
- end
16
- wrapped_mod = spec(mod).dup
17
- wrapped_mod.prepend build_null_mod(wrapped_mod)
18
- usable_config.modules[mod] = wrapped_mod
19
- if has_spec?(mod)
20
- send :include, mod
21
- else
22
- send :include, usable_config.modules[mod]
23
- end
24
- end
25
-
26
- # @description Stub out any "unwanted" methods
27
- def build_null_mod(mod)
28
- unwanted = usable_config.only ? mod.instance_methods - Array(usable_config.only) : []
29
- Module.new do
30
- unwanted.each do |method_name|
31
- define_method(method_name) { |*| }
32
- end
33
- end
31
+ yield usable_config if block_given?
32
+ mod_ext = ModExtender.new mod, usable_config
33
+ usable! mod_ext.call
34
+ usable! mod if mod_ext.has_spec?
34
35
  end
35
36
 
36
- def has_spec?(mod)
37
- mod.const_defined?(:Spec)
38
- end
39
-
40
- def spec(mod)
41
- if has_spec?(mod)
42
- mod.const_get(:Spec)
43
- else
44
- mod
45
- end
46
- end
47
-
48
- class Config < OpenStruct
49
- def available_methods
50
- modules.each_with_object({}) do |(_, mod_copy), result|
51
- mod_copy.instance_methods.each do |method_name|
52
- result[method_name] = mod_copy.instance_method method_name
53
- end
54
- end
55
- end
56
-
57
- def modules
58
- @modules ||= {}
59
- end
37
+ # @description Directly include a module whose methods you want made available in +usable_config.available_methods+
38
+ def usable!(mod)
39
+ usable_config.modules << mod
40
+ send :include, mod
60
41
  end
61
42
  end
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.2.0
4
+ version: 0.3.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-01 00:00:00.000000000 Z
11
+ date: 2016-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -97,6 +97,8 @@ files:
97
97
  - bin/console
98
98
  - bin/setup
99
99
  - lib/usable.rb
100
+ - lib/usable/config.rb
101
+ - lib/usable/mod_extender.rb
100
102
  - lib/usable/scoped.rb
101
103
  - lib/usable/version.rb
102
104
  - usable.gemspec