klee 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 508af2506e1d6db7fbadde901c29284c219ee4cf7471dac68a21d34110b82a2d
4
+ data.tar.gz: c9dcc725e9a626c9f1ff45c68e46868eaa25166ccf4e88d93bcd0009a3a7211e
5
+ SHA512:
6
+ metadata.gz: e411a8b0c08b4304327663139192ccaed4a05d62048b94b151564065d71b5c3f574c780b3e278c127c54368744a2cd99ad5fd8ba514fb1f1545b65a1977fb17d
7
+ data.tar.gz: dcc64449cbfc1d0f60243f4a2eeee661d5075a271b79c3906b476af37bcc9624b6d740424df96596103f13af663932003aa76b002bb453a82b8da413c68f8d3f
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-05-01
4
+
5
+ - Initial release
@@ -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 jim@saturnflyer.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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Jim Gay
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,71 @@
1
+ # Klee (kleː)
2
+
3
+ Find similarities and differences in object APIs by tracking patterns.
4
+
5
+ See where your objects' methods are following common patterns and where they are not.
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add klee
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install klee
16
+
17
+ ## Usage
18
+
19
+ ### Sort methods based upon patterns
20
+ ```ruby
21
+ class Something
22
+ # define methods that might match some of the patterns
23
+ end
24
+
25
+ patterns = Klee.patterns do
26
+ prefix("has_")
27
+ prefix("verify_")
28
+ infix("_in_")
29
+ suffix("_value?")
30
+ end
31
+
32
+ gestalt = Klee[Something.new, patterns: patterns]
33
+ gestalt.trace(threshold: 6) # threshold for levenshtein distance between unusual method names
34
+ puts gestalt.plot
35
+ gestalt.trace(threshold: 9) # clear the plot and trace again
36
+ puts gestalt.plot
37
+ puts gestalt["unusual"] # unusual method names
38
+ ```
39
+
40
+ ### Find hidden concepts based upon word repetition
41
+ ```ruby
42
+ class Something
43
+ # define methods that have common word parts
44
+ end
45
+
46
+ concept = Klee.object_concepts(Something)
47
+
48
+ concept[4] #=> Set of words that appear at least 4 times
49
+ concept[2] #=> larger Set of words that appear at least 2 times
50
+
51
+ filtered = Klee.object_concepts(Something, modifiers: %i[fill_in_ hover_over_ _message])
52
+ filtered[4] #=> Set of concepts excluding any common modifiers
53
+ ```
54
+
55
+ ## Development
56
+
57
+ 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.
58
+
59
+ 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).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/saturnflyer/klee. 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/saturnflyer/klee/blob/main/CODE_OF_CONDUCT.md).
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
68
+
69
+ ## Code of Conduct
70
+
71
+ Everyone interacting in the Klee project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/saturnflyer/klee/blob/main/CODE_OF_CONDUCT.md).
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 "klee"
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
data/exe/klee ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "klee"
@@ -0,0 +1,72 @@
1
+ module Klee
2
+ class Concepts
3
+ include Enumerable
4
+
5
+ def initialize(*method_names, modifiers: [])
6
+ @method_names = method_names
7
+ @modifiers = modifiers
8
+ end
9
+ attr_reader :method_names, :modifiers
10
+
11
+ def call(threshold)
12
+ warn "threshold is beyond the max count of #{max}" if threshold > max
13
+ warn "threshold is below the min count of #{min}" if threshold < min
14
+ clear
15
+ @ideas = Set.new samples
16
+ .select { |key, value|
17
+ if value.to_i >= threshold
18
+ key
19
+ end
20
+ }
21
+ .compact
22
+ .transform_keys(&:to_sym)
23
+ .keys
24
+ end
25
+ alias_method :[], :call
26
+
27
+ def clear
28
+ clearable = instance_variables - %i[@method_names @modifiers]
29
+ clearable.each { send(:remove_instance_variable, _1) }
30
+ end
31
+
32
+ def each(&block)
33
+ samples.each(&block)
34
+ end
35
+
36
+ def samples
37
+ @samples ||= method_names.flat_map{ words(_1) }.tally
38
+ end
39
+
40
+ def max
41
+ max_by { |_, v| v }.last
42
+ end
43
+
44
+ def min
45
+ min_by { |_, v| v }.last
46
+ end
47
+
48
+ def minmax
49
+ [min, max]
50
+ end
51
+
52
+ private
53
+
54
+ def modifier_matcher
55
+ @modifier_matcher ||= Regexp.new modifiers.map{ Regexp.quote(_1) }.join("|")
56
+ end
57
+
58
+ def words(method_name)
59
+ method_name.to_s
60
+ .then do |string|
61
+ if modifiers.empty?
62
+ string
63
+ else
64
+ string.gsub(modifier_matcher, "")
65
+ end
66
+ end
67
+ .gsub(/\s/, "")
68
+ .split("_")
69
+ .delete_if { _1.empty? }
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,75 @@
1
+ require "set"
2
+ module Klee
3
+ class Gestalt
4
+ def initialize(object, patterns:, ignored:)
5
+ @object = object
6
+ @patterns = patterns
7
+ @init_ignored = ignored
8
+ end
9
+ attr_reader :patterns
10
+
11
+ def unusual
12
+ @unusual ||= comparable.dup
13
+ end
14
+
15
+ def plot
16
+ @plot ||= Hash.new { |h, k| h[k] = Set.new }
17
+ end
18
+
19
+ def ignored
20
+ @ignored ||= Array(@init_ignored) + Klee.public_instance_methods
21
+ end
22
+
23
+ def comparable
24
+ @comparable ||= Set.new(@object.public_methods - ignored).map(&:to_s).freeze
25
+ end
26
+
27
+ def trace(threshold: 6, modifiers: [], concept_threshold: 3)
28
+ plot.clear
29
+ patterns.each do |pattern|
30
+ matched = comparable.select { |method_name| pattern.match?(method_name.to_s) }
31
+ unusual.delete_if { |strange| matched.include?(strange) }
32
+
33
+ plot[patterns.key_for(pattern)].merge(matched)
34
+ end
35
+
36
+ plot["unusual"].merge(unusual_set(threshold))
37
+ plot["concepts"].merge(concepts(modifiers: modifiers, threshold: concept_threshold))
38
+
39
+ self
40
+ end
41
+
42
+ def concepts(modifiers: [], threshold: 3)
43
+ @concepts ||= Concepts.new(*unusual, modifiers: modifiers).call(threshold)
44
+ end
45
+
46
+ def [](key)
47
+ trace if plot.empty?
48
+ plot.fetch(key)
49
+ end
50
+
51
+ def prefixes
52
+ plot.select { |key, _| patterns.prefixes.include?(key) }
53
+ end
54
+
55
+ def infixes
56
+ plot.select { |key, _| patterns.infixes.include?(key) }
57
+ end
58
+
59
+ def suffixes
60
+ plot.select { |key, _| patterns.suffixes.include?(key) }
61
+ end
62
+
63
+ def unusual?(*items)
64
+ items.to_set <= plot["unusual"].flatten
65
+ end
66
+
67
+ private
68
+
69
+ def unusual_set(threshold)
70
+ Set.new(unusual).divide { |a, b|
71
+ DidYouMean::Levenshtein.distance(a, b) < threshold
72
+ }
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,68 @@
1
+ module Klee
2
+ class Patterns
3
+ include Enumerable
4
+
5
+ def initialize(&block)
6
+ @patterns = Hash.new { |h, k| h[k] = Set.new }
7
+ instance_eval(&block) if block
8
+ end
9
+
10
+ def prefix(which)
11
+ @patterns[__callee__].add which
12
+ send("to_#{__callee__}", which).tap { match(_1) }
13
+ end
14
+ alias_method :suffix, :prefix
15
+ alias_method :infix, :prefix
16
+
17
+ def match(which)
18
+ @patterns[:match].add which
19
+ which
20
+ end
21
+
22
+ def prefixes
23
+ @patterns[:prefix]
24
+ end
25
+
26
+ def infixes
27
+ @patterns[:infix]
28
+ end
29
+
30
+ def suffixes
31
+ @patterns[:suffix]
32
+ end
33
+
34
+ def each(&block)
35
+ @patterns[:match].each(&block)
36
+ end
37
+
38
+ def keys
39
+ @patterns.except(:match).values.inject(Set.new, :+)
40
+ end
41
+
42
+ def key_for(pattern)
43
+ keys.find { pattern =~ _1 }
44
+ end
45
+
46
+ private
47
+
48
+ def quote(which)
49
+ Regexp.quote(which)
50
+ end
51
+
52
+ def to_match(which)
53
+ which
54
+ end
55
+
56
+ def to_prefix(which)
57
+ /\A#{quote(which)}/
58
+ end
59
+
60
+ def to_infix(which)
61
+ /.*#{quote(which)}.*/
62
+ end
63
+
64
+ def to_suffix(which)
65
+ /#{quote(which)}\z/
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klee
4
+ VERSION = "0.1.0"
5
+ end
data/lib/klee.rb ADDED
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "klee/version"
4
+ require_relative "klee/patterns"
5
+ require_relative "klee/gestalt"
6
+ require_relative "klee/concepts"
7
+
8
+ module Klee
9
+ class Error < StandardError; end
10
+
11
+ def self.extended(base)
12
+ base.define_method(:klee_patterns) { self.class.klee_patterns }
13
+ end
14
+
15
+ def klee_patterns(&block)
16
+ @klee_patterns ||= Klee.patterns(&block)
17
+ end
18
+
19
+ def self.patterns(&block)
20
+ Patterns.new(&block)
21
+ end
22
+
23
+ def self.concepts(*methond_names, modifiers: [])
24
+ Concepts.new(*methond_names, modifiers: [])
25
+ end
26
+
27
+ def self.object_concepts(object, modifiers: [])
28
+ names = if object.respond_to?(:public_instance_methods)
29
+ object.public_instance_methods(false)
30
+ else
31
+ object.public_methods(false)
32
+ end
33
+ concepts(*names, modifiers: modifiers)
34
+ end
35
+
36
+ def self.[](object, threshold = 6,
37
+ patterns: object.respond_to?(:klee_patterns) ? object.klee_patterns : raise(ArgumentError, "You must include patterns to match for #{object.inspect}"),
38
+ ignored: Class.instance_methods)
39
+ Gestalt.new(object, patterns: patterns, ignored: ignored).trace(threshold: threshold)
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: klee
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jim Gay
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-05-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Evaluate the similarities and differences in your objects. Art does not
14
+ reflect what is seen, rather it makes the hidden visible.
15
+ email:
16
+ - jim@saturnflyer.com
17
+ executables:
18
+ - klee
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - LICENSE.txt
25
+ - README.md
26
+ - bin/console
27
+ - bin/setup
28
+ - exe/klee
29
+ - lib/klee.rb
30
+ - lib/klee/concepts.rb
31
+ - lib/klee/gestalt.rb
32
+ - lib/klee/patterns.rb
33
+ - lib/klee/version.rb
34
+ homepage: https://github.com/saturnflyer/klee
35
+ licenses:
36
+ - MIT
37
+ metadata:
38
+ homepage_uri: https://github.com/saturnflyer/klee
39
+ source_code_uri: https://github.com/saturnflyer/klee
40
+ changelog_uri: https://github.com/saturnflyer/klee/blob/main/CHANGELOG.md
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.7.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.2.27
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Evaluate the similarities and differences in your objects
60
+ test_files: []