pathtree 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: 2d8abe169d75eec0e468f2632575fea2087681a73a60792f58bdb13519b71633
4
- data.tar.gz: 82d71231c2b80f0ec8ebaea69677b2d12c6ee2ebfcb0aec83accf6c67255b1df
3
+ metadata.gz: 2fa0b4b00d649556ade2438672e8f579096425641c87dc12904c0b1e83896f4c
4
+ data.tar.gz: a20dc412a32816c46688ae0ee426f1ee19d954290d3def13f64d50099a737956
5
5
  SHA512:
6
- metadata.gz: 30599d8e1979cfa9f5589985da12e7711ddbffe60e7407f877f3b9dec78185bff29876951791f8525d0531d9a94d17b47a86a5ed988ea41608fb7c60e7b4d18a
7
- data.tar.gz: 128c8141f301f80b321af22171203c7bad4c666a03e51b8a477445f6a37ec177cc890026acdf069afdca346f37745341c844085cd9b19a59c81ed2b69edf0ee9
6
+ metadata.gz: 19b2cf1822c9380fbd6161ee50b3fd57990811d93331ddd22530982907654b2e6dee118715da9ccd76c5921afd01249a7fa1357b30886cab3f4ce45430f2f53f
7
+ data.tar.gz: 01bda1e63a6dcc19803a10857cb581d7457a87329e1c2592cfb355a6f5a4e235e301eea171f22a4fd613afcdaff69a6369433824c5e4595160ceee3660d6fd94
data/CHANGELOG.md CHANGED
@@ -7,9 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## Unreleased
9
9
 
10
+ ## 0.4.0 - 2022-05-14
11
+
12
+ ### Changed in 0.4.0
13
+
14
+ * Moved `Pathtree::Dsl.read` and `Pathtree::Dsl.load` to `Pathtree.read` and `Pathree.load`.
15
+ * If root path is not given, `Pathtree::Trunk` is used, rather than `Pathname.pwd`.
16
+
17
+ ### Removed in 0.4.0
18
+
19
+ * Refinements for `String`, `Pathname`, and `Object`
20
+
10
21
  ## 0.3.0 - 2021-12-12
11
22
 
12
- ### Added
23
+ ### Added in 0.3.0
13
24
 
14
25
  * GitLab CI which checks tests with Minitest and lint rules with RuboCop
15
26
  * Enable to pass `Proc` object to `path` argument
@@ -22,20 +33,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
22
33
  * `Pathtree::StringRefinements#split_extension`
23
34
  * `Pathtree::ObjectRefinements#file_path` and `Pathtree::ObjectRefinements#directory_path`
24
35
 
25
- ### Removed
36
+ ### Removed in 0.3.0
26
37
 
27
38
  * `Pathtree::SymbolRefinements`
28
39
  * Move to `Pathtree::StringRefinements#split_extension`
29
40
  * `dot` option for `Pathtree::Dsl#file` and `Pathtree::Dsl#directory`
30
41
  * Use `Proc` for path instead
31
42
 
32
- ### Changed
43
+ ### Changed in 0.3.0
33
44
 
34
45
  * Visibility of `Pathtree::Dsl#read` to private
35
46
 
36
47
  ## 0.2.0 - 2021-11-20
37
48
 
38
- ### Changed
49
+ ### Changed in 0.2.0
39
50
 
40
51
  * `Pathtree` inherits `Pathname`
41
52
 
@@ -55,14 +66,14 @@ Pathtree::Dsl.read('paths.rb').aaa.then { path(_1) } # $PWD/aaa
55
66
  Pathtree::Dsl.read('paths.rb').aaa # $PWD/aaa
56
67
  ```
57
68
 
58
- ### Removed
69
+ ### Removed in 0.2.0
59
70
 
60
71
  * `KernelRefinements` since `Pathtree` directory object is now `Pathtree` itself
61
72
  * Instance reader attribute `@tree` of `Pathtree::Dsl`
62
73
 
63
74
  ## 0.1.1 - 2021-11-20
64
75
 
65
- ### Added
76
+ ### Added in 0.1.1
66
77
 
67
78
  * `dot` option for `Pathtree::Dsl#file` and `Pathtree::Dsl#directory`.
68
79
  If `dot: true`, then path name is prefixed with dot (period) character
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Pathtree
2
2
 
3
- Tree structure of Pathname
3
+ Pathname DSL
4
4
 
5
5
  ## Installation
6
6
 
@@ -24,13 +24,40 @@ gem install pathtree
24
24
 
25
25
  ## Usage
26
26
 
27
- See tests.
27
+ `path.rb`:
28
+
29
+ ```ruby
30
+ file :aaa
31
+
32
+ directory :public do
33
+ file :bbb
34
+ end
35
+ ```
36
+
37
+ `Rakefile`:
38
+
39
+ ```ruby
40
+ require 'pathtree'
41
+
42
+ P = Pathtree.read('path.rb')
43
+
44
+ file P.public.bbb => P.aaa do |t|
45
+ cp t.source, t.name
46
+ do
47
+ ```
28
48
 
29
49
  ## Development
30
50
 
31
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
51
+ After checking out the repo, run `bin/setup` to install dependencies.
52
+ Then, run `rake test` to run the tests.
53
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
54
 
33
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
55
+ To install this gem onto your local machine, run `bundle exec rake install`.
56
+ To release a new version, update the version number in `version.rb`,
57
+ and then run `bundle exec rake release`,
58
+ which will create a git tag for the version,
59
+ push git commits and the created tag,
60
+ and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
61
 
35
62
  ## Contributing
36
63
 
@@ -38,4 +65,6 @@ Bug reports and pull requests are welcome on GitLab at <https://gitlab.com/gemma
38
65
 
39
66
  ## License
40
67
 
41
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
68
+ The gem is available as open source under the terms of the [MIT License][mit].
69
+
70
+ [mit]: https://opensource.org/licenses/MIT
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+
5
+ module Pathtree
6
+ # DSL for constructing path tree
7
+ module Dsl
8
+ attr_reader :names
9
+
10
+ # Grows file leaf from the branch;
11
+ # The path at the point where this method is called
12
+ # attempts to create an instance method that returns the path with the +name+.
13
+ #
14
+ # * If +path+ is +nil+, +name+ is used instead.
15
+ # * If +path+ is a +Proc+, give +name+ to it.
16
+ # * Otherwise, +path+ itself is used.
17
+ def file(name, path = nil)
18
+ check_name(name)
19
+ path = normalize(name: name, path: path)
20
+ path = @root / path if @root
21
+ @names << name
22
+ define_singleton_method(name) { path }
23
+ end
24
+
25
+ # Grows directory branch from the branch.
26
+ # See also Pathtree::Dsl#file method.
27
+ # This method has optional block, which represents nested file structure.
28
+ def directory(name, path = nil, &block)
29
+ check_name(name)
30
+ path = normalize(name: name, path: path)
31
+ path = @root / path if @root
32
+ @names << name
33
+
34
+ define_singleton_method(name) do
35
+ path.tap do |p|
36
+ Dsl.extend(p, root: path)
37
+ p.instance_eval(&block) if block
38
+ end
39
+ end
40
+ end
41
+
42
+ alias dir directory
43
+
44
+ def self.extend(path, root: nil)
45
+ path.instance_variable_set(:@names, [])
46
+ path.instance_variable_set(:@root, root)
47
+ path.extend(Dsl)
48
+ path
49
+ end
50
+
51
+ private
52
+
53
+ def check_name(name)
54
+ raise MethodDuplicationError, name if respond_to?(name)
55
+ end
56
+
57
+ def normalize(name:, path: nil)
58
+ if path.nil?
59
+ Pathname(name.to_s)
60
+ elsif path.is_a?(String)
61
+ Pathname(path)
62
+ elsif path.respond_to?(:call)
63
+ path.call(name).then { normalize(name: _1) }
64
+ else
65
+ path
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+ require_relative 'dsl'
5
+
6
+ module Pathtree
7
+ # A trunk of path tree
8
+ class Trunk
9
+ include Dsl
10
+
11
+ def initialize
12
+ @names = []
13
+ @root = nil
14
+ end
15
+ end
16
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  require 'pathname'
4
4
 
5
- class Pathtree < Pathname
6
- VERSION = '0.3.0'
5
+ module Pathtree
6
+ VERSION = '0.4.0'
7
7
  end
data/lib/pathtree.rb CHANGED
@@ -1,102 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'pathname'
4
3
  require_relative 'pathtree/version'
4
+ require_relative 'pathtree/trunk'
5
5
 
6
- class Pathtree < Pathname
7
- module StringRefinements
8
- refine String do
9
- def prefix_dot
10
- return self if start_with?('.')
6
+ # Pathname DSL
7
+ module Pathtree
8
+ class MethodDuplicationError < NameError; end
11
9
 
12
- ".#{self}"
13
- end
14
-
15
- # Split at last underscore `_` and turn last part into file extension
16
- #
17
- # "aaa_bbb_ccc".split_extension #=> aaa_bbb.ccc
18
- def split_extension
19
- sub(/\A[a-z0-9_]*\K_(?=[a-z0-9]+\Z)/, '.')
20
- end
21
- end
10
+ # Read Pathtree DSL from +path+.
11
+ # +root+ is base directory for tree.
12
+ def self.read(path, root: nil)
13
+ dsl = File.read(path)
14
+ self.load(dsl, root: root)
22
15
  end
23
16
 
24
- module ObjectRefinements
25
- using StringRefinements
26
-
27
- refine Object do
28
- # Use Pathtree::StringRefinements#split_extension by default
29
- def file_path(name:)
30
- path(name: name, proc_for_nil: -> { _1.split_extension })
31
- end
32
-
33
- def directory_path(name:)
34
- path(name: name, proc_for_nil: -> { _1 })
35
- end
36
-
37
- private
38
-
39
- def path(name:, proc_for_nil:) # :nodoc:
40
- case self
41
- in String
42
- self
43
- in NilClass
44
- proc_for_nil.call(name.to_s)
45
- in Proc
46
- call(name.to_s)
47
- end
48
- end
49
- end
17
+ # Load Pathtree DSL source.
18
+ # +root+ is base directory for tree.
19
+ def self.load(dsl, root: nil)
20
+ (root ? to_path(root) : Trunk.new).tap { _1.instance_eval(dsl) }
50
21
  end
51
22
 
52
- class Dsl
53
- def self.read(file_path, working_directory: Pathname.pwd)
54
- new(working_directory: working_directory)
55
- .tap { _1.send(:read, file_path) }
56
- .instance_variable_get(:@working_directory)
57
- end
58
-
59
- # See also Pathtree::Dsl.read.
60
- def initialize(working_directory: Pathname.pwd)
61
- @working_directory = Pathtree.new(working_directory)
62
- end
63
-
64
- using ObjectRefinements
65
-
66
- def file(name, path = nil)
67
- path = path.file_path(name: name)
68
-
69
- # NOTE: Temporal variable is needed
70
- # since define_singleton_method's block is evaluated using instance_eval
71
- # which is same as define_method.
72
- working_directory = @working_directory
73
-
74
- @working_directory.define_singleton_method(name) { working_directory / path }
75
- end
76
-
77
- def directory(name, path = nil, &block)
78
- path = path.directory_path(name: name)
79
-
80
- # NOTE: Temporal variable is needed
81
- # since define_singleton_method's block is evaluated using instance_eval
82
- # which is same as define_method.
83
- working_directory = @working_directory / path
84
-
85
- @working_directory.define_singleton_method(name) do
86
- Dsl.new(working_directory: working_directory)
87
- .tap { _1.instance_eval(&block) if block_given? }
88
- .instance_variable_get(:@working_directory)
89
- end
90
- end
91
-
92
- alias dir directory
93
-
23
+ class << self
94
24
  private
95
25
 
96
- using StringRefinements
97
-
98
- def read(file_path)
99
- instance_eval File.read(file_path)
26
+ def to_path(value)
27
+ value = Pathname(value) if value.is_a?(String)
28
+ Dsl.extend(value, root: value)
100
29
  end
101
30
  end
102
31
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pathtree
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
  - gemmaro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-12 00:00:00.000000000 Z
11
+ date: 2022-05-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Tree structure of Pathname
13
+ description: Pathname DSL
14
14
  email:
15
15
  - gemmaro.dev@gmail.com
16
16
  executables: []
@@ -21,6 +21,8 @@ files:
21
21
  - LICENSE.txt
22
22
  - README.md
23
23
  - lib/pathtree.rb
24
+ - lib/pathtree/dsl.rb
25
+ - lib/pathtree/trunk.rb
24
26
  - lib/pathtree/version.rb
25
27
  homepage: https://gitlab.com/gemmaro/pathtree
26
28
  licenses:
@@ -42,8 +44,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
42
44
  - !ruby/object:Gem::Version
43
45
  version: '0'
44
46
  requirements: []
45
- rubygems_version: 3.2.32
47
+ rubygems_version: 3.3.7
46
48
  signing_key:
47
49
  specification_version: 4
48
- summary: Tree structure of Pathname
50
+ summary: Pathname DSL
49
51
  test_files: []