auser-aska 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +4 -2
- data/README +9 -0
- data/Rakefile +1 -0
- data/aska.gemspec +3 -2
- data/lib/aska.rb +32 -5
- data/spec/rules_spec.rb +32 -9
- metadata +2 -2
data/CHANGELOG
CHANGED
data/README
CHANGED
@@ -25,8 +25,17 @@
|
|
25
25
|
|
26
26
|
@car.rules_valid?(:names)
|
27
27
|
|
28
|
+
Currently, the methods that are supported are: < > == => =< to check the rules against
|
29
|
+
|
30
|
+
Aska will try to call the method name given if the method exists on the object, or it will revert to a self-created attribute (named: METHODNAME_aska, if you are curious). This way it does not overwrite any methods on accident and you have access to the data.
|
31
|
+
|
32
|
+
This rewrite does not use eval on each check.
|
33
|
+
|
28
34
|
If they do all match, then the rules_valid? will return true, otherwise it will return false
|
29
35
|
|
36
|
+
You can check if a set of rules exist with: are_rules?(:rules)
|
37
|
+
You can check look up the rules manually with: look_up_rules(:rules)
|
38
|
+
|
30
39
|
== INSTALL:
|
31
40
|
|
32
41
|
gem install aska
|
data/Rakefile
CHANGED
data/aska.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = %q{aska}
|
3
|
-
s.version = "0.0.
|
3
|
+
s.version = "0.0.3"
|
4
4
|
|
5
5
|
s.required_rubygems_version = Gem::Requirement.new("= 1.2") if s.respond_to? :required_rubygems_version=
|
6
6
|
s.authors = ["Ari Lerner"]
|
@@ -9,11 +9,12 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.description = %q{The basics of an expert system}
|
10
10
|
s.email = %q{ari.lerner@citrusbyte.com}
|
11
11
|
s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README", "lib", "lib/aska", "lib/aska.rb"]
|
12
|
-
s.files = ["CHANGELOG", "LICENSE", "README", "Rakefile", "lib", "lib/aska", "lib/aska.rb", "
|
12
|
+
s.files = ["CHANGELOG", "LICENSE", "README", "Rakefile", "lib", "lib/aska", "lib/aska.rb", "spec", "spec/rules_spec.rb", "spec/spec_helper.rb", "aska.gemspec"]
|
13
13
|
s.has_rdoc = true
|
14
14
|
s.homepage = %q{http://blog.citrusbyte.com}
|
15
15
|
s.post_install_message = %q{
|
16
16
|
Aska - Expert system basics
|
17
|
+
See blog.citrusbyte.com for more details
|
17
18
|
*** Ari Lerner @ <ari.lerner@citrusbyte.com> ***
|
18
19
|
|
19
20
|
}
|
data/lib/aska.rb
CHANGED
@@ -17,18 +17,26 @@ module Aska
|
|
17
17
|
end
|
18
18
|
def create_instance_variable(name)
|
19
19
|
return unless name
|
20
|
-
attr_accessors
|
21
|
-
|
20
|
+
unless attr_accessors.include?(":#{name}")
|
21
|
+
attr_accessors << name.to_sym
|
22
|
+
eval "attr_accessor :#{aska_named(name)}"
|
23
|
+
end
|
22
24
|
end
|
23
25
|
def look_up_rules(name)
|
24
26
|
defined_rules["#{name}"] ||= []
|
25
27
|
end
|
28
|
+
def are_rules?(name)
|
29
|
+
!look_up_rules(name).empty?
|
30
|
+
end
|
26
31
|
def attr_accessors
|
27
32
|
@attr_accessors ||= []
|
28
33
|
end
|
29
34
|
def defined_rules
|
30
35
|
@defined_rules ||= {}
|
31
36
|
end
|
37
|
+
def aska_named(name)
|
38
|
+
"#{name}_aska"
|
39
|
+
end
|
32
40
|
end
|
33
41
|
|
34
42
|
module InstanceMethods
|
@@ -41,12 +49,19 @@ module Aska
|
|
41
49
|
end
|
42
50
|
arr.reject {|a| a == true }.empty?
|
43
51
|
end
|
52
|
+
def aska(m)
|
53
|
+
if respond_to?(m.to_sym)
|
54
|
+
self.send(m.to_sym)
|
55
|
+
else
|
56
|
+
self.send(aska_named(m.to_sym))
|
57
|
+
end
|
58
|
+
end
|
44
59
|
def valid_rule?(rule)
|
45
60
|
return false unless rule # Can't apply a rule that is nil, can we?
|
46
61
|
rule.each do |key,value|
|
47
62
|
begin
|
48
63
|
# puts "testing if #{key} (#{self.send(key)}) is #{value[0]} #{get_var(value[1])} [#{value[1]}]"
|
49
|
-
return
|
64
|
+
return aska(key).send(value[0].to_sym, get_var(value[1]))
|
50
65
|
rescue Exception => e
|
51
66
|
return false
|
52
67
|
end
|
@@ -56,7 +71,10 @@ module Aska
|
|
56
71
|
# If it's defined as an attr_accessor, we know it has been defined as a rule
|
57
72
|
# Otherwise, if we are passing it as a
|
58
73
|
def get_var(name)
|
59
|
-
attr_accessor?(name) ?
|
74
|
+
attr_accessor?(name) ? aska(name) : (supported_method?(name) ? name.to_sym : name.to_f)
|
75
|
+
end
|
76
|
+
def aska_named(name)
|
77
|
+
self.class.aska_named(name)
|
60
78
|
end
|
61
79
|
def attr_accessor?(name)
|
62
80
|
self.class.attr_accessors.include?(name.to_sym)
|
@@ -64,12 +82,21 @@ module Aska
|
|
64
82
|
def supported_method?(meth)
|
65
83
|
%w(< > == => =<).include?("#{meth}")
|
66
84
|
end
|
67
|
-
|
85
|
+
|
86
|
+
def look_up_rules(r);self.class.look_up_rules(r);end
|
87
|
+
def are_rules?(r);self.class.are_rules?(r);end
|
88
|
+
|
89
|
+
def method_missing(m, *args)
|
68
90
|
if self.class.defined_rules.has_key?("#{m}")
|
69
91
|
self.class.send(:define_method, m) do
|
70
92
|
self.class.look_up_rules(m)
|
71
93
|
end
|
72
94
|
self.send m
|
95
|
+
elsif self.class.attr_accessors.include?(m.to_sym)
|
96
|
+
self.class.send :define_method, aska_named(m).to_sym do
|
97
|
+
self.send(":#{aska_named(m)}")
|
98
|
+
end
|
99
|
+
self.send(":#{aska_named(m)}")
|
73
100
|
else
|
74
101
|
super
|
75
102
|
end
|
data/spec/rules_spec.rb
CHANGED
@@ -7,6 +7,9 @@ class Car
|
|
7
7
|
y > 0
|
8
8
|
x > y
|
9
9
|
EOR
|
10
|
+
def x=(v)
|
11
|
+
@x = v
|
12
|
+
end
|
10
13
|
end
|
11
14
|
describe "Rules" do
|
12
15
|
before(:each) do
|
@@ -18,6 +21,22 @@ describe "Rules" do
|
|
18
21
|
it "should be able to look up the rules based on the name into an array" do
|
19
22
|
@car.names.class.should == Array
|
20
23
|
end
|
24
|
+
it "should be able to say that rules are defined when they are defined" do
|
25
|
+
@car.names.should_not be_nil
|
26
|
+
end
|
27
|
+
it "should be able tos ay that rules are not defined when they are not defined" do
|
28
|
+
@car.look_up_rules(:cars_and_wheels).should be_empty
|
29
|
+
end
|
30
|
+
it "should be able to say if the rules are not rules" do
|
31
|
+
@car.are_rules?(:cars_and_wheels).should be_false
|
32
|
+
end
|
33
|
+
it "should be able to say that rules are rules" do
|
34
|
+
@car.are_rules?(:names).should be_true
|
35
|
+
end
|
36
|
+
it "should define the rule names as a method after calling it" do
|
37
|
+
@car.x_aska = 4
|
38
|
+
@car.methods.include?(@car.aska_named(:x)).should == true
|
39
|
+
end
|
21
40
|
describe "parsing" do
|
22
41
|
it "should be able to parse the x > 0 into an array" do
|
23
42
|
@car.names.include?({"x"=>[">","0"]}).should == true
|
@@ -35,9 +54,13 @@ describe "Rules" do
|
|
35
54
|
Car.look_up_rules(:names).should == [{"x"=>[">", "0"]}, {"y"=>[">", "0"]}, {"x"=>[">", "y"]}]
|
36
55
|
end
|
37
56
|
end
|
57
|
+
it "should use x if available instead of x_aska" do
|
58
|
+
@car.x = 6
|
59
|
+
@car.get_var(:x).class.should == @car.x_aska.class
|
60
|
+
end
|
38
61
|
it "should be able to get the variable associated with the instance and return it" do
|
39
|
-
@car.
|
40
|
-
@car.get_var(:x).class.should == @car.
|
62
|
+
@car.x_aska = 4
|
63
|
+
@car.get_var(:x).class.should == @car.x_aska.class
|
41
64
|
end
|
42
65
|
it "should be able to get a number with the instance and return it as a float" do
|
43
66
|
@car.get_var(4).class.should == Float
|
@@ -46,25 +69,25 @@ describe "Rules" do
|
|
46
69
|
@car.get_var(:<).class.should == Symbol
|
47
70
|
end
|
48
71
|
it "should be able to retrieve the value of the rule when checking if it's valid" do
|
49
|
-
@car.
|
72
|
+
@car.x_aska = 10
|
50
73
|
@car.valid_rule?({:x => [:==, 10]}).should == true
|
51
74
|
end
|
52
75
|
it "should be able to apply the rules and say that they are not met when they aren't" do
|
53
76
|
@car.valid_rules?(:names).should == false
|
54
77
|
end
|
55
78
|
it "should be able to apply the rules and say they aren't valid when they aren't all met" do
|
56
|
-
@car.
|
57
|
-
@car.
|
79
|
+
@car.x_aska = 5
|
80
|
+
@car.y_aska = 10
|
58
81
|
@car.valid_rules?(:names).should == false
|
59
82
|
end
|
60
83
|
it "should be able to apply the rules and say they aren't valid when they aren't all met" do
|
61
|
-
@car.
|
62
|
-
@car.
|
84
|
+
@car.x_aska = 5
|
85
|
+
@car.y_aska = 0
|
63
86
|
@car.valid_rules?(:names).should == false
|
64
87
|
end
|
65
88
|
it "should be able to apply the rules and say that they are in fact valid" do
|
66
|
-
@car.
|
67
|
-
@car.
|
89
|
+
@car.x_aska = 10
|
90
|
+
@car.y_aska = 5
|
68
91
|
@car.valid_rules?(:names).should == true
|
69
92
|
end
|
70
93
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: auser-aska
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ari Lerner
|
@@ -41,7 +41,6 @@ files:
|
|
41
41
|
- lib
|
42
42
|
- lib/aska
|
43
43
|
- lib/aska.rb
|
44
|
-
- pkg
|
45
44
|
- spec
|
46
45
|
- spec/rules_spec.rb
|
47
46
|
- spec/spec_helper.rb
|
@@ -51,6 +50,7 @@ homepage: http://blog.citrusbyte.com
|
|
51
50
|
post_install_message: |+
|
52
51
|
|
53
52
|
Aska - Expert system basics
|
53
|
+
See blog.citrusbyte.com for more details
|
54
54
|
*** Ari Lerner @ <ari.lerner@citrusbyte.com> ***
|
55
55
|
|
56
56
|
rdoc_options:
|