namelab 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 34e255356d12dbf5535ae8a05fdd282d38ecbcb704bf85d153903ec98d83420a
4
+ data.tar.gz: 9999e6c7c2d18513a11b11ef94c09b9de4ed158d01ccaa8922e091d787daec9d
5
+ SHA512:
6
+ metadata.gz: b0a6edda244cf1ba15d75afb1b998af5b4ea58a7a303d4b9a760b7efa1424dc513a5ea8a5fa4f2a09c61ec8318cf75694223ffda4aec1d804237c33d6138a165
7
+ data.tar.gz: 067e8901d7697a5850160a68dd4a8be2894fa8ea9c536eec45848ad4d67a8bfa8dbee38e0cf434f1aa19d559f9367671d71ba169eb25a31a5a757f01c0e4eaf7
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.6
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.6
7
+ before_install: gem install bundler -v 1.17.2
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in namelab.gemspec
6
+ gemspec
7
+
8
+ group :development do
9
+ gem 'pry'
10
+ gem 'pry-doc'
11
+ gem 'awesome_print'
12
+ gem 'benchmark-ips'
13
+ gem 'console_utils'
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,86 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ namelab (0.1.0)
5
+ dry-cli
6
+ dry-container
7
+ dry-initializer
8
+ dry-types
9
+ yieldable
10
+
11
+ GEM
12
+ remote: https://rubygems.org/
13
+ specs:
14
+ activesupport (6.1.3)
15
+ concurrent-ruby (~> 1.0, >= 1.0.2)
16
+ i18n (>= 1.6, < 2)
17
+ minitest (>= 5.1)
18
+ tzinfo (~> 2.0)
19
+ zeitwerk (~> 2.3)
20
+ awesome_print (1.9.2)
21
+ benchmark-ips (2.8.4)
22
+ coderay (1.1.3)
23
+ concurrent-ruby (1.1.8)
24
+ console_utils (0.10.1)
25
+ activesupport (>= 5.2, < 7)
26
+ awesome_print
27
+ benchmark-ips
28
+ pastel
29
+ pry-rails
30
+ dry-cli (0.6.0)
31
+ concurrent-ruby (~> 1.0)
32
+ dry-configurable (0.12.1)
33
+ concurrent-ruby (~> 1.0)
34
+ dry-core (~> 0.5, >= 0.5.0)
35
+ dry-container (0.7.2)
36
+ concurrent-ruby (~> 1.0)
37
+ dry-configurable (~> 0.1, >= 0.1.3)
38
+ dry-core (0.5.0)
39
+ concurrent-ruby (~> 1.0)
40
+ dry-inflector (0.2.0)
41
+ dry-initializer (3.0.4)
42
+ dry-logic (1.1.0)
43
+ concurrent-ruby (~> 1.0)
44
+ dry-core (~> 0.5, >= 0.5)
45
+ dry-types (1.5.1)
46
+ concurrent-ruby (~> 1.0)
47
+ dry-container (~> 0.3)
48
+ dry-core (~> 0.5, >= 0.5)
49
+ dry-inflector (~> 0.1, >= 0.1.2)
50
+ dry-logic (~> 1.0, >= 1.0.2)
51
+ i18n (1.8.9)
52
+ concurrent-ruby (~> 1.0)
53
+ method_source (1.0.0)
54
+ minitest (5.14.4)
55
+ pastel (0.8.0)
56
+ tty-color (~> 0.5)
57
+ pry (0.14.0)
58
+ coderay (~> 1.1)
59
+ method_source (~> 1.0)
60
+ pry-doc (1.1.0)
61
+ pry (~> 0.11)
62
+ yard (~> 0.9.11)
63
+ pry-rails (0.3.9)
64
+ pry (>= 0.10.4)
65
+ rake (13.0.3)
66
+ tty-color (0.6.0)
67
+ tzinfo (2.0.4)
68
+ concurrent-ruby (~> 1.0)
69
+ yard (0.9.26)
70
+ yieldable (0.1.0)
71
+ zeitwerk (2.4.2)
72
+
73
+ PLATFORMS
74
+ ruby
75
+
76
+ DEPENDENCIES
77
+ awesome_print
78
+ benchmark-ips
79
+ console_utils
80
+ namelab!
81
+ pry
82
+ pry-doc
83
+ rake (>= 10.0)
84
+
85
+ BUNDLED WITH
86
+ 2.2.15
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Anton
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Namelab
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/namelab`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'namelab'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install namelab
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/anton.estum@gmail.com/namelab.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "namelab"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
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/exe/namelab ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "namelab"
4
+ require "namelab/cli"
5
+
6
+ Dry::CLI.new(Namelab::CLI::Commands).call
data/lib/namelab.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "namelab/version"
4
+ require "namelab/word_generator"
5
+ require "namelab/registry"
6
+
7
+ module Namelab
8
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/cli"
4
+ require "shellwords"
5
+
6
+ module Namelab
7
+ module CLI
8
+ module InheritanceHook
9
+ def inherited(base)
10
+ super
11
+
12
+ base.class_eval do
13
+ %i(examples arguments options).each do |var|
14
+ instance_variable_get("@#{var}").unshift(*superclass.public_send(var).dup)
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ module Commands
21
+ extend Dry::CLI::Registry
22
+
23
+ class Generate < Dry::CLI::Command
24
+ extend InheritanceHook
25
+
26
+ argument :lengths, :type => :array,
27
+ :default => [6],
28
+ :desc => "Sequence of lengths for each target word separated by space"
29
+
30
+ option :samples, :type => :integer,
31
+ :default => 1,
32
+ :desc => "Amount of generated samples",
33
+ :aliases => %w(-s)
34
+
35
+ option :normalize, :type => :boolean,
36
+ :default => true,
37
+ :desc => "Normalize generated words, i.e. truncate to target length",
38
+ :aliases => %w(-N)
39
+
40
+ def call(samples:, lengths: [], **opts)
41
+ generators = lengths.each_with_object(opts).map(&WordGenerator)
42
+
43
+ samples.to_i.times do
44
+ outputing { generators.map(&:call).join(" ") }
45
+ end
46
+ end
47
+
48
+ # Yields the block and outputs result.
49
+ def outputing # :yields:
50
+ result = yield
51
+ fail "Empty result" unless result && result.size > 0
52
+ output(result)
53
+ end
54
+
55
+ def output(result)
56
+ puts result
57
+ end
58
+ end
59
+
60
+ class Say < Generate
61
+ DEFAULT_ARGS = %w(-i -v Victoria).freeze
62
+ option :sayopt, :type => :string,
63
+ :default => DEFAULT_ARGS.shelljoin,
64
+ :desc => "Arguments for say command"
65
+
66
+ def call(sayopt:, **opts)
67
+ @say_args = sayopt.shellsplit
68
+ @say_args.unshift(*DEFAULT_ARGS) if @say_args != DEFAULT_ARGS
69
+ super
70
+ end
71
+
72
+ def output(result)
73
+ system("/usr/bin/say", *@say_args, result)
74
+ end
75
+ end
76
+
77
+ register "gen", Generate, aliases: %w(generate g -g)
78
+ register "say", Say, aliases: %w(speak s -s)
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "tempfile"
4
+
5
+ module Namelab
6
+ class ListRef
7
+ attr_reader :proc, :lines_count
8
+
9
+ def initialize(io_proc)
10
+ @proc = io_proc
11
+ sync!
12
+ end
13
+
14
+ def lines(enum: true, &block)
15
+ enum ? each_line(&block) : block.(each_line)
16
+ ensure
17
+ rewind
18
+ end
19
+
20
+ def each_line(&block)
21
+ return to_enum(:each_line) unless block_given?
22
+ io.each_line(chomp: true, &block)
23
+ end
24
+
25
+ def io
26
+ @io ||= @proc[]
27
+ end
28
+
29
+ def with_io
30
+ io
31
+ yield(self)
32
+ ensure
33
+ close
34
+ end
35
+
36
+ def close
37
+ @io&.close
38
+ if defined?(@_tmp)
39
+ File.unlink(@_tmp)
40
+ remove_instance_variable(:@_tmp)
41
+ end
42
+ remove_instance_variable(:@io)
43
+ end
44
+
45
+ def rewind
46
+ @io&.rewind
47
+ end
48
+
49
+ def sample_lineno
50
+ rand(lines_count)
51
+ end
52
+
53
+ def filter(&block) # :yields:
54
+ @filter = block.to_proc
55
+ self
56
+ end
57
+
58
+ def sample(filter = true)
59
+ if filter && defined?(@filter)
60
+ proc = @filter
61
+ remove_instance_variable(:@filter)
62
+ filtered_sample(&proc)
63
+ else
64
+ get(sample_lineno).strip
65
+ end
66
+ end
67
+
68
+ def filtered_sample # :yields:
69
+ result = nil
70
+ while result.nil?
71
+ value = sample(false)
72
+ redo unless yield(value)
73
+ result = value
74
+ end
75
+ result
76
+ end
77
+
78
+ def get(lineno)
79
+ offset = lineno * @sample_size
80
+ begin
81
+ @io.pread(@sample_size, offset)
82
+ rescue EOFError
83
+ offset -= @sample_size
84
+ retry
85
+ end
86
+ end
87
+
88
+ NORMALIZED_FMT = "%1$*2$s"
89
+
90
+ def sync!
91
+ remove_instance_variable(:@_tmp) if defined?(@_tmp)
92
+ tempfile = Tempfile.new(File.basename(io.path))
93
+ lines enum: false do |enum|
94
+ @sample_size = enum.max_by(&:length).length
95
+ rewind
96
+ lines_count = 0
97
+ enum.each do |v|
98
+ tempfile.printf(NORMALIZED_FMT, v, -@sample_size)
99
+ lines_count += 1
100
+ end
101
+ @lines_count = lines_count
102
+ end
103
+ tempfile.rewind
104
+ close
105
+ @_tmp = tempfile.path
106
+ @io = tempfile.to_io
107
+ at_exit { close }
108
+ end
109
+ end
110
+ end