logica 0.0.1

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
+ SHA1:
3
+ metadata.gz: 6f7c39b53470ab8de4b254289de127f5381dd1e5
4
+ data.tar.gz: 5b6c07bb425e77e05aac88438db2b433dec846fc
5
+ SHA512:
6
+ metadata.gz: e1c8605b2591fdd244bdb060655a4599a6d46b9f630b737c53b70733a335e43c93ad90491e31800f4027cf00a2d00b6b133a2ef41ab45a9909050ffca54d0fb1
7
+ data.tar.gz: 7c9eccba730c03480e363eed52bcd3fb61521c94ab93bb215a2b856591508bab57fffd2f15221563f1321844ac6168e3676fbbeae78dea522c38c38f9c37e08c
data/.gitignore ADDED
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ Gemfile.lock
46
+ .ruby-version
47
+ .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - 2.2.0
7
+ - 2.3.0
8
+ - 2.4.0
9
+ before_install: gem install bundler -v 1.15.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,14 @@
1
+ # Logica Changelog
2
+
3
+ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning][Semver].
4
+
5
+ ## [Unreleased]
6
+
7
+ * Your contribution here!
8
+
9
+ ## 0.0.1 (2017-06-20)
10
+
11
+ * Initial release
12
+
13
+ [Semver]: http://semver.org
14
+ [Unreleased]: https://github.com/eugeniobruno/logica/compare/v0.0.1...HEAD
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at eugeniobruno@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in logica.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Eugenio Bruno
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,91 @@
1
+ # Logica
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/logica.svg)](https://badge.fury.io/rb/logica)
4
+ [![Build Status](https://travis-ci.org/eugeniobruno/logica.svg?branch=master)](https://travis-ci.org/eugeniobruno/logica)
5
+ [![Coverage Status](https://coveralls.io/repos/github/eugeniobruno/logica/badge.svg?branch=master)](https://coveralls.io/github/eugeniobruno/logica?branch=master)
6
+ [![Code Climate](https://codeclimate.com/github/eugeniobruno/logica.svg)](https://codeclimate.com/github/eugeniobruno/logica)
7
+ [![Dependency Status](https://gemnasium.com/eugeniobruno/logica.svg)](https://gemnasium.com/eugeniobruno/logica)
8
+
9
+ Logica is a reusable implementation of the Specification design pattern, or a framework to manage predicates of arbitrary complexity.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'logica'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install logica
26
+
27
+ ## Usage
28
+
29
+ Here is a simple example:
30
+
31
+ ```ruby
32
+ class IsEven < Logica::Predicates::Base
33
+
34
+ def satisfied_by?(number)
35
+ number.even?
36
+ end
37
+
38
+ end
39
+
40
+ class IsGreaterThan < Logica::Predicates::Base
41
+
42
+ attr_reader :threshold
43
+
44
+ def initialize(threshold)
45
+ @threshold = threshold
46
+ end
47
+
48
+ def satisfied_by?(number)
49
+ number > threshold
50
+ end
51
+
52
+ end
53
+
54
+ is_even = IsEven.new
55
+
56
+ is_even.satisfied_by?(1) # false
57
+ is_even.satisfied_by?(2) # true
58
+
59
+ is_even.negated.satisfied_by?(3) # true
60
+ is_even.negated.satisfied_by?(4) # false
61
+
62
+ is_greater_than_five = IsGreaterThan.new(5)
63
+
64
+ is_greater_than_five.satisfied_by?(5) # false
65
+ is_greater_than_five.satisfied_by?(6) # true
66
+
67
+ is_even.and(is_greater_than_five).satisfied_by?(6) # true
68
+ is_even.and(is_greater_than_five).satisfied_by?(7) # false
69
+
70
+ is_even.or(is_greater_than_five).satisfied_by?(8) # true
71
+ is_even.or(is_greater_than_five).satisfied_by?(9) # true
72
+ ```
73
+
74
+ More examples that show many more features can be found in the test files.
75
+
76
+
77
+ ## Development
78
+
79
+ 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.
80
+
81
+ To install this gem onto your local machine, run `bundle exec rake install`.
82
+
83
+ ## Contributing
84
+
85
+ Bug reports and pull requests are welcome on GitHub at https://github.com/eugeniobruno/logica. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
86
+
87
+
88
+ ## License
89
+
90
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
91
+
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['test/**/test_*.rb']
7
+ end
8
+
9
+ task default: :test
10
+
11
+ desc 'Start a REPL session'
12
+ task :console do
13
+ require 'logica'
14
+ require 'pry'
15
+ ARGV.clear
16
+ Pry.start
17
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'logica'
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,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,24 @@
1
+ module Logica
2
+ module ComparableByState
3
+ def ==(other)
4
+ other.instance_of?(self.class) && other.state == state
5
+ end
6
+ alias_method :eql?, :==
7
+
8
+ def hash
9
+ state.hash
10
+ end
11
+
12
+ def state
13
+ attributes
14
+ end
15
+
16
+ private
17
+
18
+ def attributes
19
+ instance_variables.each_with_object({}) do |name, h|
20
+ h[name[1..-1].to_sym] = instance_variable_get(name)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,111 @@
1
+ module Logica
2
+ class PredicateFactory
3
+ def conjunction(predicates)
4
+ compound_from_list(predicates, conjunction_class)
5
+ end
6
+
7
+ def conjunction_from_pair(first_predicate, second_predicate)
8
+ compound_from_pair(first_predicate, second_predicate, conjunction_class)
9
+ end
10
+
11
+ def disjunction(predicates)
12
+ compound_from_list(predicates, disjunction_class)
13
+ end
14
+
15
+ def disjunction_from_pair(first_predicate, second_predicate)
16
+ compound_from_pair(first_predicate, second_predicate, disjunction_class)
17
+ end
18
+
19
+ def negation(predicate)
20
+ negation_class.new(predicate)
21
+ end
22
+
23
+ def at_least(minimum, predicates)
24
+ if minimum <= 0
25
+ tautology
26
+ elsif minimum == 1
27
+ disjunction(predicates)
28
+ elsif minimum == predicates.size
29
+ conjunction(predicates)
30
+ elsif minimum >= predicates.size
31
+ contradiction
32
+ else
33
+ at_least_class.new(minimum, predicates)
34
+ end
35
+ end
36
+
37
+ def at_most(maximum, predicates)
38
+ minimum = predicates.size - maximum
39
+ negations = predicates.map(&:negated)
40
+
41
+ at_least(minimum, negations)
42
+ end
43
+
44
+ def between(minimum, maximum, predicates)
45
+ at_least(minimum, predicates).and(at_most(maximum, predicates))
46
+ end
47
+
48
+ def exactly(amount, predicates)
49
+ between(amount, amount, predicates)
50
+ end
51
+
52
+ def tautology
53
+ tautology_class.new
54
+ end
55
+
56
+ def contradiction
57
+ contradiction_class.new
58
+ end
59
+
60
+ def ad_hoc(name = 'anonymous', &definition)
61
+ ad_hoc_class.new(name, &definition)
62
+ end
63
+ alias_method :from_block, :ad_hoc
64
+
65
+ def partial_application(predicate, *first_arguments)
66
+ partial_application_class.new(predicate, *first_arguments)
67
+ end
68
+
69
+ private
70
+
71
+ def compound_from_list(predicates, compound_class)
72
+ compound_class.new_from_list(predicates)
73
+ end
74
+
75
+ def compound_from_pair(first_predicate, second_predicate, compound_class)
76
+ compound_class.new_from_pair(first_predicate, second_predicate)
77
+ end
78
+
79
+ def negation_class
80
+ Predicates::Negation
81
+ end
82
+
83
+ def conjunction_class
84
+ Predicates::Compounds::Conjunction
85
+ end
86
+
87
+ def disjunction_class
88
+ Predicates::Compounds::Disjunction
89
+ end
90
+
91
+ def at_least_class
92
+ Predicates::Compounds::AtLeast
93
+ end
94
+
95
+ def tautology_class
96
+ Predicates::Tautology
97
+ end
98
+
99
+ def contradiction_class
100
+ Predicates::Contradiction
101
+ end
102
+
103
+ def ad_hoc_class
104
+ Predicates::AdHoc
105
+ end
106
+
107
+ def partial_application_class
108
+ Predicates::PartialApplication
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,16 @@
1
+ module Logica
2
+ module Predicates
3
+ class AdHoc < Base
4
+ attr_reader :name
5
+
6
+ def initialize(name, &definition)
7
+ @name = name
8
+ define_singleton_method :satisfied_by?, &definition
9
+ end
10
+
11
+ def name_and_attributes
12
+ "#{name}()"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,210 @@
1
+ module Logica
2
+ module Predicates
3
+ class Base
4
+ include ComparableByState
5
+ include OpenHouse::Acceptor
6
+
7
+ ACCEPTOR_TYPE_ID = :predicate
8
+
9
+ def self.predicate_factory
10
+ Logica.predicate_factory
11
+ end
12
+
13
+ def unsatisfied_by?(*arguments)
14
+ !satisfied_by?(*arguments)
15
+ end
16
+
17
+ def and(other)
18
+ other.and_with_other(self)
19
+ end
20
+
21
+ def or(other)
22
+ other.or_with_other(self)
23
+ end
24
+
25
+ def negated
26
+ predicate_factory.negation(self)
27
+ end
28
+
29
+ def and_not(other)
30
+ self.and(other.negated)
31
+ end
32
+
33
+ def or_not(other)
34
+ self.or(other.negated)
35
+ end
36
+
37
+ def xor(other)
38
+ self.or(other).and(self.and(other).negated)
39
+ end
40
+
41
+ def implies(other)
42
+ negated.or(other)
43
+ end
44
+
45
+ def iff(other)
46
+ # this is equivalent to xor(other).negated
47
+ self.and(other).or(self.or(other).negated)
48
+ end
49
+
50
+ def specialization_of?(other)
51
+ other.generalization_of_other?(self)
52
+ end
53
+
54
+ def generalization_of?(other)
55
+ other.specialization_of?(self)
56
+ end
57
+
58
+ def generalization_of_other?(other)
59
+ other == self
60
+ end
61
+
62
+ def generalization_of_negation_of?(other)
63
+ false
64
+ end
65
+
66
+ def disjoint_with?(other)
67
+ other.disjoint_with_other?(self)
68
+ end
69
+
70
+ def exhaustive_with?(other)
71
+ other.exhaustive_with_other?(self)
72
+ end
73
+
74
+ def portion_satisfied_by(*arguments)
75
+ satisfied_by?(*arguments) ? self : predicate_factory.tautology
76
+ end
77
+
78
+ def remainder_unsatisfied_by(*arguments)
79
+ satisfied_by?(*arguments) ? predicate_factory.contradiction : self
80
+ end
81
+
82
+ def partially_applied_with(*first_arguments)
83
+ return self if first_arguments.empty?
84
+ validate_partial_application(first_arguments)
85
+ do_partially_applied_with(first_arguments)
86
+ end
87
+
88
+ def to_method
89
+ method(:satisfied_by?)
90
+ end
91
+
92
+ def arity
93
+ to_method.arity
94
+ end
95
+
96
+ def to_proc
97
+ to_method.to_proc
98
+ end
99
+
100
+ def to_s
101
+ "#{name_and_attributes}#{to_s_suffix}"
102
+ end
103
+
104
+ def name_and_attributes
105
+ "#{name}(#{attributes.values.join(', ')})"
106
+ end
107
+
108
+ def method_missing(name, *args, &block)
109
+ if name.to_s.start_with?('and_with_')
110
+ and_with_other(*args)
111
+ elsif name.to_s.start_with?('or_with_')
112
+ or_with_other(*args)
113
+ elsif name.to_s.start_with?('generalization_of_')
114
+ generalization_of_other?(*args)
115
+ elsif name.to_s.start_with?('disjoint_with_')
116
+ disjoint_with_other?(*args)
117
+ elsif name.to_s.start_with?('exhaustive_with_')
118
+ exhaustive_with_other?(*args)
119
+ else
120
+ ret = do_method_missing(name, *args, &block)
121
+ ret == :__super__ ? super : ret
122
+ end
123
+ end
124
+
125
+ def respond_to_missing?(method_name, include_private = false)
126
+ prefixes = %w(and_with or_with generalization_of_ disjoint_with_ exhaustive_with_)
127
+
128
+ prefixes.any? { |prefix| method_name.to_s.start_with?(prefix) } ||
129
+ begin
130
+ ret = do_respond_to_missing?(method_name, include_private)
131
+ ret == :__super__ ? super : ret
132
+ end
133
+ end
134
+
135
+ protected
136
+
137
+ def and_with_conjunction(conjunction)
138
+ conjunction.with_extra_predicate_last(self)
139
+ end
140
+
141
+ def and_with_at_least(at_least)
142
+ at_least.and_with_other(self, other_first: false)
143
+ end
144
+
145
+ def and_with_other(other)
146
+ predicate_factory.conjunction_from_pair(other, self)
147
+ end
148
+
149
+ def or_with_disjunction(disjunction)
150
+ disjunction.with_extra_predicate_last(self)
151
+ end
152
+
153
+ def or_with_other(other)
154
+ predicate_factory.disjunction_from_pair(other, self)
155
+ end
156
+
157
+ def generalization_of_conjunction?(conjunction)
158
+ conjunction.specialization_of_other?(self)
159
+ end
160
+
161
+ def disjoint_with_other?(other)
162
+ specialization_of?(other.negated)
163
+ end
164
+
165
+ def exhaustive_with_other?(other)
166
+ negated.specialization_of?(other)
167
+ end
168
+
169
+ def without_predicates(preds, options = {})
170
+ preds.include?(self) ? [] : [self]
171
+ end
172
+
173
+ def to_s_suffix
174
+ " |#{arity}|"
175
+ end
176
+
177
+ private
178
+
179
+ def validate_partial_application(first_arguments)
180
+ if first_arguments.size > arity
181
+ raise ArgumentError, partial_application_error_message(first_arguments)
182
+ end
183
+ end
184
+
185
+ def partial_application_error_message(first_arguments)
186
+ "cannot partially apply #{self} with #{first_arguments} (too many arguments)"
187
+ end
188
+
189
+ def do_partially_applied_with(first_arguments)
190
+ predicate_factory.partial_application(self, first_arguments)
191
+ end
192
+
193
+ def name
194
+ self.class.name.split('::').last
195
+ end
196
+
197
+ def predicate_factory
198
+ self.class.predicate_factory
199
+ end
200
+
201
+ def do_method_missing(name, *args, &block)
202
+ :__super__
203
+ end
204
+
205
+ def do_respond_to_missing?(method_name, include_private = false)
206
+ :__super__
207
+ end
208
+ end
209
+ end
210
+ end