eunomia_gen 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: 53072ce6f110e4e2c4750839e4cd632831b102a4b1d22cc407cbb681be6a1c28
4
+ data.tar.gz: b9cf7bcb05f3c5fb4b8db1bebc8f04d6a35b52bfb5520d3ef87b7756812b8675
5
+ SHA512:
6
+ metadata.gz: e3ef8833a30649a1666f2328b131ebbf59e20137e1afed01d24ae2687b040aa26c706e3e97b0b522e77e997bf1a5eef5a56ccb6405fd4961474e644bb309e5dc
7
+ data.tar.gz: 161a8d306af2e98859663126a4e35889c417e80f66377329b2d3d30ef59f17033d9f031c7f5368b574dee7f3d21f0011252964f7b6540424ea434142aee160e6
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,9 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 3.0
4
+
5
+ Style/StringLiterals:
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ EnforcedStyle: double_quotes
@@ -0,0 +1,11 @@
1
+ // Folder-specific settings
2
+ //
3
+ // For a full list of overridable settings, and general information on folder-specific settings,
4
+ // see the documentation: https://zed.dev/docs/configuring-zed#settings-files
5
+ {
6
+ "languages": {
7
+ "Ruby": {
8
+ "language_servers": ["solargraph", "rubocop", "!ruby-lsp"]
9
+ }
10
+ }
11
+ }
data/.zed/tasks.json ADDED
@@ -0,0 +1,43 @@
1
+ // Static tasks configuration.
2
+ //
3
+ // Example:
4
+ [
5
+ {
6
+ "label": "Example task",
7
+ "command": "for i in {1..5}; do echo \"Hello $i/5\"; sleep 1; done",
8
+ //"args": [],
9
+ // Env overrides for the command, will be appended to the terminal's environment from the settings.
10
+ "env": { "foo": "bar" },
11
+ // Current working directory to spawn the command into, defaults to current project root.
12
+ //"cwd": "/path/to/working/directory",
13
+ // Whether to use a new terminal tab or reuse the existing one to spawn the process, defaults to `false`.
14
+ "use_new_terminal": false,
15
+ // Whether to allow multiple instances of the same task to be run, or rather wait for the existing ones to finish, defaults to `false`.
16
+ "allow_concurrent_runs": false,
17
+ // What to do with the terminal pane and tab, after the command was started:
18
+ // * `always` — always show the terminal pane, add and focus the corresponding task's tab in it (default)
19
+ // * `never` — avoid changing current terminal pane focus, but still add/reuse the task's tab there
20
+ "reveal": "always",
21
+ // What to do with the terminal pane and tab, after the command had finished:
22
+ // * `never` — Do nothing when the command finishes (default)
23
+ // * `always` — always hide the terminal tab, hide the pane also if it was the last tab in it
24
+ // * `on_success` — hide the terminal tab on task success only, otherwise behaves similar to `always`
25
+ "hide": "never",
26
+ // Which shell to use when running a task inside the terminal.
27
+ // May take 3 values:
28
+ // 1. (default) Use the system's default terminal configuration in /etc/passwd
29
+ // "shell": "system"
30
+ // 2. A program:
31
+ // "shell": {
32
+ // "program": "sh"
33
+ // }
34
+ // 3. A program with arguments:
35
+ // "shell": {
36
+ // "with_arguments": {
37
+ // "program": "/bin/bash",
38
+ // "arguments": ["--login"]
39
+ // }
40
+ // }
41
+ "shell": "system"
42
+ }
43
+ ]
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-08-25
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 G Palmer
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,157 @@
1
+ # Eunomia
2
+
3
+ This gem is a text generator; a cross between mad-libs and Faker. The focus is on
4
+ generating text that is appropriate for games, stories, and other creative works.
5
+
6
+ Why does this exist? In the age of generatve text using LLMs, there's probably not
7
+ a good reason other than it's more understandable and possibly easier to fine tune.
8
+
9
+ Why Eunomia? Eunomia is a minor Greek goddess of law and order and since the generator
10
+ is set up using a series of rules, it seemed appropriate.
11
+
12
+ ## Installation
13
+
14
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
15
+
16
+ Install the gem and add to the application's Gemfile by executing:
17
+
18
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
19
+
20
+ If bundler is not being used to manage dependencies, install the gem by executing:
21
+
22
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
23
+
24
+ ## Usage
25
+
26
+ The gem is used by loading JSON (or ruby hashes) into a store, building a request context,
27
+ and then selecting a generator to use.
28
+
29
+ There are three main concepts:
30
+ * Segments are elements in a sequence that can be combined to form a string.
31
+ Segments in square brackets have a special meaning. Dice notation (e.g. `[d4]`)
32
+ generates a random number. A segment that is a key (e.g. `[thing]`) will be replaced
33
+ with a value chosen by the generator.
34
+ * Items are selected at random from the generator that they are nested in. Items
35
+ can have metadata about the generated string such as value (an arbitrary integer),
36
+ ruby hash, or a weight (the likihood of being selected)
37
+ * Generators are the top level of the JSON. They contain a list of items that are
38
+ selected at random.
39
+
40
+ ```ruby
41
+ require 'eunomia_gen'
42
+
43
+ data = [
44
+ {
45
+ key: "random-fruit",
46
+ items: [
47
+ functions: %w[pluralize capitalize],
48
+ segments: "[d4] [fruit]"
49
+ ]
50
+ },
51
+ {
52
+ key: "fruit",
53
+ items: %w[apple banana orange pear kiwi]
54
+ }
55
+ ]
56
+
57
+ Eunomia.add(data)
58
+ p Eunomia.generate("random-fruit").to_s # => "3 Bananas"
59
+ ```
60
+
61
+ ### Translations
62
+
63
+ A request may have a key to translate a generated text into a different language.
64
+ A translation hash may also be added to the request itself. If a translation is set
65
+ on the item, then it needs a key to look up the translation. On the request any matching
66
+ string is replaced with the value.
67
+
68
+ ```ruby
69
+ require 'eunomia_gen'
70
+
71
+ data = [
72
+ { key: "fruit", items: %w[apple banana orange pear kiwi], alts: { "apple" => { "es" => "manzana" } } }
73
+ ]
74
+
75
+ Eunomia.add(data)
76
+ request = Eunomia::Request.new("fruit", alt_key: "es", unique: true, alts: { "kiwi" => "a small flightless bird" })
77
+ arr = []
78
+ 5.times { arr << request.generate.to_s }
79
+ p arr.join(", ") # => "manzana, banana, orange, pear, a small flightless bird"
80
+ ```
81
+
82
+ ### Functions
83
+
84
+ A function is a method that can be called on the generated segments in a sequence. There are few
85
+ built-in functions, but it is possible to add custom functions by passing in a proc that
86
+ takes an array of strings and returns an array of strings.
87
+
88
+ Functions are called in the order that they are listed in the request. Functions defined on the
89
+ request are applied to the final generated string. Functions defined on a generator or item
90
+ are applied to the segments generated by that entity.
91
+
92
+ ```ruby
93
+ require 'eunomia_gen'
94
+
95
+ data = [
96
+ { key: "fruit", items: %w[apple banana orange pear kiwi] },
97
+ { key: "random-fruit", items: [{ segments: "[d4] [fruit]" }] }
98
+ ]
99
+
100
+ Eunomia.add(data)
101
+ Eunomia.add_function(:reverse, proc { |arr| arr.map(&:reverse) })
102
+ request = Eunomia::Request.new("random-fruit", functions: %w[pluralize reverse])
103
+ p request.generate.to_s # => "3 sananab"
104
+
105
+ request = Eunomia::Request.new("random-fruit", functions: %w[reverse pluralize])
106
+ p request.generate.to_s # => "3 ikiks"
107
+ ```
108
+
109
+ ### Weights
110
+
111
+ Items can have a weight that determines the likelihood of being selected. The default weight is 1.
112
+
113
+ ### Unique
114
+
115
+ A request has a unique flag, if set to true the request will add all generated strings to an internal
116
+ set and will discard a string if it is already in the set. It will make up to 100 attempts to
117
+ generate a unique string before raising an error.
118
+
119
+ ### Values
120
+
121
+ Items can have a value that is an arbitrary integer. This is multiplied by the first segment in the
122
+ generated string if it a number (e.g. a constant or randomly generated from a dice segment).
123
+ If the first segment is not a number the multiplier defaults to 1.
124
+
125
+ ### Constants
126
+
127
+ A constant hash can be added to the request. A string that matches the hash key will be replaced by the
128
+ constant value.
129
+
130
+ ## Development
131
+
132
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec`
133
+ to run the tests. You can also run `bin/console` for an interactive prompt that will allow
134
+ you to experiment.
135
+
136
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a
137
+ new version, update the version number in `version.rb`, and then run `bundle exec rake release`,
138
+ which will create a git tag for the version, push git commits and the created tag, and push
139
+ the `.gem` file to [rubygems.org](https://rubygems.org).
140
+
141
+ ## Contributing
142
+
143
+ Bug reports and pull requests are welcome on GitHub at https://github.com/palmergs/eunomia_gen.
144
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are
145
+ expected to adhere to the
146
+ [code of conduct](https://github.com/[USERNAME]/eunomia_gen/blob/master/CODE_OF_CONDUCT.md).
147
+
148
+ ## License
149
+
150
+ The gem is available as open source under the terms of the
151
+ [MIT License](https://opensource.org/licenses/MIT).
152
+
153
+ ## Code of Conduct
154
+
155
+ Everyone interacting in the EunomiaGen project's codebases, issue trackers, chat rooms and
156
+ mailing lists is expected to follow the
157
+ [code of conduct](https://github.com/[USERNAME]/eunomia_gen/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ require "rb_sys/extensiontask"
13
+
14
+ task build: :compile
15
+
16
+ GEMSPEC = Gem::Specification.load("eunomia_gen.gemspec")
17
+
18
+ RbSys::ExtensionTask.new("eunomia_gen", GEMSPEC) do |ext|
19
+ ext.lib_dir = "lib/eunomia_gen"
20
+ end
21
+
22
+ task default: %i[compile spec rubocop]
data/exe/eunomia_gen ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "eunomia_gen"
@@ -0,0 +1,30 @@
1
+ module Eunomia
2
+ class Element
3
+ attr_reader :orig, :str, :value, :multiplier, :meta, :children
4
+
5
+ def initialize(str, value: 0, multiplier: 1, children: nil)
6
+ @orig = str
7
+ @str = str
8
+ @value = value
9
+ @multiplier = multiplier
10
+ @meta = {}
11
+ @children = children
12
+ end
13
+
14
+ def children?
15
+ !children.nil? && !children.empty?
16
+ end
17
+
18
+ def to_s
19
+ str
20
+ end
21
+
22
+ def to_h
23
+ if children?
24
+ { orig:, str:, value:, multiplier:, children: children.map(&:to_h) }
25
+ else
26
+ { orig:, str:, value:, multiplier: }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Eunomia
4
+ module Function
5
+ # Capitalize each string in the array
6
+ class Capitalize
7
+ def apply(arr)
8
+ to_proc.call(arr)
9
+ end
10
+
11
+ def to_proc
12
+ proc do |arr|
13
+ arr.map(&:capitalize)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Eunomia
4
+ module Function
5
+ # Downcase each string in the array
6
+ class Downcase
7
+ def apply(arr)
8
+ to_proc.call(arr)
9
+ end
10
+
11
+ def to_proc
12
+ proc do |arr|
13
+ arr.map(&:downcase)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Eunomia
4
+ module Function
5
+ # Pluralize attempts to pluralize the last word in the array
6
+ # if the first word is a number and not equal to 1. This is
7
+ # mostly useful for phrases in the form "number adjective? noun"
8
+ class Pluralize
9
+ EXCEPTIONS = {
10
+ "foot" => "feet",
11
+ "axis" => "axes",
12
+ "child" => "children",
13
+ "codex" => "codices",
14
+ "die" => "dice",
15
+ "dwarf" => "dwarves",
16
+ "goose" => "geese",
17
+ "elf" => "elves",
18
+ "man" => "men",
19
+ "ox" => "oxen",
20
+ "thief" => "thieves",
21
+ "tooth" => "teeth",
22
+ "wolf" => "wolves",
23
+ "woman" => "women"
24
+ }.freeze
25
+
26
+ SINGLE = Set.new(%w[a an the this that my your his her its our their])
27
+
28
+ def apply(arr)
29
+ to_proc.call(arr)
30
+ end
31
+
32
+ def to_proc
33
+ proc do |arr|
34
+ num = to_num(arr[0])
35
+ if num && num > 1
36
+ dc = arr[-1].downcase
37
+ arr[-1] = exceptions(dc) || others(dc) || ends_with_y(dc) || simple(dc)
38
+ end
39
+ arr
40
+ end
41
+ end
42
+
43
+ def to_num(str)
44
+ return 1 if SINGLE.include?(str.downcase)
45
+
46
+ str =~ /\d+/ ? str.to_i : nil
47
+ end
48
+
49
+ def exceptions(str)
50
+ EXCEPTIONS[str]
51
+ end
52
+
53
+ def others(str)
54
+ if str.end_with?("s") || str.end_with?("x") || str.end_with?("z") || str.end_with?("ch") || str.end_with?("sh")
55
+ "#{str}es"
56
+ end
57
+ end
58
+
59
+ def ends_with_y(str)
60
+ "#{str[0..-2]}ies" if str.end_with?("y")
61
+ end
62
+
63
+ def simple(str)
64
+ "#{str}s"
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Eunomia
4
+ module Function
5
+ # Quote add a quote to the beginning of the first string and the end of the last string
6
+ class Quote
7
+ def apply(arr)
8
+ to_proc.call(arr)
9
+ end
10
+
11
+ def to_proc
12
+ proc do |arr|
13
+ arr[0] = "\"#{arr[0]}"
14
+ arr[-1] = "#{arr[-1]}\""
15
+ arr
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Eunomia
4
+ module Function
5
+ # Titleize capitalizes each string in the array unless it's a word that
6
+ # is not normally capitalized in a title.
7
+ class Titleize
8
+ SKIP_WORDS = Set.new(%w[a an and as at but by for if in of on or the to v via vs])
9
+
10
+ def apply(arr)
11
+ to_proc.call(arr)
12
+ end
13
+
14
+ def to_proc
15
+ proc do |arr|
16
+ idx = 0
17
+ len = arr.length
18
+ while idx < len
19
+ arr[idx] = arr[idx].capitalize if capitalize?(arr[idx], idx, len)
20
+ idx += 1
21
+ end
22
+ arr
23
+ end
24
+ end
25
+
26
+ def capitalize?(str, idx, len)
27
+ idx.zero? || idx == len - 1 || !SKIP_WORDS.include?(str.downcase)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Eunomia
4
+ module Function
5
+ # Upcase each string in the array
6
+ class Upcase
7
+ def apply(arr)
8
+ to_proc.call(arr)
9
+ end
10
+
11
+ def to_proc
12
+ proc do |arr|
13
+ arr.map(&:upcase)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "function/capitalize"
4
+ require_relative "function/downcase"
5
+ require_relative "function/pluralize"
6
+ require_relative "function/quote"
7
+ require_relative "function/titleize"
8
+ require_relative "function/upcase"
9
+
10
+ module Eunomia
11
+ # Function module contains functions that can be applied to an array of strings.
12
+ class Functions
13
+ def initialize
14
+ @functions = {
15
+ "capitalize" => Eunomia::Function::Capitalize.new.to_proc,
16
+ "downcase" => Eunomia::Function::Downcase.new.to_proc,
17
+ "pluralize" => Eunomia::Function::Pluralize.new.to_proc,
18
+ "quote" => Eunomia::Function::Quote.new.to_proc,
19
+ "titleize" => Eunomia::Function::Titleize.new.to_proc,
20
+ "upcase" => Eunomia::Function::Upcase.new.to_proc
21
+ }
22
+ end
23
+
24
+ def apply(arr, funcs)
25
+ funcs.each do |function|
26
+ arr = @functions[function].call(arr)
27
+ end
28
+ arr
29
+ end
30
+
31
+ def add(name, proc)
32
+ @functions[name.to_s] = proc
33
+ end
34
+ end
35
+ end