rulesy 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.
- data/.document +5 -0
- data/.gitignore +22 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/rulesy.rb +1 -0
- data/lib/rulesy/definition.rb +63 -0
- data/rails/init.rb +1 -0
- data/spec/rulesy_spec.rb +47 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +10 -0
- metadata +87 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Gabe Varela
|
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
@@ -0,0 +1,17 @@
|
|
1
|
+
= rulesy
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2009 Gabe Varela. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "rulesy"
|
8
|
+
gem.summary = %Q{A super simple dsl for managing dynamic business rules}
|
9
|
+
gem.description = %Q{A super simple dsl for managing dynamic business rules}
|
10
|
+
gem.email = "gvarela@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/gvarela/rulesy"
|
12
|
+
gem.authors = ["Gabe Varela"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
gem.add_development_dependency "yard", ">= 0"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
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 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
begin
|
39
|
+
require 'yard'
|
40
|
+
YARD::Rake::YardocTask.new
|
41
|
+
rescue LoadError
|
42
|
+
task :yardoc do
|
43
|
+
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
|
44
|
+
end
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/rulesy.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rulesy/definition'
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Rulesy
|
2
|
+
module Definition
|
3
|
+
@@rule_definitions ||= {}
|
4
|
+
|
5
|
+
def rule_definitions
|
6
|
+
@@rule_definitions
|
7
|
+
end
|
8
|
+
|
9
|
+
def define_rules_for(name, &block)
|
10
|
+
unless included_modules.include?(InstanceMethods)
|
11
|
+
include InstanceMethods
|
12
|
+
end
|
13
|
+
|
14
|
+
rule_definitions[name] = block
|
15
|
+
end
|
16
|
+
|
17
|
+
module InstanceMethods
|
18
|
+
|
19
|
+
def rule(name, message = 'is invalid.', &block)
|
20
|
+
value = instance_eval &block
|
21
|
+
self.business_rules_errors.merge!({name => message}) unless value
|
22
|
+
end
|
23
|
+
|
24
|
+
def business_rules_errors(reload = false)
|
25
|
+
@business_rules_errors = nil if reload
|
26
|
+
@business_rules_errors ||= {}
|
27
|
+
end
|
28
|
+
|
29
|
+
def business_rules_errors=(val)
|
30
|
+
@business_rules_errors = val
|
31
|
+
end
|
32
|
+
|
33
|
+
def rule_definitions
|
34
|
+
self.class.rule_definitions
|
35
|
+
end
|
36
|
+
|
37
|
+
# def can_display
|
38
|
+
# validate_business_rules(:can_display)
|
39
|
+
# end
|
40
|
+
# alias_method :can_display?, :can_display
|
41
|
+
|
42
|
+
def method_missing(method_id, *args)
|
43
|
+
method_name = method_id.to_s.gsub(/\?/, '').to_sym
|
44
|
+
if self.rule_definitions.key?(method_name)
|
45
|
+
instance_variable_set( "@#{method_name}", self.validate_business_rules(method_name)) if instance_variable_get( "@#{method_name}" ).nil?
|
46
|
+
else
|
47
|
+
super
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
protected
|
52
|
+
# This method accepts two parameters: a name for the type of business rules being validated (used for the business_rules_errors hash)
|
53
|
+
# And a hash where the key is the object and the value is the method being called on the object.
|
54
|
+
def validate_business_rules(rules_type)
|
55
|
+
self.business_rules_errors = {}
|
56
|
+
|
57
|
+
instance_eval &rule_definitions[rules_type]
|
58
|
+
|
59
|
+
self.business_rules_errors.empty?
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ActiveRecord::Base.extend Rulesy::Definition
|
data/spec/rulesy_spec.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
class Engine
|
4
|
+
extend Rulesy::Definition
|
5
|
+
define_rules_for :can_start do
|
6
|
+
rule(:has_gas) { has_gas? }
|
7
|
+
rule(:has_wheels, "you don't have valid wheels") { has_wheels? }
|
8
|
+
end
|
9
|
+
|
10
|
+
def has_gas?
|
11
|
+
true
|
12
|
+
end
|
13
|
+
|
14
|
+
def has_wheels?
|
15
|
+
false
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe Rulesy::Definition do
|
20
|
+
|
21
|
+
context "wired up?" do
|
22
|
+
it "should have class method define_rules_for" do
|
23
|
+
Engine.should respond_to(:define_rules_for)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should have rule_definitions class attr" do
|
27
|
+
Engine.should respond_to(:rule_definitions)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should include the InstanceMethods module" do
|
31
|
+
Engine.included_modules.should include(Rulesy::Definition::InstanceMethods)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "have respond to method defined by rule definition" do
|
36
|
+
it "should return false for can_start?"do
|
37
|
+
Engine.new.can_start.should be( false )
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should have a validation message when validation fails" do
|
41
|
+
engine = Engine.new
|
42
|
+
engine.can_start?
|
43
|
+
engine.business_rules_errors.should_not be_empty
|
44
|
+
engine.business_rules_errors.first.should == [:has_wheels, "you don't have valid wheels"]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rulesy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gabe Varela
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-14 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.9
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: yard
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: A super simple dsl for managing dynamic business rules
|
36
|
+
email: gvarela@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- lib/rulesy.rb
|
52
|
+
- lib/rulesy/definition.rb
|
53
|
+
- rails/init.rb
|
54
|
+
- spec/rulesy_spec.rb
|
55
|
+
- spec/spec.opts
|
56
|
+
- spec/spec_helper.rb
|
57
|
+
has_rdoc: true
|
58
|
+
homepage: http://github.com/gvarela/rulesy
|
59
|
+
licenses: []
|
60
|
+
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options:
|
63
|
+
- --charset=UTF-8
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: "0"
|
77
|
+
version:
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.3.5
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: A super simple dsl for managing dynamic business rules
|
85
|
+
test_files:
|
86
|
+
- spec/rulesy_spec.rb
|
87
|
+
- spec/spec_helper.rb
|