spaced 0.3.0 → 0.4.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
  SHA256:
3
- metadata.gz: 4be5f8b778afea6200764ac30e9588cd76ebedd1e6414c3abf7c2b57fadf9fc1
4
- data.tar.gz: 01de2584949b4f50211847ba3a2fd11081f7d2e8b30769117d6016ed665596c7
3
+ metadata.gz: a2da420b63a66615b4f9227fb1064c16e443b120ac506b1ebaeb820ddfb80216
4
+ data.tar.gz: 0b52d7dd0de000a0895a98f42d1cac4e1a5fb426b1865eb48c923fecc40c4654
5
5
  SHA512:
6
- metadata.gz: 29031788d5ab97c24364206b47583f63d37ed9f2186b541cd10003d7cdfd94f96711d638b485823085d400301d4e8c37302b587f40d4ee1e2cb4a674c2a3e6b6
7
- data.tar.gz: f0854fa0e2813f899fc32b85e57c72b1f44845aa4574f7c419ce5e7ba5f65ad0dfd5b07e4c6de55f2514b95da1abd51627bbec0c0363e936a650a0ace847e06b
6
+ metadata.gz: e5c2ebb06e5298dcdfb3e9d4654fb3397e5d91c32cfd7a2bac1f2426f23d7f256fba70f948a7ccc5adcbb9dd5728db76acec16b05b7803f07f18f33a997d8326
7
+ data.tar.gz: 5e745b116152c80528a3f3c2c585a826925edff33458a368188d992ba3f86883950f2016c53d5fb9e8cf1d7e47d222d5ccda0c8910545562e1c3436263e9ba57
data/.rubocop.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  AllCops:
2
2
  TargetRubyVersion: 3.1.0
3
- SuggestExtensions: false
4
3
  NewCops: enable
5
4
 
6
5
  Style/StringLiterals:
@@ -15,5 +14,8 @@ Layout/LineLength:
15
14
  Max: 120
16
15
  Metrics/MethodLength:
17
16
  Max: 20
17
+ Metrics/BlockLength:
18
+ Exclude:
19
+ - test/**/*
18
20
  Style/Documentation:
19
- Enabled: false
21
+ Enabled: false
data/Gemfile CHANGED
@@ -5,8 +5,8 @@ source "https://rubygems.org"
5
5
  # Specify your gem's dependencies in spaced.gemspec
6
6
  gemspec
7
7
 
8
- gem "minitest", "~> 5.0"
9
- gem "minitest-focus"
10
8
  gem "rake", "~> 13.0"
11
9
  gem "rubocop", "~> 1.21"
12
10
  gem "rubocop-minitest"
11
+ gem "rubocop-rake"
12
+ gem "sus", group: :test
data/Gemfile.lock CHANGED
@@ -1,48 +1,55 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- spaced (0.2.0)
4
+ spaced (0.4.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  ast (2.4.2)
10
- minitest (5.15.0)
11
- minitest-focus (1.3.1)
12
- minitest (>= 4, < 6)
13
- parallel (1.22.1)
14
- parser (3.1.2.0)
10
+ json (2.7.1)
11
+ language_server-protocol (3.17.0.3)
12
+ parallel (1.23.0)
13
+ parser (3.2.2.4)
15
14
  ast (~> 2.4.1)
15
+ racc
16
+ racc (1.7.3)
16
17
  rainbow (3.1.1)
17
- rake (13.0.6)
18
- regexp_parser (2.4.0)
19
- rexml (3.2.5)
20
- rubocop (1.30.0)
18
+ rake (13.1.0)
19
+ regexp_parser (2.8.3)
20
+ rexml (3.2.6)
21
+ rubocop (1.58.0)
22
+ json (~> 2.3)
23
+ language_server-protocol (>= 3.17.0)
21
24
  parallel (~> 1.10)
22
- parser (>= 3.1.0.0)
25
+ parser (>= 3.2.2.4)
23
26
  rainbow (>= 2.2.2, < 4.0)
24
27
  regexp_parser (>= 1.8, < 3.0)
25
28
  rexml (>= 3.2.5, < 4.0)
26
- rubocop-ast (>= 1.18.0, < 2.0)
29
+ rubocop-ast (>= 1.30.0, < 2.0)
27
30
  ruby-progressbar (~> 1.7)
28
- unicode-display_width (>= 1.4.0, < 3.0)
29
- rubocop-ast (1.18.0)
30
- parser (>= 3.1.1.0)
31
- rubocop-minitest (0.19.1)
32
- rubocop (>= 0.90, < 2.0)
33
- ruby-progressbar (1.11.0)
34
- unicode-display_width (2.1.0)
31
+ unicode-display_width (>= 2.4.0, < 3.0)
32
+ rubocop-ast (1.30.0)
33
+ parser (>= 3.2.1.0)
34
+ rubocop-minitest (0.33.0)
35
+ rubocop (>= 1.39, < 2.0)
36
+ rubocop-rake (0.6.0)
37
+ rubocop (~> 1.0)
38
+ ruby-progressbar (1.13.0)
39
+ sus (0.24.0)
40
+ unicode-display_width (2.5.0)
35
41
 
36
42
  PLATFORMS
37
43
  arm64-darwin-21
44
+ arm64-darwin-22
38
45
 
39
46
  DEPENDENCIES
40
- minitest (~> 5.0)
41
- minitest-focus
42
47
  rake (~> 13.0)
43
48
  rubocop (~> 1.21)
44
49
  rubocop-minitest
50
+ rubocop-rake
45
51
  spaced!
52
+ sus
46
53
 
47
54
  BUNDLED WITH
48
55
  2.3.7
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,58 @@ 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 `#_!` or `#call` method in your namespaced class, you can then conveniently call that with a bang method:
42
+
43
+ ```ruby
44
+ class User
45
+ include Spaced
46
+
47
+ namespace :tweet do
48
+ def _!(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 `#_!` 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 _?
63
+ false
64
+ end
65
+ end
66
+ user = User.new
67
+ user.tweet? # Will call the `#predicate` method.
68
+ ```
69
+
70
+ Both bang and predicate methods can be defined using identically named methods:
71
+
72
+ ```ruby
73
+ namespace :tweet do
74
+ def tweet?
75
+ false
76
+ end
77
+
78
+ def tweet!
79
+ # ...
80
+ end
81
+ end
82
+ user = User.new
83
+ user.tweet? # Will call the `#tweet?` method on `:tweet` namespace.
84
+ user.tweet! # Will call the `#tweet!` method on `:tweet` namespace.
85
+ ```
86
+
47
87
  ## Installation
48
88
 
49
89
  Add this line to your application's Gemfile:
data/Rakefile CHANGED
@@ -1,16 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "bundler/gem_tasks"
4
- require "rake/testtask"
4
+ require "rubocop/rake_task"
5
5
 
6
- Rake::TestTask.new(:test) do |t|
7
- t.libs << "test"
8
- t.libs << "lib"
9
- t.test_files = FileList["test/**/test_*.rb"]
6
+ task :test do
7
+ sh "bundle exec sus"
10
8
  end
11
9
 
12
- require "rubocop/rake_task"
13
-
14
10
  RuboCop::RakeTask.new
15
11
 
16
12
  task default: %i[test rubocop]
data/fixtures/daddy.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Daddy < Spaced::Base
4
+ def full_name
5
+ "Kevin Moss"
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spaced"
4
+ require "bundler"
5
+
6
+ Bundler.require :test
7
+
8
+ require "daddy"
9
+ require "user"
data/fixtures/user.rb ADDED
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ class User
4
+ include Spaced
5
+
6
+ namespace :mum do
7
+ def full_name
8
+ "Lesley Moss"
9
+ end
10
+ end
11
+
12
+ namespace :dad, Daddy
13
+ namespace :brother do
14
+ def call(append = nil)
15
+ "Andy Moss#{append}"
16
+ end
17
+
18
+ def predicate
19
+ true
20
+ end
21
+ end
22
+
23
+ namespace :sister do
24
+ def sister!(append = nil)
25
+ "Alex Moss#{append}"
26
+ end
27
+
28
+ def sister?
29
+ true
30
+ end
31
+ end
32
+
33
+ namespace :mother do
34
+ def _!(append = nil)
35
+ "Lesley Moss#{append}"
36
+ end
37
+
38
+ def _?
39
+ true
40
+ end
41
+ end
42
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Spaced
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
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)
@@ -15,12 +14,12 @@ module Spaced
15
14
  end
16
15
 
17
16
  module ClassMethods
18
- def namespace(name, klass = nil, &)
17
+ def namespace(name, klass = nil, &) # rubocop:disable Metrics/*
19
18
  if klass
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,42 @@ 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?(:_!)
44
+ module_eval <<-RUBY, __FILE__, __LINE__ + 1
45
+ def #{name}!(...); #{name}._!(...); end # def user!; user._!; end
46
+ RUBY
47
+ elsif methods.include?(:"#{name}!")
48
+ module_eval <<-RUBY, __FILE__, __LINE__ + 1
49
+ def #{name}!(...); #{name}.#{name}!(...); end # def user!; user.user!; end
50
+ RUBY
51
+ elsif methods.include?(:call) # DEPRECATED
52
+ module_eval <<-RUBY, __FILE__, __LINE__ + 1
53
+ def #{name}!(...); #{name}.call(...); end # def user!(...); user.call(...); end
54
+ RUBY
48
55
  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
56
+ define_method :"#{name}!" do
57
+ raise NoMethodError,
58
+ "undefined method `#{name}!' for #<#{klass}>. Have you defined `#{klass}#_!`?", caller
53
59
  end
60
+ end
54
61
 
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
62
+ if methods.include?(:_?)
63
+ module_eval <<-RUBY, __FILE__, __LINE__ + 1
64
+ def #{name}?; #{name}._?; end # def user?; user._?; end
65
+ RUBY
66
+ elsif methods.include?(:"#{name}?")
67
+ module_eval <<-RUBY, __FILE__, __LINE__ + 1
68
+ def #{name}?; #{name}.#{name}?; end # def user?; user.user?; end
69
+ RUBY
70
+ elsif methods.include?(:predicate) # DEPRECATED
71
+ module_eval <<-RUBY, __FILE__, __LINE__ + 1
72
+ def #{name}?; #{name}.predicate; end # def user?; user.predicate; end
73
+ RUBY
74
+ else
75
+ define_method :"#{name}?" do
76
+ raise NoMethodError,
77
+ "undefined method `#{name}?' for #<#{klass}>. Have you defined `#{klass}#_?`?",
78
+ caller
59
79
  end
60
80
  end
61
81
  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.4.0
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: 2023-12-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -24,6 +24,9 @@ files:
24
24
  - LICENSE.txt
25
25
  - README.md
26
26
  - Rakefile
27
+ - fixtures/daddy.rb
28
+ - fixtures/test_helper.rb
29
+ - fixtures/user.rb
27
30
  - lib/spaced.rb
28
31
  - lib/spaced/version.rb
29
32
  - sig/spaced.rbs
@@ -50,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
53
  - !ruby/object:Gem::Version
51
54
  version: '0'
52
55
  requirements: []
53
- rubygems_version: 3.3.7
56
+ rubygems_version: 3.4.22
54
57
  signing_key:
55
58
  specification_version: 4
56
59
  summary: Super simple and convenient way to isolate and namespace a collection of