aska 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +2 -0
- data/LICENSE +0 -0
- data/Manifest +8 -0
- data/README +60 -0
- data/aska.gemspec +31 -0
- data/lib/aska.rb +59 -0
- data/spec/rules_spec.rb +46 -0
- data/spec/spec_helper.rb +11 -0
- metadata +68 -0
data/CHANGELOG
ADDED
data/LICENSE
ADDED
File without changes
|
data/Manifest
ADDED
data/README
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
= AskA
|
2
|
+
Ari Lerner
|
3
|
+
CitrusByte
|
4
|
+
http://blog.citrusbyte.com
|
5
|
+
|
6
|
+
== DESCRIPTION:
|
7
|
+
|
8
|
+
* Super basic rule-based parser to match given rules
|
9
|
+
|
10
|
+
== Basics
|
11
|
+
|
12
|
+
In your class, add your rule definitions
|
13
|
+
|
14
|
+
class Car
|
15
|
+
include Aska
|
16
|
+
attr_accessor :x, :y
|
17
|
+
rules :names, <<-EOR
|
18
|
+
x > 0
|
19
|
+
x > y
|
20
|
+
y > 0
|
21
|
+
EOR
|
22
|
+
end
|
23
|
+
|
24
|
+
Then you can see if the rules are matched with the call
|
25
|
+
|
26
|
+
@car.rules_valid?(:names)
|
27
|
+
|
28
|
+
If they do all match, then the rules_valid? will return true, otherwise it will return false
|
29
|
+
|
30
|
+
== INSTALL:
|
31
|
+
|
32
|
+
gem install aska
|
33
|
+
|
34
|
+
== ROADMAP
|
35
|
+
* v0.0.1 - First release
|
36
|
+
|
37
|
+
== LICENSE:
|
38
|
+
|
39
|
+
(The MIT License)
|
40
|
+
|
41
|
+
Copyright (c) 2008 Ari Lerner. CitrusByte
|
42
|
+
|
43
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
44
|
+
a copy of this software and associated documentation files (the
|
45
|
+
'Software'), to deal in the Software without restriction, including
|
46
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
47
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
48
|
+
permit persons to whom the Software is furnished to do so, subject to
|
49
|
+
the following conditions:
|
50
|
+
|
51
|
+
The above copyright notice and this permission notice shall be
|
52
|
+
included in all copies or substantial portions of the Software.
|
53
|
+
|
54
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
55
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
56
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
57
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
58
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
59
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
60
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/aska.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
|
2
|
+
# Gem::Specification for Aska-0.0.2
|
3
|
+
# Originally generated by Echoe
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = %q{aska}
|
7
|
+
s.version = "0.0.2"
|
8
|
+
|
9
|
+
s.specification_version = 2 if s.respond_to? :specification_version=
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
+
s.authors = [""]
|
13
|
+
s.date = %q{2008-05-28}
|
14
|
+
s.description = %q{}
|
15
|
+
s.email = %q{}
|
16
|
+
s.extra_rdoc_files = ["CHANGELOG", "lib/aska.rb", "LICENSE", "README"]
|
17
|
+
s.files = ["aska.gemspec", "CHANGELOG", "lib/aska.rb", "LICENSE", "Manifest", "README", "spec/rules_spec.rb", "spec/spec_helper.rb"]
|
18
|
+
s.has_rdoc = true
|
19
|
+
s.homepage = %q{}
|
20
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Aska", "--main", "README"]
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
s.rubyforge_project = %q{aska}
|
23
|
+
s.rubygems_version = %q{1.0.1}
|
24
|
+
s.summary = %q{}
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
# # Original Rakefile source (requires the Echoe gem):
|
29
|
+
#
|
30
|
+
# require 'echoe'
|
31
|
+
# Echoe.new('aska')
|
data/lib/aska.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed
|
2
|
+
|
3
|
+
Dir["aska/**"].each {|a| require a }
|
4
|
+
|
5
|
+
module Aska
|
6
|
+
module ClassMethods
|
7
|
+
def rules(name=:rules, str="")
|
8
|
+
r = look_up_rules(name)
|
9
|
+
str.each_line do |line|
|
10
|
+
k = line[/(.+)[=\\\<\>](.*)/, 1].gsub(/\s+/, '')
|
11
|
+
v = line[/(.+)[=\\<>](.*)/, 0].gsub(/\s+/, '')
|
12
|
+
r << {k => v}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
def look_up_rules(name)
|
16
|
+
defined_rules["#{name}"] ||= []
|
17
|
+
end
|
18
|
+
def defined_rules
|
19
|
+
@defined_rules ||= {}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module InstanceMethods
|
24
|
+
def rules
|
25
|
+
@rules ||= self.class.defined_rules
|
26
|
+
end
|
27
|
+
def valid_rules?(name=:rules)
|
28
|
+
self.class.look_up_rules(name).each do |rule|
|
29
|
+
return false unless valid_rule?(rule, name)
|
30
|
+
end
|
31
|
+
return true
|
32
|
+
end
|
33
|
+
def valid_rule?(rule, rules)
|
34
|
+
return false unless rule # Can't apply a rule that is nil, can we?
|
35
|
+
rule.each do |key,value|
|
36
|
+
begin
|
37
|
+
return eval(value)
|
38
|
+
rescue Exception => e
|
39
|
+
return false
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
def method_missing(m, *args)
|
44
|
+
if self.class.defined_rules.has_key?("#{m}")
|
45
|
+
self.class.send(:define_method, m) do
|
46
|
+
self.class.look_up_rules(m)
|
47
|
+
end
|
48
|
+
self.send m
|
49
|
+
else
|
50
|
+
super
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.included(receiver)
|
56
|
+
receiver.extend ClassMethods
|
57
|
+
receiver.send :include, InstanceMethods
|
58
|
+
end
|
59
|
+
end
|
data/spec/rules_spec.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
class Car
|
4
|
+
include Aska
|
5
|
+
attr_accessor :x, :y
|
6
|
+
rules :names, <<-EOR
|
7
|
+
x > 0
|
8
|
+
y > 0
|
9
|
+
x > y
|
10
|
+
EOR
|
11
|
+
end
|
12
|
+
describe "Rules" do
|
13
|
+
before(:each) do
|
14
|
+
@car = Car.new
|
15
|
+
end
|
16
|
+
it "should be able to define rules as an array and they should be set as the rules on the class" do
|
17
|
+
@car.rules.class.should == Hash
|
18
|
+
end
|
19
|
+
it "should be able to look up the rules based on the name into an array" do
|
20
|
+
@car.names.class.should == Array
|
21
|
+
end
|
22
|
+
it "should be able to parse the rules lines into an array" do
|
23
|
+
@car.names.include?({"x"=>"x>0"}).should == true
|
24
|
+
@car.names.include?({"x"=>"x>y"}).should == true
|
25
|
+
@car.names.include?({"y"=>"y>0"}).should == true
|
26
|
+
end
|
27
|
+
it "should be able to apply the rules and say that they are not met when they aren't" do
|
28
|
+
@car.x = 0
|
29
|
+
@car.valid_rules?(:names).should == false
|
30
|
+
end
|
31
|
+
it "should be able to apply the rules and say they aren't valid when they aren't all met" do
|
32
|
+
@car.x = 5
|
33
|
+
@car.y = 10
|
34
|
+
@car.valid_rules?(:names).should == false
|
35
|
+
end
|
36
|
+
it "should be able to apply the rules and say they aren't valid when they aren't all met" do
|
37
|
+
@car.x = 5
|
38
|
+
@car.y = 0
|
39
|
+
@car.valid_rules?(:names).should == false
|
40
|
+
end
|
41
|
+
it "should be able to apply the rules and say that they are in fact valid" do
|
42
|
+
@car.x = 10
|
43
|
+
@car.y = 5
|
44
|
+
@car.valid_rules?(:names).should == true
|
45
|
+
end
|
46
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), *%w[.. lib aska])
|
2
|
+
|
3
|
+
%w(test/spec).each do |library|
|
4
|
+
begin
|
5
|
+
require library
|
6
|
+
rescue
|
7
|
+
STDERR.puts "== Cannot run test without #{library}"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
Dir["#{File.dirname(__FILE__)}/helpers/**"].each {|a| require a}
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aska
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ""
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-05-28 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: ""
|
17
|
+
email: ""
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- CHANGELOG
|
24
|
+
- lib/aska.rb
|
25
|
+
- LICENSE
|
26
|
+
- README
|
27
|
+
files:
|
28
|
+
- aska.gemspec
|
29
|
+
- CHANGELOG
|
30
|
+
- lib/aska.rb
|
31
|
+
- LICENSE
|
32
|
+
- Manifest
|
33
|
+
- README
|
34
|
+
- spec/rules_spec.rb
|
35
|
+
- spec/spec_helper.rb
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: ""
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options:
|
40
|
+
- --line-numbers
|
41
|
+
- --inline-source
|
42
|
+
- --title
|
43
|
+
- Aska
|
44
|
+
- --main
|
45
|
+
- README
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project: aska
|
63
|
+
rubygems_version: 1.0.1
|
64
|
+
signing_key:
|
65
|
+
specification_version: 2
|
66
|
+
summary: ""
|
67
|
+
test_files: []
|
68
|
+
|