auser-aska 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -6,6 +6,7 @@ begin
6
6
  p.summary = "The basics of an expert system"
7
7
  p.url = "http://blog.citrusbyte.com"
8
8
  p.dependencies = []
9
+ p.version = "0.0.6"
9
10
  p.install_message =<<-EOM
10
11
 
11
12
  Aska - Expert system basics
data/aska.gemspec CHANGED
@@ -1,15 +1,15 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{aska}
3
- s.version = "0.0.5"
3
+ s.version = "0.0.6"
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"]
7
7
  s.cert_chain = nil
8
- s.date = %q{2008-08-01}
8
+ s.date = %q{2008-10-31}
9
9
  s.description = %q{The basics of an expert system}
10
10
  s.email = %q{ari.lerner@citrusbyte.com}
11
- s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README", "lib", "lib/aska.rb"]
12
- s.files = ["CHANGELOG", "LICENSE", "README", "Rakefile", "lib", "lib/aska.rb", "spec", "spec/rules_spec.rb", "spec/spec_helper.rb", "aska.gemspec"]
11
+ s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README", "lib", "lib/aska.rb", "lib/object.rb"]
12
+ s.files = ["CHANGELOG", "LICENSE", "README", "Rakefile", "lib", "lib/aska.rb", "lib/object.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{
data/lib/aska.rb CHANGED
@@ -1,34 +1,37 @@
1
1
  =begin rdoc
2
2
  Aska
3
3
  =end
4
+ require "#{File.dirname(__FILE__)}/object"
4
5
  module Aska
5
6
  module ClassMethods
6
- def rules(name=:rules, str="")
7
- r = look_up_rules(name)
8
- str.each_line do |line|
9
- k = line[/(.+)[=\\\<\>](.*)/, 1].gsub(/\s+/, '')
10
- v = line[/(.+)[=\\<>](.*)/, 2].gsub(/\s+/, '')
11
- m = line[/[=\\<>]/, 0].gsub(/\s+/, '')
12
-
13
- create_instance_variable(k)
14
- r << {k => [m, v]}
7
+ def rules(name=:rules, arr=[])
8
+ returning look_up_rules(name) do |rs|
9
+ arr.each do |line|
10
+ k = line[/(.+)[=\\\<\>](.*)/, 1].gsub(/\s+/, '')
11
+ v = line[/(.+)[=\\<>](.*)/, 2].gsub(/\s+/, '')
12
+ m = line[/[=\\<>]/, 0].gsub(/\s+/, '')
13
+
14
+ create_instance_variable(k)
15
+ rs << {k => [m, v]}
16
+ end
17
+ self.send(:define_method, name) do
18
+ look_up_rules(name)
19
+ end
15
20
  end
16
21
  end
17
- def create_instance_variable(name)
18
- return unless name
19
- unless attr_accessors.include?(":#{name}")
20
- attr_accessors << name.to_sym
21
- eval "attr_accessor :#{aska_named(name)}"
22
- end
22
+ def create_instance_variable(k)
23
+ aska_attr_accessors << k.to_sym unless aska_attr_accessors.include?(k.to_sym)
24
+ attr_reader k.to_sym unless respond_to?("#{k}".to_sym)
25
+ attr_writer k.to_sym unless respond_to?("#{k}=".to_sym)
23
26
  end
24
27
  def look_up_rules(name)
25
- defined_rules["#{name}"] ||= []
28
+ defined_rules[name.to_sym] ||= []
26
29
  end
27
30
  def are_rules?(name)
28
31
  !look_up_rules(name).empty?
29
32
  end
30
- def attr_accessors
31
- @attr_accessors ||= []
33
+ def aska_attr_accessors
34
+ @@aska_attr_accessors ||= []
32
35
  end
33
36
  def defined_rules
34
37
  @defined_rules ||= {}
@@ -43,24 +46,20 @@ module Aska
43
46
  @rules ||= self.class.defined_rules
44
47
  end
45
48
  def valid_rules?(name=:rules)
46
- arr = self.class.look_up_rules(name).collect do |rule|
47
- valid_rule?(rule)
48
- end
49
- arr.reject {|a| a == true }.empty?
49
+ self.class.look_up_rules(name).reject {|rule| valid_rule?(rule) }.empty?
50
50
  end
51
- def aska(m)
51
+ def __aska_aska_stuff(m)
52
52
  if respond_to?(m.to_sym)
53
53
  self.send(m.to_sym)
54
54
  else
55
- self.send(aska_named(m.to_sym))
55
+ m
56
56
  end
57
57
  end
58
58
  def valid_rule?(rule)
59
- return false unless rule # Can't apply a rule that is nil, can we?
60
59
  rule.each do |key,value|
61
60
  begin
62
- # puts "testing if #{key} (#{self.send(key)}) is #{value[0]} #{get_var(value[1])} [#{value[1]}]"
63
- return aska(key).send(value[0].to_sym, get_var(value[1]))
61
+ # puts "#{aska(key)} #{value[0].to_sym} #{get_var(value[1])} (#{attr_accessor?(value[1])})"
62
+ return __aska_aska_stuff(key).send(value[0].to_sym, __aska_get_var(value[1]))
64
63
  rescue Exception => e
65
64
  return false
66
65
  end
@@ -69,14 +68,15 @@ module Aska
69
68
  # Get the variable from the class
70
69
  # If it's defined as an attr_accessor, we know it has been defined as a rule
71
70
  # Otherwise, if we are passing it as a
72
- def get_var(name)
73
- attr_accessor?(name) ? aska(name) : (supported_method?(name) ? name.to_sym : name.to_f)
71
+ def __aska_get_var(name)
72
+ # attr_accessor?(name) ? aska(name) :
73
+ (supported_method?(name) ? name.to_sym : name.to_f)
74
74
  end
75
- def aska_named(name)
75
+ def __aska_aska(name)
76
76
  self.class.aska_named(name)
77
77
  end
78
78
  def attr_accessor?(name)
79
- self.class.attr_accessors.include?(name.to_sym)
79
+ self.class.aska_attr_accessors.include?(name.to_sym)
80
80
  end
81
81
  def supported_method?(meth)
82
82
  %w(< > == => =<).include?("#{meth}")
@@ -85,21 +85,16 @@ module Aska
85
85
  def look_up_rules(r);self.class.look_up_rules(r);end
86
86
  def are_rules?(r);self.class.are_rules?(r);end
87
87
 
88
- def method_missing(m, *args)
89
- if self.class.defined_rules.has_key?("#{m}")
90
- self.class.send(:define_method, m) do
91
- self.class.look_up_rules(m)
92
- end
93
- self.send m
94
- elsif self.class.attr_accessors.include?(m.to_sym)
95
- self.class.send :define_method, aska_named(m).to_sym do
96
- # self.send(aska_named(m).to_sym)
97
- end
98
- self.send(aska_named(m).to_sym)
99
- else
100
- super
101
- end
102
- end
88
+ # def method_missing(m, *args, &block)
89
+ # if self.class.defined_rules.has_key?(m.to_sym)
90
+ # self.class.send(:define_method, m) do
91
+ # self.class.look_up_rules(m)
92
+ # end
93
+ # self.send m
94
+ # else
95
+ # super
96
+ # end
97
+ # end
103
98
  end
104
99
 
105
100
  def self.included(receiver)
data/lib/object.rb ADDED
@@ -0,0 +1,6 @@
1
+ class Object
2
+ def returning(receiver)
3
+ yield receiver
4
+ receiver
5
+ end
6
+ end
data/spec/rules_spec.rb CHANGED
@@ -2,14 +2,11 @@ require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  class Car
4
4
  include Aska
5
- rules :names, <<-EOR
6
- x > 0
7
- y > 0
8
- x > y
9
- EOR
10
- def x=(v)
11
- @x = v
12
- end
5
+ rules :names, [
6
+ "x > 0",
7
+ "y > 0",
8
+ "x > y"
9
+ ]
13
10
  end
14
11
  describe "Rules" do
15
12
  before(:each) do
@@ -33,10 +30,6 @@ describe "Rules" do
33
30
  it "should be able to say that rules are rules" do
34
31
  @car.are_rules?(:names).should be_true
35
32
  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
40
33
  describe "parsing" do
41
34
  it "should be able to parse the x > 0 into an array" do
42
35
  @car.names.include?({"x"=>[">","0"]}).should == true
@@ -54,40 +47,48 @@ describe "Rules" do
54
47
  Car.look_up_rules(:names).should == [{"x"=>[">", "0"]}, {"y"=>[">", "0"]}, {"x"=>[">", "y"]}]
55
48
  end
56
49
  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
61
- it "should be able to get the variable associated with the instance and return it" do
62
- @car.x_aska = 4
63
- @car.get_var(:x).class.should == @car.x_aska.class
64
- end
50
+ # it "should use x if available instead of x_aska" do
51
+ # @car.x = 6
52
+ # @car.get_var(:x).class.should == @car.x_aska.class
53
+ # end
54
+ # it "should be able to get the variable associated with the instance and return it" do
55
+ # @car.x_aska = 4
56
+ # @car.get_var(:x).class.should == @car.x_aska.class
57
+ # end
65
58
  it "should be able to get a number with the instance and return it as a float" do
66
- @car.get_var(4).class.should == Float
59
+ @car.__aska_get_var(4).class.should == Float
67
60
  end
68
61
  it "should be able to get the method it's applying as a method symbol" do
69
- @car.get_var(:<).class.should == Symbol
62
+ @car.__aska_get_var(:<).class.should == Symbol
63
+ end
64
+ it "should be able to get the method as a symbol" do
65
+ @car.__aska_get_var("<").class.should == Symbol
70
66
  end
71
67
  it "should be able to retrieve the value of the rule when checking if it's valid" do
72
- @car.x_aska = 10
68
+ @car.x = 10
73
69
  @car.valid_rule?({:x => [:==, 10]}).should == true
74
70
  end
75
71
  it "should be able to apply the rules and say that they are not met when they aren't" do
76
72
  @car.valid_rules?(:names).should == false
77
73
  end
78
74
  it "should be able to apply the rules and say they aren't valid when they aren't all met" do
79
- @car.x_aska = 5
80
- @car.y_aska = 10
81
- @car.valid_rules?(:names).should == false
75
+ @car.x = 5
76
+ @car.y = 10
77
+ @car.valid_rules?(:names).should == true
82
78
  end
83
79
  it "should be able to apply the rules and say they aren't valid when they aren't all met" do
84
- @car.x_aska = 5
85
- @car.y_aska = 0
80
+ @car.x = 5
81
+ @car.y = 0
86
82
  @car.valid_rules?(:names).should == false
87
83
  end
88
84
  it "should be able to apply the rules and say that they are in fact valid" do
89
- @car.x_aska = 10
90
- @car.y_aska = 5
85
+ @car.x = 10
86
+ @car.y = 5
91
87
  @car.valid_rules?(:names).should == true
92
88
  end
89
+ it "should have the rules in an array of hashes" do
90
+ @car.names.each do |n|
91
+
92
+ end
93
+ end
93
94
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auser-aska
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ari Lerner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain:
11
- date: 2008-08-01 00:00:00 -07:00
11
+ date: 2008-10-31 00:00:00 -07:00
12
12
  default_executable:
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
@@ -32,6 +32,7 @@ extra_rdoc_files:
32
32
  - README
33
33
  - lib
34
34
  - lib/aska.rb
35
+ - lib/object.rb
35
36
  files:
36
37
  - CHANGELOG
37
38
  - LICENSE
@@ -39,6 +40,7 @@ files:
39
40
  - Rakefile
40
41
  - lib
41
42
  - lib/aska.rb
43
+ - lib/object.rb
42
44
  - spec
43
45
  - spec/rules_spec.rb
44
46
  - spec/spec_helper.rb