leap 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.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +7 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/lib/leap/committee.rb +28 -0
- data/lib/leap/decision.rb +27 -0
- data/lib/leap/quorum.rb +19 -0
- data/lib/leap/subject.rb +17 -0
- data/lib/leap.rb +19 -0
- data/test/helper.rb +59 -0
- data/test/test_decider.rb +13 -0
- metadata +114 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Andy Rossmeissl
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "leap"
|
8
|
+
gem.summary = %Q{A heuristics engine for your Ruby objects}
|
9
|
+
gem.description = %Q{Leap to conclusions}
|
10
|
+
gem.email = "andy@rossmeissl.net"
|
11
|
+
gem.homepage = "http://github.com/rossmeissl/leap"
|
12
|
+
gem.authors = ["Andy Rossmeissl", "Seamus Abshere"]
|
13
|
+
gem.add_development_dependency "shoulda", ">= 0"
|
14
|
+
gem.add_dependency 'blockenspiel'
|
15
|
+
gem.add_dependency 'activesupport', '>=2.3.5'
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
Rake::TestTask.new(:test) do |test|
|
24
|
+
test.libs << 'lib' << 'test'
|
25
|
+
test.pattern = 'test/**/test_*.rb'
|
26
|
+
test.verbose = true
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'rcov/rcovtask'
|
31
|
+
Rcov::RcovTask.new do |test|
|
32
|
+
test.libs << 'test'
|
33
|
+
test.pattern = 'test/**/test_*.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
task :rcov do
|
38
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
task :test => :check_dependencies
|
43
|
+
|
44
|
+
task :default => :test
|
45
|
+
|
46
|
+
require 'rake/rdoctask'
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
49
|
+
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = "decider #{version}"
|
52
|
+
rdoc.rdoc_files.include('README*')
|
53
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
54
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Leap
|
2
|
+
class Committee
|
3
|
+
attr_reader :name, :quorums
|
4
|
+
|
5
|
+
def initialize(name)
|
6
|
+
@name = name
|
7
|
+
@quorums = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def report(characteristics, considerations)
|
11
|
+
quorums.detect do |quorum|
|
12
|
+
next unless quorum.satisfied_by? characteristics
|
13
|
+
if conclusion = quorum.acknowledge(characteristics, considerations)
|
14
|
+
break conclusion
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
include ::Blockenspiel::DSL
|
20
|
+
def quorum(name, options = {}, &blk)
|
21
|
+
@quorums << ::Leap::Quorum.new(name, options, blk)
|
22
|
+
end
|
23
|
+
|
24
|
+
def default(&blk)
|
25
|
+
quorum 'default', {}, &blk
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Leap
|
2
|
+
class Decision
|
3
|
+
attr_reader :goal, :signature_method, :committees
|
4
|
+
|
5
|
+
def initialize(goal, options)
|
6
|
+
@goal = goal
|
7
|
+
@signature_method = options[:with] || {}
|
8
|
+
@committees = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def make(characteristics, *considerations)
|
12
|
+
committees.reverse.inject(characteristics) do |characteristics, committee|
|
13
|
+
if report = committee.report(characteristics, considerations)
|
14
|
+
characteristics[committee.name] = report
|
15
|
+
end
|
16
|
+
characteristics
|
17
|
+
end[goal]
|
18
|
+
end
|
19
|
+
|
20
|
+
include ::Blockenspiel::DSL
|
21
|
+
def committee(name, &blk)
|
22
|
+
committee = ::Leap::Committee.new(name)
|
23
|
+
@committees << committee
|
24
|
+
::Blockenspiel.invoke blk, committee
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/leap/quorum.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module Leap
|
2
|
+
class Quorum
|
3
|
+
attr_reader :name, :requirements, :supplements, :process
|
4
|
+
def initialize(name, options, blk)
|
5
|
+
@name = name
|
6
|
+
@requirements = Array.wrap(options[:needs])
|
7
|
+
@supplements = Array.wrap(options[:appreciates])
|
8
|
+
@process = blk
|
9
|
+
end
|
10
|
+
|
11
|
+
def satisfied_by?(characteristics)
|
12
|
+
(requirements - characteristics.keys).empty?
|
13
|
+
end
|
14
|
+
|
15
|
+
def acknowledge(characteristics, considerations)
|
16
|
+
process.call(*considerations.unshift(characteristics)[0...process.arity])
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/leap/subject.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
module Leap
|
2
|
+
module Subject
|
3
|
+
def self.extended(base)
|
4
|
+
base.instance_variable_set :@decisions, {}
|
5
|
+
class << base
|
6
|
+
attr_reader :decisions
|
7
|
+
end
|
8
|
+
end
|
9
|
+
def decide(goal, options = {}, &blk)
|
10
|
+
decisions[goal] = ::Leap::Decision.new goal, options
|
11
|
+
Blockenspiel.invoke(blk, decisions[goal])
|
12
|
+
define_method goal do |considerations|
|
13
|
+
self.class.decisions[goal].make send(self.class.decisions[goal].signature_method), considerations
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/leap.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'active_support/version'
|
2
|
+
%w{
|
3
|
+
active_support/core_ext/hash/slice
|
4
|
+
active_support/core_ext/array/wrap
|
5
|
+
}.each do |active_support_3_requirement|
|
6
|
+
require active_support_3_requirement
|
7
|
+
end if ActiveSupport::VERSION::MAJOR == 3
|
8
|
+
require 'blockenspiel'
|
9
|
+
|
10
|
+
require 'leap/subject'
|
11
|
+
require 'leap/committee'
|
12
|
+
require 'leap/quorum'
|
13
|
+
require 'leap/decision'
|
14
|
+
|
15
|
+
module Leap
|
16
|
+
def self.included(base)
|
17
|
+
base.extend ::Leap::Subject
|
18
|
+
end
|
19
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
|
+
require 'leap'
|
8
|
+
|
9
|
+
class Test::Unit::TestCase
|
10
|
+
end
|
11
|
+
|
12
|
+
class Person
|
13
|
+
attr_reader :name
|
14
|
+
attr_reader :age
|
15
|
+
attr_reader :date_of_birth
|
16
|
+
|
17
|
+
def initialize(options = {})
|
18
|
+
@name = options[:name].upcase if options[:name] && options[:name].is_a?(String)
|
19
|
+
@age = options[:age].round if options[:age] && options[:age].is_a?(Numeric) && options[:age].round > 0
|
20
|
+
@date_of_birth = options[:date_of_birth] if options[:date_of_birth] && options[:date_of_birth].is_a?(Date)
|
21
|
+
end
|
22
|
+
|
23
|
+
def attributes
|
24
|
+
{ :name => name, :age => age, :date_of_birth => date_of_birth}.delete_if { |key, val| val.nil? }
|
25
|
+
end
|
26
|
+
|
27
|
+
include Leap
|
28
|
+
decide :lucky_number, :with => :attributes do
|
29
|
+
committee :lucky_number do
|
30
|
+
quorum 'super magic method', :needs => [:magic_integer, :magic_float] do |characteristics|
|
31
|
+
characteristics[:magic_integer] + characteristics[:magic_float]
|
32
|
+
end
|
33
|
+
|
34
|
+
quorum 'normal magic method', :needs => :magic_integer do |characteristics|
|
35
|
+
characteristics[:magic_integer] ** 2
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
committee :magic_integer do
|
40
|
+
quorum 'top-secret technique', :needs => [:magic_float, :age] do |characteristics|
|
41
|
+
characteristics[:magic_float].round + characteristics[:age]
|
42
|
+
end
|
43
|
+
|
44
|
+
quorum 'ninja style', :needs => :age do |characteristics|
|
45
|
+
characteristics[:age] + 1
|
46
|
+
end
|
47
|
+
|
48
|
+
default do
|
49
|
+
0
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
committee :magic_float do
|
54
|
+
quorum 'ancient recipe', :needs => :name do |characteristics|
|
55
|
+
('A'..'Z').to_a.index(characteristics[:name].chars.to_a.first) / ('A'..'Z').to_a.index(characteristics[:name].chars.to_a.last)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: leap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Andy Rossmeissl
|
13
|
+
- Seamus Abshere
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-05-12 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: shoulda
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: blockenspiel
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
segments:
|
41
|
+
- 0
|
42
|
+
version: "0"
|
43
|
+
type: :runtime
|
44
|
+
version_requirements: *id002
|
45
|
+
- !ruby/object:Gem::Dependency
|
46
|
+
name: activesupport
|
47
|
+
prerelease: false
|
48
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
segments:
|
53
|
+
- 2
|
54
|
+
- 3
|
55
|
+
- 5
|
56
|
+
version: 2.3.5
|
57
|
+
type: :runtime
|
58
|
+
version_requirements: *id003
|
59
|
+
description: Leap to conclusions
|
60
|
+
email: andy@rossmeissl.net
|
61
|
+
executables: []
|
62
|
+
|
63
|
+
extensions: []
|
64
|
+
|
65
|
+
extra_rdoc_files:
|
66
|
+
- LICENSE
|
67
|
+
- README.rdoc
|
68
|
+
files:
|
69
|
+
- .document
|
70
|
+
- .gitignore
|
71
|
+
- LICENSE
|
72
|
+
- README.rdoc
|
73
|
+
- Rakefile
|
74
|
+
- VERSION
|
75
|
+
- lib/leap.rb
|
76
|
+
- lib/leap/committee.rb
|
77
|
+
- lib/leap/decision.rb
|
78
|
+
- lib/leap/quorum.rb
|
79
|
+
- lib/leap/subject.rb
|
80
|
+
- test/helper.rb
|
81
|
+
- test/test_decider.rb
|
82
|
+
has_rdoc: true
|
83
|
+
homepage: http://github.com/rossmeissl/leap
|
84
|
+
licenses: []
|
85
|
+
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options:
|
88
|
+
- --charset=UTF-8
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
requirements: []
|
106
|
+
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 1.3.6
|
109
|
+
signing_key:
|
110
|
+
specification_version: 3
|
111
|
+
summary: A heuristics engine for your Ruby objects
|
112
|
+
test_files:
|
113
|
+
- test/helper.rb
|
114
|
+
- test/test_decider.rb
|