spaced 0.3.0 → 0.3.1
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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +34 -11
- data/lib/spaced/version.rb +1 -1
- data/lib/spaced.rb +14 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 496fc25b4cc4a7b4d8332661ace97b36a26ee5cb9592bfeb06e1743483c47c3b
|
4
|
+
data.tar.gz: 96b4ddf6c1c5e89ead4f894ced1f71c2b2a671c7838766572f94f85462bedf82
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4751df5962de7c88361b534dff26007baeed5a91bd8a4f796aa75d6ff31f5a48c10c8b9dc8d53528af00f9089cc8bc8865b3c97b209227cb5c9dab96f84f4c33
|
7
|
+
data.tar.gz: eb9fd26c2e44f605bf9c9f733b22263ff86a0f5fd4d8003fe1457544b3a4b2a7f08ac13b3132a7447230c3017aab6c1c4bce5d407ee4bc56d53a6fc4dd86f8ef
|
data/Gemfile.lock
CHANGED
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:
|
data/lib/spaced/version.rb
CHANGED
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 =
|
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)
|
45
|
-
|
46
|
-
|
47
|
-
|
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
|
-
|
50
|
-
|
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
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
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.
|
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-
|
11
|
+
date: 2022-10-06 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|