expand 1.0.3 → 1.0.5

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: 2ea9a1efdd9f3eb0013dcf094347e32d3c48aeb6d819d95d76a368c14de20862
4
- data.tar.gz: d21f446e4e5a1d793da4cc23c9017d6e15ba80066905cd302e9be33e25cfff2f
3
+ metadata.gz: 1c749a422399f6bef2c67bd4ab1503cfd5d3ab99a912b2164c6930931d2861ad
4
+ data.tar.gz: e5639cd2f02dae004d3e7d60eac7a2a3eeace870eb2022ca778d785d451b5d20
5
5
  SHA512:
6
- metadata.gz: 0660ff8939459c77f34920c14d5b678ed5a8703bc3dfab0b2a523afd75a07b516e9d25596b0ca333ad3c3f52ae1fd623047bb4033961a68e306fef25970f8401
7
- data.tar.gz: 3e51316a7a7a0b7c5ca31de138b6c09ebba2446f450166ce1a43ec4a1912b86f1cce7baf3c3e063bd05bd55c34513b67304a8ac99ddeb068b830102afde4d0ad
6
+ metadata.gz: 3fd15dae2380332348db1806ae0ec289ac57a936404df715d2cb0418cbafd009f213df5d958699510b0d4eed668355ffcba681feb8927adb5fa5e24f87dc0700
7
+ data.tar.gz: e4197b51d7e9c5c352492c418bae31e385efb17aac68ef0db5d5fe2fbcebe5b9e6397e227d3d0911a81f1fc9044b77412eaf2e3964c1785473bae8f35a00905b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## 1.0.5
2
+
3
+ - use pattern matching in the `namespace` method
4
+ - add an `apply` method to eval blocks in the Manager object
5
+
6
+ ## 1.0.4
7
+
8
+ - use a module function `Expand.namespace` so you don't need to extend
9
+
1
10
  ## 1.0.3
2
11
 
3
12
  - allow the creation of a module or class in the namespace/expand method itself.
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2016 'Jim Gay'
3
+ Copyright (c) 2016-2023 'Jim Gay'
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -19,22 +19,19 @@ end
19
19
  SomeGem::Thing::NewThing #=> warning: toplevel constant NewThing referenced by SomeGem::Thing::NewThing
20
20
  ```
21
21
 
22
- To make this simpler, you can require `expand` and extend your current context with Expand.
22
+ To make this simpler, you can require `expand` and specify the namespace and the new class or module.
23
23
 
24
24
  ```ruby
25
25
  require 'expand'
26
- extend Expand
27
26
 
28
- namespace SomeGem::Thing do
29
- create_class :NewThing do
30
- # define methods here
31
- end
27
+ Expand.namespace SomeGem::Thing, class: :NewThing do
28
+ # define methods for your class here
32
29
  end
33
30
 
34
31
  SomeGem::Thing::NewThing #=> SomeGem::Thing::NewThing
35
32
  ```
36
33
 
37
- And even simpler, you can do it all in one line:
34
+ You can also extend an class or object to add the `namespace` method.
38
35
 
39
36
  ```ruby
40
37
  require 'expand'
@@ -44,11 +41,28 @@ namespace SomeGem::Thing, class: :NewThing, parent: SomeExistingOtherClass do
44
41
  # define methods here
45
42
  end
46
43
 
47
- namespace SomeGem::Thing, class: :NewModule do
44
+ namespace SomeGem::Thing, module: :NewModule do
48
45
  # define methods here
49
46
  end
50
47
  ```
51
48
 
49
+ If you want more explicit code or to create multiple modules or classes under a sinngle namespace you can use `create_class` and `create_module`
50
+
51
+ ```ruby
52
+ require 'expand'
53
+ extend Expand
54
+
55
+ namespace SomeGem::Thing do
56
+ create_class :NewClass do
57
+ # define methods here
58
+ end
59
+
60
+ create_module :NewModule do
61
+ # define methods here
62
+ end
63
+ end
64
+ ```
65
+
52
66
  ## Installation
53
67
 
54
68
  Add this line to your application's Gemfile:
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require "rake/testtask"
4
4
  Rake::TestTask.new(:test) do |t|
5
5
  t.libs << "test"
6
6
  t.libs << "lib"
7
- t.test_files = FileList['test/**/*_test.rb']
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
8
  end
9
9
 
10
- task :default => :test
10
+ task default: :test
@@ -1,3 +1,3 @@
1
1
  module Expand
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.5"
3
3
  end
data/lib/expand.rb CHANGED
@@ -2,7 +2,6 @@ require "expand/version"
2
2
 
3
3
  # Extend your classes or modules to use the Expand methods
4
4
  module Expand
5
-
6
5
  # The primary way to access Manager objects is to use the Expand namespace method.
7
6
  # @see Expand#namespace
8
7
  #
@@ -48,6 +47,20 @@ module Expand
48
47
  @managed.const_set(name, klass)
49
48
  klass
50
49
  end
50
+
51
+ def apply(&block)
52
+ instance_eval(&block)
53
+ @managed
54
+ end
55
+
56
+ def self.for(context)
57
+ unless context.is_a?(Module)
58
+ context = context.to_s.split("::").inject(Object) do |base, mod|
59
+ base.const_get(mod)
60
+ end
61
+ end
62
+ new(context)
63
+ end
51
64
  end
52
65
 
53
66
  # Allows you to open a module namespace to add constants
@@ -63,7 +76,17 @@ module Expand
63
76
  # end
64
77
  # end
65
78
  #
79
+ # namespace SomeGem::Thing, class: :Other do
80
+ # # add methods here
81
+ # end
82
+ # namespace SomeGem::Thing, module: :Another do
83
+ # # add methods here
84
+ # end
85
+ #
66
86
  # @param context [Module, String, or any object responding to to_s] representing a module namespace.
87
+ # @param module: module name to create
88
+ # @param class: class name to create
89
+ # @param parent: optional parent class for the created class, defaults to Object
67
90
  # @yield the provided block is executed against an instance of Expand::Manager using instance_eval
68
91
  #
69
92
  # @return [Expand::Manager] instance which can allow you to create classes and modules in the given context.
@@ -72,28 +95,25 @@ module Expand
72
95
  # @see Expand::Manager#create_module
73
96
  #
74
97
  def namespace(context, **class_or_module, &block)
75
- unless context.is_a?(Module)
76
- context = context.to_s.split('::').inject(Object) do |base, mod|
77
- base.const_get(mod)
78
- end
79
- end
80
- manager = Manager.new(context)
98
+ manager = Manager.for(context)
81
99
 
82
- creating_class, creating_module = class_or_module[:class], class_or_module[:module]
83
- raise ArgumentError, "You must choose either class: or module: but not both." if creating_class && creating_module
84
-
85
- case
86
- when creating_class
100
+ case class_or_module
101
+ in { module: Symbol => _creating_module, class: Symbol => _creating_class }
102
+ raise ArgumentError, "You must choose either class: or module: but not both."
103
+ in { class: Symbol => creating_class, ** }
87
104
  parent = class_or_module[:parent] || Object
105
+
88
106
  manager.create_class(creating_class, parent: parent, &block)
89
- when creating_module
90
- if class_or_module[:parent]
91
- warn "An option for :parent was provided as `#{class_or_module[:parent]}' but was ignored when creating the module: #{class_or_module[:module]}"
107
+ in { module: Symbol => creating_module, ** }
108
+ if parent = class_or_module[:parent]
109
+ warn "An option for :parent was provided as `#{parent}' but was ignored when creating the module: #{creating_module}"
92
110
  end
111
+
93
112
  manager.create_module(creating_module, &block)
94
113
  else
95
- manager.instance_eval(&block)
114
+ manager.apply(&block)
96
115
  end
97
116
  end
98
- alias expand namespace
117
+ alias_method :expand, :namespace
118
+ module_function :expand, :namespace
99
119
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: expand
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
- - "'Jim Gay'"
7
+ - Jim Gay
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-20 00:00:00.000000000 Z
11
+ date: 2023-01-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Create classes and modules under an existing namespace
14
14
  email:
@@ -17,18 +17,11 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
- - ".github/dependabot.yml"
21
- - ".github/workflows/test.yml"
22
- - ".gitignore"
23
20
  - CHANGELOG.md
24
21
  - CODE_OF_CONDUCT.md
25
- - Gemfile
26
22
  - LICENSE.txt
27
23
  - README.md
28
24
  - Rakefile
29
- - bin/console
30
- - bin/setup
31
- - expand.gemspec
32
25
  - lib/expand.rb
33
26
  - lib/expand/version.rb
34
27
  homepage: https://github.com/saturnflyer/expand
@@ -50,7 +43,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
43
  - !ruby/object:Gem::Version
51
44
  version: '0'
52
45
  requirements: []
53
- rubygems_version: 3.3.17
46
+ rubygems_version: 3.4.1
54
47
  signing_key:
55
48
  specification_version: 4
56
49
  summary: Create classes and modules under an existing namespace
@@ -1,8 +0,0 @@
1
- version: 2
2
- updates:
3
- - package-ecosystem: bundler
4
- directory: "/"
5
- schedule:
6
- interval: daily
7
- time: "10:00"
8
- open-pull-requests-limit: 10
@@ -1,37 +0,0 @@
1
- # This workflow uses actions that are not certified by GitHub.
2
- # They are provided by a third-party and are governed by
3
- # separate terms of service, privacy policy, and support
4
- # documentation.
5
- # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
- # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
-
8
- name: Ruby
9
-
10
- on:
11
- push:
12
- branches: [ "master" ]
13
- pull_request:
14
- branches: [ "master" ]
15
-
16
- permissions:
17
- contents: read
18
-
19
- jobs:
20
- test:
21
-
22
- runs-on: ubuntu-latest
23
- strategy:
24
- matrix:
25
- ruby-version: ['2.6', '2.7', '3.0', '3.1']
26
-
27
- steps:
28
- - uses: actions/checkout@v3
29
- - name: Set up Ruby
30
- # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
31
- # change this to (see https://github.com/ruby/setup-ruby#versioning):
32
- uses: ruby/setup-ruby@v1
33
- with:
34
- ruby-version: ${{ matrix.ruby-version }}
35
- bundler-cache: true # runs 'bundle install' and caches installed gems automatically
36
- - name: Run tests
37
- run: bundle exec rake
data/.gitignore DELETED
@@ -1,9 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
data/Gemfile DELETED
@@ -1,8 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in expand.gemspec
4
- gemspec
5
-
6
- gem "minitest"
7
- gem "simplecov"
8
- gem "rake"
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "expand"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
data/expand.gemspec DELETED
@@ -1,21 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'expand/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "expand"
8
- spec.version = Expand::VERSION
9
- spec.authors = ["'Jim Gay'"]
10
- spec.email = ["jim@saturnflyer.com"]
11
-
12
- spec.summary = %q{Create classes and modules under an existing namespace}
13
- spec.description = %q{Create classes and modules under an existing namespace}
14
- spec.homepage = "https://github.com/saturnflyer/expand"
15
- spec.license = "MIT"
16
-
17
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
- spec.bindir = "exe"
19
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
- spec.require_paths = ["lib"]
21
- end