spaced 0.3.0 → 0.3.1

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
  SHA256:
3
- metadata.gz: 4be5f8b778afea6200764ac30e9588cd76ebedd1e6414c3abf7c2b57fadf9fc1
4
- data.tar.gz: 01de2584949b4f50211847ba3a2fd11081f7d2e8b30769117d6016ed665596c7
3
+ metadata.gz: 496fc25b4cc4a7b4d8332661ace97b36a26ee5cb9592bfeb06e1743483c47c3b
4
+ data.tar.gz: 96b4ddf6c1c5e89ead4f894ced1f71c2b2a671c7838766572f94f85462bedf82
5
5
  SHA512:
6
- metadata.gz: 29031788d5ab97c24364206b47583f63d37ed9f2186b541cd10003d7cdfd94f96711d638b485823085d400301d4e8c37302b587f40d4ee1e2cb4a674c2a3e6b6
7
- data.tar.gz: f0854fa0e2813f899fc32b85e57c72b1f44845aa4574f7c419ce5e7ba5f65ad0dfd5b07e4c6de55f2514b95da1abd51627bbec0c0363e936a650a0ace847e06b
6
+ metadata.gz: 4751df5962de7c88361b534dff26007baeed5a91bd8a4f796aa75d6ff31f5a48c10c8b9dc8d53528af00f9089cc8bc8865b3c97b209227cb5c9dab96f84f4c33
7
+ data.tar.gz: eb9fd26c2e44f605bf9c9f733b22263ff86a0f5fd4d8003fe1457544b3a4b2a7f08ac13b3132a7447230c3017aab6c1c4bce5d407ee4bc56d53a6fc4dd86f8ef
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- spaced (0.2.0)
4
+ spaced (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Spaced
2
2
 
3
- Spaced is a super simple and convenient way to isolate and namespace a collection of related methods.
3
+ Spaced is a super simple and convenient way to isolate and namespace a collection of related methods into any class.
4
+
5
+ ## Usage
4
6
 
5
7
  ```ruby
6
8
  class User
@@ -16,14 +18,6 @@ class User
16
18
  api.read_tweet id
17
19
  end
18
20
 
19
- def call(msg)
20
- create msg
21
- end
22
-
23
- def predicate
24
- subject.twitter_id?
25
- end
26
-
27
21
  private
28
22
 
29
23
  def api
@@ -38,12 +32,41 @@ end
38
32
  user = User.new
39
33
  id = user.twitter.create("Spaced man!")
40
34
  user.twitter.read(id)
41
- user.twitter!("Spaced man!") # calls the `call` method.
42
- user.twitter? # calls the `predicate` method.
43
35
  ```
44
36
 
45
37
  In the example above, `namespace` creates and initializes a new class `Twitter` and returns it from the `#twitter` method. The parent class - in this case `User` - is available at `#parent` and `@parent` from within the namespace.
46
38
 
39
+ ## Magic bang and predicate methods
40
+
41
+ If you define a `call` method in your namespaced class, you can then conveniently call that with a bang method:
42
+
43
+ ```ruby
44
+ class User < Spaced::Base
45
+ include Spaced
46
+
47
+ namespace :tweet do
48
+ def call(content)
49
+ create_tweet content
50
+ end
51
+ end
52
+ end
53
+
54
+ user = User.new
55
+ user.tweet!('my new tweet') # Will call the `#call` method with whatever arguments you give it.
56
+ ```
57
+
58
+ There is also an equivalent `predicate` method:
59
+
60
+ ```ruby
61
+ namespace :tweet do
62
+ def predicate
63
+ false
64
+ end
65
+ end
66
+ user = User.new
67
+ user.tweet? # Will call the `#predicate` method.
68
+ ```
69
+
47
70
  ## Installation
48
71
 
49
72
  Add this line to your application's Gemfile:
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Spaced
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.1"
5
5
  end
data/lib/spaced.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "spaced/version"
4
- require "forwardable"
5
4
 
6
5
  module Spaced
7
6
  def self.included(base)
@@ -20,7 +19,7 @@ module Spaced
20
19
  raise "#{klass} must be a subclass of Spaced::Base" unless klass < Spaced::Base
21
20
  else
22
21
  class_name = name.to_s.split("_").collect(&:capitalize).join
23
- klass = eval <<-RUBY, binding, __FILE__, __LINE__ + 1 # rubocop:disable Security/Eval
22
+ klass = module_eval <<-RUBY, __FILE__, __LINE__ + 1
24
23
  #{self}::#{class_name} = Class.new(Base, &) # Parent::Namespace = Class.new(Base, &)
25
24
  RUBY
26
25
  end
@@ -41,21 +40,22 @@ module Spaced
41
40
  # Define the bang and predicate methods.
42
41
  methods = klass.instance_methods(false)
43
42
 
44
- if methods.include?(:call) || methods.include?(:predicate)
45
- extend Forwardable
46
- def_delegator :"#{name}", :call, :"#{name}!" if methods.include?(:call)
47
- def_delegator :"#{name}", :predicate, :"#{name}?" if methods.include?(:predicate)
43
+ if methods.include?(:call)
44
+ module_eval <<-RUBY, __FILE__, __LINE__ + 1
45
+ def #{name}!(...); #{name}.call(...); end # def user!(...); user.call(...); end
46
+ RUBY
48
47
  else
49
- unless methods.include?(:call)
50
- define_method :"#{name}!" do
51
- raise NoMethodError, "undefined method `#{name}!' for #<#{klass}>. Have you defined `#{klass}#call`?"
52
- end
48
+ define_method :"#{name}!" do
49
+ raise NoMethodError, "undefined method `#{name}!' for #<#{klass}>. Have you defined `#{klass}#call`?", caller
53
50
  end
51
+ end
54
52
 
55
- unless methods.include?(:predicate)
56
- define_method :"#{name}?" do
57
- raise NoMethodError, "undefined method `#{name}?' for #<#{klass}>. Have you defined `#{klass}#predicate`?"
58
- end
53
+ if methods.include?(:predicate)
54
+ define_method(:"#{name}?") { send(name).predicate }
55
+ else
56
+ define_method :"#{name}?" do
57
+ raise NoMethodError, "undefined method `#{name}?' for #<#{klass}>. Have you defined `#{klass}#predicate`?",
58
+ caller
59
59
  end
60
60
  end
61
61
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spaced
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Moss
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-05 00:00:00.000000000 Z
11
+ date: 2022-10-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: