usable 0.1.0 → 0.2.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: 97cce0168afce1846a0d35f692a9880e517dbe7e
4
- data.tar.gz: 9fde115f8e348ef8fa94f9b79f231f1dce592026
3
+ metadata.gz: e6ce8a1897bd2183446ed294714d2dbb6bd4f1b3
4
+ data.tar.gz: 24de43ed69870b8a62906f04a1125db2ba1e84ff
5
5
  SHA512:
6
- metadata.gz: 3a19a138c0a0ddfb9a6c980cb4abaac2477f2458e95ca3a6a1bd07c6f6f6fcd1f5e53d5ce2e6c1ef08a14b3a065ce68bc817f12545a1bd59497587e39f0beab8
7
- data.tar.gz: 6f8fd5859cdd1f1afac52db4555d374fc5cad64a578239358a7d6b2d4ff9eccc780281c314cf11c8324bfbbcde3367c27555dc7c14e8dfb7a1cb63deb653eda5
6
+ metadata.gz: c2d8bd54e08e2c2b46450cb1dc916df6d5915bb526f52f7b50de6d6dcdc6940b19e7380160adc1cf70ec820f6b18110eff225069424dd8f3c815582041b42542
7
+ data.tar.gz: 663d86bd5095d61a06f639fcb7cf31aa35398c9239353f0418f6d3324c12ada5f8795daee2a89c5baa8ff44459b5a5522616caa251d363a10ac7a13000e61934
data/README.md CHANGED
@@ -3,26 +3,57 @@
3
3
  Rack style mixins for Ruby objects. Mount your modules like you mean it!
4
4
 
5
5
  ```ruby
6
+ module VersionKit
7
+ def save_version
8
+ "Saving up to #{self.class.config.max_versions} versions to #{self.class.config.table_name}"
9
+ end
10
+
11
+ def destroy_version
12
+ "Deleting versions from #{self.class.config.table_name}"
13
+ end
14
+ end
15
+
6
16
  class Model
7
17
  extend Usable
8
-
9
- module Versionable
10
- def versions
11
- "Saving #{self.class.config.max_versions} versions to #{self.class.config.table_name}"
12
- end
18
+
19
+ usable VersionKit, only: :save_version do |config|
20
+ config.max_versions = 10
21
+ config.table_name = 'custom_versions'
13
22
  end
23
+ end
14
24
 
15
- # with options hash
16
- use Versionable, table_name: 'custom_versions'
25
+ >> Model.config.table_name
26
+ => "custom_versions"
27
+ >> Model.new.versions
28
+ => "Saving up to 10 versions to custom_versions"
29
+ >> Model.new.destroy_version
30
+ => nil
31
+ ```
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:
17
34
 
18
- # or with block
19
- use Versionable do |config|
20
- config.max_versions = 10
35
+ ```ruby
36
+ module VersionKit
37
+ module Spec
38
+ def version
39
+ "spec version included"
40
+ end
41
+ end
42
+
43
+ def version
44
+ "this version not included"
45
+ end
46
+
47
+ def self.included(base)
48
+ puts base.usable_config.available_methods[:version].bind(self).call
21
49
  end
22
50
  end
23
51
 
24
- Model.config.table_name #=> 'custom_versions'
25
- Model.new.versions #=> "Saving 10 versions to custom_versions"
52
+ >> Example = Class.new.extend Usable
53
+ => Example
54
+ >> Example.usable VersionKit
55
+ spec version included
56
+ => Example
26
57
  ```
27
58
 
28
59
  ## Installation
data/bin/console CHANGED
@@ -10,5 +10,25 @@ require "usable"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
+ module VersionKit
14
+ def save_version
15
+ "Saving up to #{self.class.config.max_versions} versions to #{self.class.config.table_name}"
16
+ end
17
+
18
+ def destroy_version
19
+ "Deleting versions from #{self.class.config.table_name}"
20
+ end
21
+ end
22
+
23
+ class Model
24
+ extend Usable
25
+
26
+ usable VersionKit, only: :save_version do |config|
27
+ config.max_versions = 10
28
+ config.table_name = 'custom_versions'
29
+ end
30
+ end
31
+
13
32
  require "irb"
33
+
14
34
  IRB.start
data/lib/usable/scoped.rb CHANGED
@@ -13,7 +13,7 @@ module Usable
13
13
  base.extend Configurable
14
14
  end
15
15
 
16
- def use(mod, options = {})
16
+ def usable(mod, options = {})
17
17
  send :include, mod unless self < mod
18
18
  if block_given?
19
19
  yield configs[mod]
@@ -42,12 +42,12 @@ end
42
42
  extend Usable::Scoped
43
43
 
44
44
  # with options hash
45
- use Versionable, table_name: 'custom_versions'
45
+ usable Versionable, table_name: 'custom_versions'
46
46
  # or with block
47
- use Versionable do |config|
47
+ usable Versionable do |config|
48
48
  config.max_versions = 10
49
49
  end
50
- use Notable do |config|
50
+ usable Notable do |config|
51
51
  config.max_versions = 20
52
52
  config.table_name = 'custom_notes'
53
53
  end
@@ -1,3 +1,3 @@
1
1
  module Usable
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
data/lib/usable.rb CHANGED
@@ -2,19 +2,60 @@ require "ostruct"
2
2
  require "usable/version"
3
3
 
4
4
  module Usable
5
- def config
6
- @config ||= Config.new
5
+ def usable_config
6
+ @usable_config ||= Config.new
7
7
  end
8
8
 
9
- def use(mod, options = {})
10
- send :include, mod unless self < mod
9
+ alias_method :config, :usable_config unless method_defined?(:config)
10
+
11
+ def usable(mod, options = {})
12
+ options.each { |k, v| usable_config.public_send "#{k}=", v }
11
13
  if block_given?
12
- yield config
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
34
+ end
35
+
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)
13
43
  else
14
- options.each { |k, v| config.public_send "#{k}=", v }
44
+ mod
15
45
  end
16
46
  end
17
47
 
18
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
19
60
  end
20
61
  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.1.0
4
+ version: 0.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-01-30 00:00:00.000000000 Z
11
+ date: 2016-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler