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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 012d85e8c84454e2b546f05763299d4157d768ac
4
- data.tar.gz: 44c5faec64db7d6dcc843c97dbf04601b6900039
3
+ metadata.gz: 5dd9e5a4f0ef7f2d80c5e58c0465af4215842155
4
+ data.tar.gz: 26c44236196a65f58ee95aaf5609f54c6f90017e
5
5
  SHA512:
6
- metadata.gz: e5b18af041a6be1df266076067657767c6dcdbb050c0a71e08cfcc348077966d0a86ffd6607d42ab6cab85e526b7fa1f23f65bc99925c49242f8d9031f39ae7d
7
- data.tar.gz: 4297b9a8d18efe624a92a8a03859930f37a937a812f9cb2f662df7260c3036f385206431c28e322a753eb6dee34a26730beebe1ff7725d902937405267929218
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.config.max_versions} versions to #{self.class.config.table_name}"
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.config.table_name}"
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.config.table_name
25
+ >> Model.usable_config.table_name
26
26
  => "custom_versions"
27
- >> Model.new.versions
27
+ >> Model.new.save_version
28
28
  => "Saving up to 10 versions to custom_versions"
29
- >> Model.new.destroy_version
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
- You can also define a custom module within the "usable" module that defines the methods which can be configured to be
33
- extended or excluded. The module must be named "Spec" and be defined one level inside the namespace. For example:
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 Spec
55
+ module UsableSpec
38
56
  def version
39
- "spec version included"
57
+ "yo"
58
+ end
59
+
60
+ def name
61
+ "nope"
40
62
  end
41
63
  end
42
64
 
43
- def version
44
- "this version not included"
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
- spec version included
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/[USERNAME]/usable.
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.config.max_versions} versions to #{self.class.config.table_name}"
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.config.table_name}"
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({}) do |mod, result|
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
@@ -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(:Spec).dup : mod.dup
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
- copy.prepend override
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
- Module.new do
18
- unwanted.each do |method_name|
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?(:Spec)
25
+ mod.const_defined?(:UsableSpec)
26
26
  end
27
27
  end
28
28
  end
@@ -1,3 +1,3 @@
1
1
  module Usable
2
- VERSION = "0.3.0".freeze
2
+ VERSION = "1.0.0".freeze
3
3
  end
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 Spec within the given mods namespace and uses the instance of
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 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
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.3.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-02 00:00:00.000000000 Z
11
+ date: 2016-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler