makes_sense 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: a2206b5da4722518a9ef5028e9e8fd3acdd6fdd98406927d76b776d5ad48cf05
4
+ data.tar.gz: b25d323b3d861b321598eba5bb6a43ccd7bed1fedb07282a9df1525f772f9763
5
+ SHA512:
6
+ metadata.gz: f76f1bdbb6a42428e71c41e0e2413ac4b6d7d6cd5c61d6cbc9fd46291ef259ae46c0a2c7518994533265b2f7b687fff4eeaba92ea3b22e35a197bbe9e2ef4d31
7
+ data.tar.gz: 41bc9a922d55cbd1b5b01eabeea1313da3ab9aaef50a42bf40271f1c8ccd7dcc6379fecffe7f03234657c4192f1b0735e4f1bb3bc9e2430a472b602b1cd546c1
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at smizell@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in makes_sense.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,40 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ makes_sense (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.3)
10
+ diff-lcs (1.5.0)
11
+ method_source (1.0.0)
12
+ pry (0.14.2)
13
+ coderay (~> 1.1)
14
+ method_source (~> 1.0)
15
+ rake (13.0.6)
16
+ rspec (3.12.0)
17
+ rspec-core (~> 3.12.0)
18
+ rspec-expectations (~> 3.12.0)
19
+ rspec-mocks (~> 3.12.0)
20
+ rspec-core (3.12.2)
21
+ rspec-support (~> 3.12.0)
22
+ rspec-expectations (3.12.3)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.12.0)
25
+ rspec-mocks (3.12.5)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.12.0)
28
+ rspec-support (3.12.0)
29
+
30
+ PLATFORMS
31
+ arm64-darwin-20
32
+
33
+ DEPENDENCIES
34
+ makes_sense!
35
+ pry
36
+ rake (~> 13.0)
37
+ rspec (~> 3.0)
38
+
39
+ BUNDLED WITH
40
+ 2.2.26
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # MakesSense
2
+
3
+ Define a Decision Table in Ruby code, validate it, and execute a ruleset against it.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'makes_sense'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install makes_sense
20
+
21
+ ## Usage
22
+
23
+ This is a work in progress. Please see the [specs](./spec/) directory for now to see how to use this library
24
+
25
+ ## Development
26
+
27
+ 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.
28
+
29
+ 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).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/makes_sense. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/makes_sense/blob/master/CODE_OF_CONDUCT.md).
34
+
35
+ ## Code of Conduct
36
+
37
+ Everyone interacting in the MakesSense project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/makes_sense/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
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
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "makes_sense"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ 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
@@ -0,0 +1,190 @@
1
+ require_relative "result"
2
+ require_relative "values"
3
+
4
+ module MakesSense
5
+ Condition = Struct.new(:name, :values)
6
+ ResultTable = Struct.new(:rows)
7
+ Row = Struct.new(:conditions, :results, :row_index)
8
+
9
+ class DecisionTable
10
+ def initialize(name, conditions, result_table, args)
11
+ @name = name
12
+ @conditions = conditions
13
+ @result_table = result_table
14
+ @args = args
15
+ end
16
+
17
+ def self.define(name, &block)
18
+ dsl = DecisionTableDsl.new
19
+ dsl.instance_eval(&block)
20
+ new(name, dsl.conditions, dsl.result_table, dsl.args)
21
+ end
22
+
23
+ def validate
24
+ condition_values = @conditions.map(&:values)
25
+ possible_conditions = condition_values[0].product(*condition_values[1..])
26
+ expanded_rows = expand_rows
27
+ expanded_row_conditions = expanded_rows.map(&:conditions)
28
+
29
+ errors = []
30
+
31
+ possible_conditions.each do |possible_condition|
32
+ unless expanded_row_conditions.include?(possible_condition)
33
+ errors << {
34
+ type: :missing,
35
+ message: "Missing result condition: #{possible_condition}",
36
+ expected_conditions: possible_condition,
37
+ expected: {}.tap do |condition_map|
38
+ @conditions.each_with_index do |condition, index|
39
+ condition_map[condition.name] = possible_condition[index]
40
+ end
41
+ end
42
+ }
43
+ end
44
+ end
45
+
46
+ visited = []
47
+
48
+ expanded_rows.each do |row|
49
+ found_index = visited.find_index(row.conditions)
50
+ if found_index
51
+ errors << {
52
+ type: :duplicate,
53
+ message: "Duplicate result #{row.conditions} for rows #{found_index} and #{row.row_index}",
54
+ row: row,
55
+ duplicate_index: found_index
56
+ }
57
+ end
58
+
59
+ visited << row.conditions
60
+ end
61
+
62
+ if !errors.empty?
63
+ Failure.new(errors)
64
+ else
65
+ Success.new(self)
66
+ end
67
+ end
68
+
69
+ def with_ruleset(ruleset)
70
+ ->(**kwargs) do
71
+ expanded_rows = expand_rows
72
+
73
+ ruleset_results = @conditions.map do |condition|
74
+ method_kwarg_names = ruleset.method(condition.name).parameters.map { |parameter| parameter[1] }
75
+ args = method_kwarg_names.map { |kwarg_name| kwargs[kwarg_name] }
76
+ ruleset.public_send(condition.name, *args)
77
+ end
78
+
79
+ result_row = @result_table.rows.find do |row|
80
+ row.conditions == ruleset_results
81
+ end
82
+
83
+ if result_row.results.respond_to?(:call)
84
+ method_kwarg_names = result_row.results.parameters.map { |parameter| parameter[1] }
85
+ args = method_kwarg_names.map { |kwarg_name| kwargs[kwarg_name] }
86
+ result_row.results.call(*args)
87
+ else
88
+ result_row.results
89
+ end
90
+ end
91
+ end
92
+
93
+ private
94
+
95
+ # Expands rows where `any` is used
96
+ # It will replace an `any` with an array of the respective values.
97
+ # It will then take the product of those to create the correct number of new rows.
98
+ def expand_rows
99
+ new_rows = []
100
+
101
+ @result_table.rows.each_with_index do |row, row_index|
102
+ needs_expansion = row.conditions.any? { |condition| condition.is_a?(Values::Any) }
103
+
104
+ unless needs_expansion
105
+ new_rows << row
106
+ next
107
+ end
108
+
109
+ collected = []
110
+ current = []
111
+
112
+ row.conditions.each_with_index do |condition, index|
113
+ case condition
114
+ when Values::Any
115
+ unless current.empty?
116
+ collected << current
117
+ current = []
118
+ end
119
+
120
+ collected << @conditions[index].values
121
+ else
122
+ current << condition
123
+ end
124
+ end
125
+
126
+ collected << current unless current.empty?
127
+
128
+ # When the `collected` length is `1`, it results in unexpected values with `#reduce`
129
+ # Checking for the length and only reducing when it's greater than one causes the
130
+ # resulting value to be as expected.
131
+ new_conditions =
132
+ if collected.length > 1
133
+ collected.reduce { |a, b| a.product(b) }
134
+ else
135
+ collected[0].map { |v| [v] }
136
+ end
137
+
138
+ new_conditions.each do |conditions|
139
+ new_rows << Row.new(conditions, row.results, row_index)
140
+ end
141
+ end
142
+
143
+ new_rows
144
+ end
145
+ end
146
+
147
+ class DecisionTableDsl
148
+ include Values
149
+
150
+ attr_reader :args, :conditions, :result_table
151
+
152
+ def initialize
153
+ @args = []
154
+ @conditions = []
155
+ @result_table = nil
156
+ end
157
+
158
+ def arg(name)
159
+ @args << name
160
+ end
161
+
162
+ def condition(name, type)
163
+ values =
164
+ case type
165
+ when :bool then [t, f]
166
+ end
167
+
168
+ @conditions << Condition.new(name, values)
169
+ end
170
+
171
+ def table(&block)
172
+ result_table = ResultTable.new([])
173
+ dsl = ResultTableDsl.new(result_table)
174
+ dsl.instance_eval(&block)
175
+ @result_table = result_table
176
+ end
177
+ end
178
+
179
+ class ResultTableDsl
180
+ include Values
181
+
182
+ def initialize(result_table)
183
+ @result_table = result_table
184
+ end
185
+
186
+ def row(conditions, results)
187
+ @result_table.rows << Row.new(conditions, results, @result_table.rows.length)
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,47 @@
1
+ module MakesSense
2
+ class Success
3
+ attr_reader :value
4
+
5
+ def initialize(value)
6
+ @value = value
7
+ end
8
+
9
+ def success?
10
+ true
11
+ end
12
+
13
+ def failure?
14
+ false
15
+ end
16
+
17
+ def value!
18
+ @value
19
+ end
20
+
21
+ def failure
22
+ raise "Not a failure"
23
+ end
24
+ end
25
+
26
+ class Failure
27
+ def initialize(value)
28
+ @value = value
29
+ end
30
+
31
+ def success?
32
+ false
33
+ end
34
+
35
+ def failure?
36
+ true
37
+ end
38
+
39
+ def value!
40
+ raise "Not successful"
41
+ end
42
+
43
+ def failure
44
+ @value
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+ module MakesSense
2
+ module Values
3
+ def t
4
+ true
5
+ end
6
+
7
+ def f
8
+ false
9
+ end
10
+
11
+ class Any; end
12
+
13
+ def any
14
+ Any.new
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MakesSense
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "makes_sense/version"
4
+ require_relative "makes_sense/decision_table"
5
+
6
+ module MakesSense
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/makes_sense/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "makes_sense"
7
+ spec.version = MakesSense::VERSION
8
+ spec.authors = ["Stephen Mizell"]
9
+
10
+ spec.summary = "Write, validate, and use decision tables for logic"
11
+ spec.description = "Write, validate, and use decision tables for logic"
12
+ spec.homepage = "https://github.com/smizell/makes_sense"
13
+ spec.required_ruby_version = ">= 2.4.0"
14
+
15
+ # spec.metadata["allowed_push_host"] = "https://mygemserver.com"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/smizell/makes_sense"
19
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ spec.add_development_dependency "pry"
34
+
35
+ # For more information and examples about making a new gem, checkout our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: makes_sense
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Stephen Mizell
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-05-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Write, validate, and use decision tables for logic
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - ".rspec"
34
+ - CODE_OF_CONDUCT.md
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - README.md
38
+ - Rakefile
39
+ - bin/console
40
+ - bin/setup
41
+ - lib/makes_sense.rb
42
+ - lib/makes_sense/decision_table.rb
43
+ - lib/makes_sense/result.rb
44
+ - lib/makes_sense/values.rb
45
+ - lib/makes_sense/version.rb
46
+ - makes_sense.gemspec
47
+ homepage: https://github.com/smizell/makes_sense
48
+ licenses: []
49
+ metadata:
50
+ homepage_uri: https://github.com/smizell/makes_sense
51
+ source_code_uri: https://github.com/smizell/makes_sense
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.4.0
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.2.15
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Write, validate, and use decision tables for logic
71
+ test_files: []