conditioner 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +22 -33
- data/Rakefile +38 -27
- data/init.rb +1 -0
- data/lib/conditioner.rb +10 -0
- data/lib/conditioner/condition.rb +9 -23
- data/lib/conditioner/configurator.rb +34 -0
- data/test/{test_conditioner.rb → conditioner_test.rb} +18 -6
- data/test/test_helper.rb +1 -1
- metadata +20 -65
- data/script/console +0 -10
- data/script/destroy +0 -14
- data/script/generate +0 -14
data/README.rdoc
CHANGED
@@ -4,55 +4,44 @@
|
|
4
4
|
|
5
5
|
== DESCRIPTION:
|
6
6
|
|
7
|
-
|
7
|
+
Simple conditions builder for active_record
|
8
8
|
|
9
|
-
==
|
9
|
+
== INSTALL:
|
10
10
|
|
11
|
-
|
12
|
-
cnd = User.conditioner
|
13
|
-
assert_not_nil(cnd)
|
14
|
-
params={:name=>'nicola',:email=>'nicola@mail.com'}
|
15
|
-
cnd = User.conditioner(params)
|
16
|
-
assert_not_nil(cnd)
|
17
|
-
end
|
11
|
+
in config/environment.rb:
|
18
12
|
|
19
|
-
|
20
|
-
cnd=User.conditioner(:updated_at=>'2009-01-01', :email=>'nicola@mail.com',:unexisting=>'value')
|
21
|
-
assert_match(/"users"."email"\s*=\s*E'nicola@mail.com'/, cnd)
|
22
|
-
assert_match(/users.updated_at BETWEEN E'2009-01-01 00:00' AND E'2009-01-01 23:59:59.999'/, cnd)
|
23
|
-
assert_no_match(/unexisting/, cnd)
|
24
|
-
end
|
13
|
+
config.gem 'conditioner'
|
25
14
|
|
26
|
-
|
27
|
-
cnd = User.conditioner :to_updated_at =>'2010-02-02', :from_updated_at=>'2009-01-01'
|
28
|
-
assert_match(/updated_at <= E'2010-02-02 23:59:59.999'/, cnd)
|
29
|
-
assert_match(/updated_at >= E'2009-01-01 00:00:00.000'/, cnd)
|
30
|
-
end
|
15
|
+
and then
|
31
16
|
|
32
|
-
|
33
|
-
cnd = User.conditioner :name=>'*nicola*'
|
34
|
-
assert_match(/name ILIKE E'%nicola%'/, cnd)
|
35
|
-
end
|
17
|
+
sudo rake gems:install
|
36
18
|
|
37
|
-
|
19
|
+
To override Conditioner configurations create config/initializers/conditioner.rb with:
|
38
20
|
|
39
|
-
* Add pluguble rules for extactions
|
40
21
|
|
41
|
-
|
22
|
+
Conditioner.configure do
|
42
23
|
|
43
|
-
Conditioner.configure do
|
44
24
|
extraction_rule do |key,value|
|
45
25
|
if /.*_gt/ =~ key
|
46
26
|
["#{table_name}.#{key.gsub(/_gt$/,'')} > ?",value]
|
47
27
|
end
|
48
28
|
end
|
49
|
-
|
29
|
+
end
|
50
30
|
|
51
|
-
==
|
31
|
+
== USAGE
|
52
32
|
|
53
|
-
|
54
|
-
|
55
|
-
|
33
|
+
#this create conditions from params using extract rules
|
34
|
+
cnd = YourModel.conditioner(params)
|
35
|
+
#<b>and</b> and <b>or</b> eat same parameters as :conditions option in ActiveRecord.find
|
36
|
+
cnd.and :field=>'value'
|
37
|
+
cnd.and ['field='?',value]
|
38
|
+
|
39
|
+
#here we pass condition into find methods
|
40
|
+
YourModel.all(:conditions=>cnd)
|
41
|
+
|
42
|
+
== CHANGE LIST:
|
43
|
+
|
44
|
+
* 0.0.2 Add configurable rules for extactions
|
56
45
|
|
57
46
|
== LICENSE:
|
58
47
|
|
data/Rakefile
CHANGED
@@ -1,35 +1,46 @@
|
|
1
|
-
require '
|
2
|
-
|
3
|
-
require '
|
4
|
-
require '
|
5
|
-
require './lib/conditioner'
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'rake/gempackagetask'
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
# Hoe.plugin :cucumberfeatures
|
6
|
+
desc 'Default: run unit tests.'
|
7
|
+
task :default => :test
|
10
8
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
9
|
+
desc 'Test the pg_gnostic plugin.'
|
10
|
+
Rake::TestTask.new(:test) do |t|
|
11
|
+
t.libs << 'lib'
|
12
|
+
t.libs << 'test'
|
13
|
+
t.pattern = 'test/**/*_test.rb'
|
14
|
+
t.verbose = true
|
15
|
+
end
|
18
16
|
|
17
|
+
desc 'Generate documentation for the pg_gnostic plugin.'
|
18
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
19
|
+
rdoc.rdoc_dir = 'rdoc'
|
20
|
+
rdoc.title = 'Conditioner'
|
21
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
22
|
+
rdoc.rdoc_files.include('README.rdoc')
|
23
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
19
24
|
end
|
20
25
|
|
21
|
-
require 'newgem/tasks'
|
22
|
-
Dir['tasks/**/*.rake'].each { |t| load t }
|
23
26
|
|
27
|
+
PKG_FILES = FileList[ '[a-zA-Z]*', 'generators/**/*', 'lib/**/*', 'rails/**/*', 'tasks/**/*', 'test/**/*' ]
|
28
|
+
|
29
|
+
spec = Gem::Specification.new do |s|
|
30
|
+
s.name = "conditioner"
|
31
|
+
s.version = "0.0.2"
|
32
|
+
s.author = "niquola"
|
33
|
+
s.email = "niquola@gmail.com"
|
34
|
+
s.homepage = "http://github.com/niquola/conditioner"
|
35
|
+
s.platform = Gem::Platform::RUBY
|
36
|
+
s.summary = "Simple conditions builder for active_record"
|
37
|
+
s.files = PKG_FILES.to_a
|
38
|
+
s.require_path = "lib"
|
39
|
+
s.has_rdoc = false
|
40
|
+
s.extra_rdoc_files = ["README.rdoc"]
|
41
|
+
end
|
24
42
|
|
25
|
-
desc '
|
26
|
-
|
27
|
-
|
28
|
-
gem_name = File.basename(Dir['pkg/*.gem'].max)
|
29
|
-
last_gem = File.join(File.dirname(__FILE__),'pkg',gem_name);
|
30
|
-
server = 'demo'
|
31
|
-
gem_copy_path = "/tmp/#{gem_name}"
|
32
|
-
system "scp #{last_gem} #{server}:#{gem_copy_path}"
|
33
|
-
system "ssh -t #{server} sudo gem install #{gem_copy_path}"
|
34
|
-
end
|
43
|
+
desc 'Turn this plugin into a gem.'
|
44
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
45
|
+
pkg.gem_spec = spec
|
35
46
|
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'conditioner'
|
data/lib/conditioner.rb
CHANGED
@@ -5,14 +5,24 @@ module Conditioner
|
|
5
5
|
VERSION = '0.0.1'
|
6
6
|
autoload :ActiveRecordMixin, 'conditioner/active_record_mixin'
|
7
7
|
autoload :Condition, 'conditioner/condition'
|
8
|
+
autoload :Configurator, 'conditioner/configurator'
|
8
9
|
|
9
10
|
class << self
|
10
11
|
def enable
|
11
12
|
ActiveRecord::Base.send :extend, ActiveRecordMixin
|
13
|
+
Conditioner.config.activate_default_rules!
|
12
14
|
end
|
13
15
|
|
14
16
|
def condition(table_name,fields)
|
15
17
|
end
|
18
|
+
|
19
|
+
def config
|
20
|
+
@config ||= Configurator.new
|
21
|
+
end
|
22
|
+
|
23
|
+
def configure
|
24
|
+
yield config
|
25
|
+
end
|
16
26
|
end
|
17
27
|
end
|
18
28
|
|
@@ -25,6 +25,7 @@ module Conditioner
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def add_condition(unit,*args)
|
28
|
+
@condition_called_flag= true
|
28
29
|
result= []
|
29
30
|
unless @first_condition
|
30
31
|
result<<' '<<unit
|
@@ -37,37 +38,22 @@ module Conditioner
|
|
37
38
|
end
|
38
39
|
|
39
40
|
def is_field?(field)
|
40
|
-
@column_names.include?(field
|
41
|
+
@column_names.include?(field)
|
41
42
|
end
|
42
43
|
|
43
|
-
def
|
44
|
+
def with_table_name(field_name)
|
44
45
|
%Q[#{@model.table_name}.#{field_name}]
|
45
46
|
end
|
46
47
|
|
47
|
-
#FIXME: document conventions
|
48
|
-
#add more conventions like
|
49
|
-
#name_start_from
|
50
|
-
#field_end_with
|
51
|
-
#field_in_range
|
52
|
-
#field_gt
|
53
|
-
#field_mt
|
54
48
|
def extract(hash)
|
55
49
|
hash.each do |k,v|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
_and(["upper(#{c(k)}) like upper(?)",v])
|
61
|
-
#FIXME: add logic for ranges
|
62
|
-
elsif k.to_s =~ /^from_(\w*_(datetime|at))/
|
63
|
-
_and(["#{c($1)} >= ?","#{v} 00:00:00.000"])
|
64
|
-
elsif k.to_s =~ /^to_(\w*_(datetime|at))/
|
65
|
-
_and(["#{c($1)} <= ?","#{v} 23:59:59.999"])
|
66
|
-
elsif k.to_s.include?('_datetime') || k.to_s.include?('_at')
|
67
|
-
_and(["(#{c(k)} BETWEEN ? AND ?)","#{v} 00:00","#{v} 23:59:59.999"])
|
68
|
-
else
|
69
|
-
_and(k=>v)
|
50
|
+
field = k.to_s
|
51
|
+
Conditioner.config.extract_rules.each do |rule|
|
52
|
+
rule.call(field,v,self)
|
53
|
+
break if @condition_called_flag
|
70
54
|
end
|
55
|
+
_and(field=>v) if is_field?(field) and !@condition_called_flag
|
56
|
+
@condition_called_flag = false
|
71
57
|
end
|
72
58
|
self
|
73
59
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Conditioner
|
2
|
+
class Configurator
|
3
|
+
|
4
|
+
def extract_rules
|
5
|
+
@rules ||= []
|
6
|
+
end
|
7
|
+
|
8
|
+
def add_rule(&rule)
|
9
|
+
extract_rules<< rule
|
10
|
+
end
|
11
|
+
|
12
|
+
#You can clear default rules with
|
13
|
+
def clear_rules!
|
14
|
+
@rules = []
|
15
|
+
end
|
16
|
+
|
17
|
+
def activate_default_rules!
|
18
|
+
add_rule do |field, v, cnd|
|
19
|
+
if cnd.is_field?(field) && v =~ /(\*$|^\*)/
|
20
|
+
cnd.and(["#{cnd.with_table_name(field)} ILIKE ?",v.gsub(/(\*$|^\*)/,'%')])
|
21
|
+
elsif cnd.is_field?(field) && v =~ /(%$|^%)/
|
22
|
+
cnd.and(["upper(#{cnd.with_table_name(field)}) like upper(?)",v])
|
23
|
+
#FIXME: add logic for ranges
|
24
|
+
elsif field=~ /^from_(\w*_(datetime|at))/ and cnd.is_field?($1)
|
25
|
+
cnd.and(["#{cnd.with_table_name($1)} >= ?","#{v} 00:00:00.000"])
|
26
|
+
elsif field=~ /^to_(\w*_(datetime|at))/ and cnd.is_field?($1)
|
27
|
+
cnd.and(["#{cnd.with_table_name($1)} <= ?","#{v} 23:59:59.999"])
|
28
|
+
elsif cnd.is_field?(field) && (field.include?('_datetime') || field.include?('_at'))
|
29
|
+
cnd.and(["(#{cnd.with_table_name(field)} BETWEEN ? AND ?)","#{v} 00:00","#{v} 23:59:59.999"])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -1,5 +1,17 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
2
|
|
3
|
+
Conditioner.configure do |cfg|
|
4
|
+
#Must be activated by default
|
5
|
+
#cfg.activate_default_rules!
|
6
|
+
#You can clear default rules with
|
7
|
+
#cfg.clear_rules!
|
8
|
+
|
9
|
+
cfg.add_rule do |key, value, cnd|
|
10
|
+
if /(.\w*)_gt/ =~ key.to_s && cnd.is_field?($1)
|
11
|
+
cnd.and(["#{key.gsub(/_gt$/,'')} > ?",value])
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
3
15
|
|
4
16
|
class TestConditioner < Test::Unit::TestCase
|
5
17
|
|
@@ -11,12 +23,6 @@ class TestConditioner < Test::Unit::TestCase
|
|
11
23
|
assert_not_nil(cnd)
|
12
24
|
end
|
13
25
|
|
14
|
-
def test_without_model_conditioner
|
15
|
-
#table_name = 'roles'
|
16
|
-
#fields = %w{name created_at}
|
17
|
-
#Conditioner.condition(table_name,fields)
|
18
|
-
end
|
19
|
-
|
20
26
|
def test_extraction
|
21
27
|
cnd=User.conditioner(:updated_at=>'2009-01-01', :email=>'nicola@mail.com',:unexisting=>'value')
|
22
28
|
assert_match(/"users"."email"\s*=\s*E'nicola@mail.com'/, cnd)
|
@@ -33,5 +39,11 @@ class TestConditioner < Test::Unit::TestCase
|
|
33
39
|
def test_ilike
|
34
40
|
cnd = User.conditioner :name=>'*nicola*'
|
35
41
|
assert_match(/name ILIKE E'%nicola%'/, cnd)
|
42
|
+
assert_no_match(/\*nicola\*/, cnd,'Rule must work only once!')
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_configurator
|
46
|
+
cnd = User.conditioner :created_at_gt => '2010-01-01'
|
47
|
+
assert_match(/created_at > E'2010-01-01'/, cnd)
|
36
48
|
end
|
37
49
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: conditioner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- niquola
|
@@ -9,82 +9,38 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-03-
|
12
|
+
date: 2010-03-04 00:00:00 +03:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
version_requirement:
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">="
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 2.3.5
|
24
|
-
version:
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: rubyforge
|
27
|
-
type: :development
|
28
|
-
version_requirement:
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: 2.0.3
|
34
|
-
version:
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
name: gemcutter
|
37
|
-
type: :development
|
38
|
-
version_requirement:
|
39
|
-
version_requirements: !ruby/object:Gem::Requirement
|
40
|
-
requirements:
|
41
|
-
- - ">="
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: 0.3.0
|
44
|
-
version:
|
45
|
-
- !ruby/object:Gem::Dependency
|
46
|
-
name: hoe
|
47
|
-
type: :development
|
48
|
-
version_requirement:
|
49
|
-
version_requirements: !ruby/object:Gem::Requirement
|
50
|
-
requirements:
|
51
|
-
- - ">="
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: 2.5.0
|
54
|
-
version:
|
55
|
-
description: Simple conditions builder for active_resource
|
56
|
-
email:
|
57
|
-
- niquola@gmail.com
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: niquola@gmail.com
|
58
18
|
executables: []
|
59
19
|
|
60
20
|
extensions: []
|
61
21
|
|
62
22
|
extra_rdoc_files:
|
63
|
-
-
|
64
|
-
- Manifest.txt
|
65
|
-
- PostInstall.txt
|
23
|
+
- README.rdoc
|
66
24
|
files:
|
67
|
-
-
|
25
|
+
- init.rb
|
68
26
|
- Manifest.txt
|
69
27
|
- PostInstall.txt
|
70
|
-
- README.rdoc
|
71
28
|
- Rakefile
|
29
|
+
- README.rdoc
|
30
|
+
- History.txt
|
72
31
|
- lib/conditioner.rb
|
73
|
-
- lib/conditioner/active_record_mixin.rb
|
74
32
|
- lib/conditioner/condition.rb
|
75
|
-
-
|
76
|
-
-
|
77
|
-
-
|
78
|
-
- test/test_conditioner.rb
|
33
|
+
- lib/conditioner/active_record_mixin.rb
|
34
|
+
- lib/conditioner/configurator.rb
|
35
|
+
- test/conditioner_test.rb
|
79
36
|
- test/test_helper.rb
|
80
37
|
has_rdoc: true
|
81
38
|
homepage: http://github.com/niquola/conditioner
|
82
39
|
licenses: []
|
83
40
|
|
84
|
-
post_install_message:
|
85
|
-
rdoc_options:
|
86
|
-
|
87
|
-
- README.rdoc
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
88
44
|
require_paths:
|
89
45
|
- lib
|
90
46
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -101,11 +57,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
101
57
|
version:
|
102
58
|
requirements: []
|
103
59
|
|
104
|
-
rubyforge_project:
|
60
|
+
rubyforge_project:
|
105
61
|
rubygems_version: 1.3.5
|
106
62
|
signing_key:
|
107
63
|
specification_version: 3
|
108
|
-
summary: Simple conditions builder for
|
109
|
-
test_files:
|
110
|
-
|
111
|
-
- test/test_helper.rb
|
64
|
+
summary: Simple conditions builder for active_record
|
65
|
+
test_files: []
|
66
|
+
|
data/script/console
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# File: script/console
|
3
|
-
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
-
|
5
|
-
libs = " -r irb/completion"
|
6
|
-
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
-
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
-
libs << " -r #{File.dirname(__FILE__) + '/../lib/conditioner.rb'}"
|
9
|
-
puts "Loading conditioner gem"
|
10
|
-
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
-
|
4
|
-
begin
|
5
|
-
require 'rubigen'
|
6
|
-
rescue LoadError
|
7
|
-
require 'rubygems'
|
8
|
-
require 'rubigen'
|
9
|
-
end
|
10
|
-
require 'rubigen/scripts/destroy'
|
11
|
-
|
12
|
-
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
-
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
-
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
-
|
4
|
-
begin
|
5
|
-
require 'rubigen'
|
6
|
-
rescue LoadError
|
7
|
-
require 'rubygems'
|
8
|
-
require 'rubigen'
|
9
|
-
end
|
10
|
-
require 'rubigen/scripts/generate'
|
11
|
-
|
12
|
-
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
-
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
-
RubiGen::Scripts::Generate.new.run(ARGV)
|