alias2 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +47 -4
  3. data/lib/alias2.rb +33 -6
  4. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 2ca79b58531d2f43d184204d979e460be1132e27
4
- data.tar.gz: 414e6aedd76c1b46a7ceb0c6dedfe264003d8769
2
+ SHA256:
3
+ metadata.gz: '086b102714da0bad17fa027730bab6e2b5db8ed1dbd76e4813cc94754e0b4324'
4
+ data.tar.gz: 91c87e7e03ed79033b487cb45d4c0aac9a579f5bccecebe02f3a52fb3dba2a38
5
5
  SHA512:
6
- metadata.gz: 513da498fe3e3e4c61ef3702eae7ff711790d9a0e4d6802560c08595205eb8ab7270119e2bdc489f23f8cd9bd32f3fd766e5f9001ab54abb39695e893b027cd0
7
- data.tar.gz: 648985efa3ef7b101f03b9f4c80a1b8f0a9781148ba3d3d9d64b0cc9906ffddbdb37920dae89e89d1bddfbbcf9defb45c4758147d2fa74f9bf8f63968ce81302
6
+ metadata.gz: c737a1662da7810bb640f830870a1a23227299dde7ddcf1c7a134ee6275413b517515b70104dbe42dc5b4228649626433c2e37e25f8af9da71ee6841188cb01d
7
+ data.tar.gz: 2b499f4df51047583bfd0da28377f4e8403fa2f408a7aa4a9f5189891ca9a67f0f0e01f48ee162de849fec0f4c7ae0866f6bcae986285dc01e2d5931d8c69a02
data/README.md CHANGED
@@ -25,6 +25,18 @@ Baz = Some::Very::Long::Name::Here::Baz
25
25
 
26
26
  The namespace can also be a `String`.
27
27
 
28
+ To alias everything in the namespace to the top-level:
29
+ ```rb
30
+ alias2 Some::Very::Long::Name::Here, "*"
31
+ ```
32
+
33
+ Same as above:
34
+ ```rb
35
+ Foo = Some::Very::Long::Name::Here::Foo
36
+ Bar = Some::Very::Long::Name::Here::Bar
37
+ Baz = Some::Very::Long::Name::Here::Baz
38
+ ```
39
+
28
40
  If you'd like to alias them using a different name you can:
29
41
  ```rb
30
42
  alias2 Some::Very::Long::Name::Here, :Foo => "FooHoo", :Bar => "BarHar", :Baz => "Bazzzz"
@@ -34,12 +46,42 @@ Same as:
34
46
  ```rb
35
47
  FooHoo = Some::Very::Long::Name::Here::Foo
36
48
  BarHar = Some::Very::Long::Name::Here::Bar
37
- Bazzzz = Some::Very::Long::Name::Here::Bar
49
+ Bazzzz = Some::Very::Long::Name::Here::Baz
38
50
  ```
39
51
 
40
52
  Keys can also be `String`s.
41
53
 
42
- The target can be a namespace you want created:
54
+ You can filter the classes/modules to alias:
55
+ ```rb
56
+ alias2 Some::Very::Long::Name::Here, "*" do |klass|
57
+ klass < ActiveRecord::Base
58
+ end
59
+ ```
60
+
61
+ The above is the same as:
62
+ ```rb
63
+ Foo = Some::Very::Long::Name::Here::Foo if Some::Very::Long::Name::Here::Foo.is_a?(ActiveRecord::Base)
64
+ Bar = Some::Very::Long::Name::Here::Bar if Some::Very::Long::Name::Here::Bar.is_a?(ActiveRecord::Base)
65
+ Baz = Some::Very::Long::Name::Here::Baz if Some::Very::Long::Name::Here::Baz.is_a?(ActiveRecord::Base)
66
+ ```
67
+
68
+ When a block is given you can omit the alias. It will default to `"*"`.
69
+
70
+ The block can also return an alias:
71
+ ```rb
72
+ alias2 Some::Very::Long::Name::Here do |klass|
73
+ klass.name.end_with?("Foo") ? "Foo_X" : klass.name.split("::")[-1]
74
+ end
75
+ ```
76
+
77
+ This is the same as:
78
+ ```rb
79
+ Foo_X = Some::Very::Long::Name::Here::Foo if Some::Very::Long::Name::Here::Foo.name.end_with?("Foo")
80
+ Bar = Some::Very::Long::Name::Here::Bar
81
+ Baz = Some::Very::Long::Name::Here::Baz
82
+ ```
83
+
84
+ An alias' target can also be a namespace you want created:
43
85
  ```rb
44
86
  alias2 Some::Very::Long::Name::Here, :Foo => "New::Namespace::SameFoo", :Bar => "BarHar"
45
87
  ```
@@ -70,12 +112,13 @@ In all cases the original namespace is not modified and remains in scope.
70
112
 
71
113
  ### Errors
72
114
 
73
- A `NameError` is raised when a constant cannot be found or when a constant is already defined.
115
+ - `NameError` - raised when a constant cannot be found or when a constant is already defined
116
+ - `ArgumentError` - raised when an alias list or block is not provided
74
117
 
75
118
  ## See Also
76
119
 
77
120
  * [aliased](https://metacpan.org/pod/aliased) - The Perl module that served as inspiration
78
- * [require2](https://github.com/sshaw/require2) - `Kernel#require` something and make it accessible via a different namespace
121
+ * [require3](https://github.com/sshaw/require3) - `Kernel#require` something and make it accessible via a different namespace
79
122
  * [class2](https://github.com/sshaw/class2) - Easily create hierarchies that support nested attributes, type conversion, serialization and more
80
123
 
81
124
  ## Author
@@ -1,13 +1,30 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Alias2
2
- VERSION = "0.0.1".freeze
4
+ VERSION = "0.1.0".freeze
3
5
 
4
6
  class << self
5
- def alias(namespace, aliases)
7
+ def alias(namespace, aliases = nil)
8
+ raise ArgumentError, "you must provide an alias or a block" if aliases.nil? && !block_given?
9
+
6
10
  namespace = constant(namespace)
11
+ aliases = find_names(namespace) if aliases.nil? || aliases == "*"
12
+
7
13
  if aliases.is_a?(String)
8
14
  set(namespace, aliases)
9
- else
10
- aliases.each { |target, alias_as| set(namespace.const_get(target), (alias_as || target).to_s) }
15
+ return
16
+ end
17
+
18
+ aliases.each do |target, alias_as|
19
+ klass = namespace.const_get(target)
20
+ if block_given?
21
+ keep_or_alias = yield klass
22
+ next unless keep_or_alias
23
+ # Block returned a new alias
24
+ alias_as = keep_or_alias if keep_or_alias != true
25
+ end
26
+
27
+ set(klass, (alias_as || target).to_s)
11
28
  end
12
29
 
13
30
  nil
@@ -20,6 +37,16 @@ module Alias2
20
37
  Object.const_get(namespace)
21
38
  end
22
39
 
40
+ def find_names(namespace)
41
+ names = []
42
+ namespace.constants.each do |name|
43
+ const = namespace.const_get(name)
44
+ names << name if const.is_a?(Module)
45
+ end
46
+
47
+ names
48
+ end
49
+
23
50
  # Stolen from class2: https://github.com/sshaw/class2
24
51
  def find_and_or_create_namespace(str)
25
52
  parts = str.split("::")
@@ -51,7 +78,7 @@ module Alias2
51
78
  end
52
79
 
53
80
  unless %w[true 1].include?(ENV["ALIAS2_NO_EXPORT"])
54
- def alias2(target, alias_as)
55
- Alias2.alias(target, alias_as)
81
+ def alias2(target, alias_as = nil, &block)
82
+ Alias2.alias(target, alias_as, &block)
56
83
  end
57
84
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alias2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Skye Shaw
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-03 00:00:00.000000000 Z
11
+ date: 2019-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -85,8 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
85
  - !ruby/object:Gem::Version
86
86
  version: '0'
87
87
  requirements: []
88
- rubyforge_project:
89
- rubygems_version: 2.6.14
88
+ rubygems_version: 3.0.3
90
89
  signing_key:
91
90
  specification_version: 4
92
91
  summary: Make classes, modules, and constants accessible via a different namespace