conditioner 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2010-02-22
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,13 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/conditioner.rb
7
+ lib/conditioner/active_record_mixin.rb
8
+ lib/conditioner/condition.rb
9
+ script/console
10
+ script/destroy
11
+ script/generate
12
+ test/test_conditioner.rb
13
+ test/test_helper.rb
data/PostInstall.txt ADDED
@@ -0,0 +1,7 @@
1
+
2
+ For more information on conditioner, see http://conditioner.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
data/README.rdoc ADDED
@@ -0,0 +1,80 @@
1
+ = conditioner
2
+
3
+ * http://github.com/niquola/conditioner
4
+
5
+ == DESCRIPTION:
6
+
7
+ Simple conditions builder for active_resource
8
+
9
+ == SYNOPSIS:
10
+
11
+ def test_conditioner_creation
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
18
+
19
+ def test_extraction
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
25
+
26
+ def test_extract_from_and_to_prefixed_date_fields
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
31
+
32
+ def test_ilike
33
+ cnd = User.conditioner :name=>'*nicola*'
34
+ assert_match(/name ILIKE E'%nicola%'/, cnd)
35
+ end
36
+
37
+ == TODO:
38
+
39
+ * Add pluguble rules for extactions
40
+
41
+ Example:
42
+
43
+ Conditioner.configure do
44
+ extraction_rule do |key,value|
45
+ if /.*_gt/ =~ key
46
+ ["#{table_name}.#{key.gsub(/_gt$/,'')} > ?",value]
47
+ end
48
+ end
49
+ end
50
+
51
+ == INSTALL:
52
+
53
+ in config/environmen.rb
54
+ gem 'conditioner'
55
+ sudo rake gems:install
56
+
57
+ == LICENSE:
58
+
59
+ (The MIT License)
60
+
61
+ Copyright (c) 2010 niquola
62
+
63
+ Permission is hereby granted, free of charge, to any person obtaining
64
+ a copy of this software and associated documentation files (the
65
+ 'Software'), to deal in the Software without restriction, including
66
+ without limitation the rights to use, copy, modify, merge, publish,
67
+ distribute, sublicense, and/or sell copies of the Software, and to
68
+ permit persons to whom the Software is furnished to do so, subject to
69
+ the following conditions:
70
+
71
+ The above copyright notice and this permission notice shall be
72
+ included in all copies or substantial portions of the Software.
73
+
74
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
75
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
76
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
77
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
78
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
79
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
80
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/conditioner'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'conditioner' do
14
+ self.developer 'niquola', 'niquola@gmail.com'
15
+ self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
16
+ self.rubyforge_name = self.name # TODO this is default value
17
+ self.extra_deps = [['activerecord','>= 2.3.5']]
18
+
19
+ end
20
+
21
+ require 'newgem/tasks'
22
+ Dir['tasks/**/*.rake'].each { |t| load t }
23
+
24
+
25
+ desc 'push gem to medapp'
26
+ namespace :medapp do
27
+ task :push do
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
35
+ end
@@ -0,0 +1,21 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module Conditioner
5
+ VERSION = '0.0.1'
6
+ autoload :ActiveRecordMixin, 'conditioner/active_record_mixin'
7
+ autoload :Condition, 'conditioner/condition'
8
+
9
+ class << self
10
+ def enable
11
+ ActiveRecord::Base.send :extend, ActiveRecordMixin
12
+ end
13
+
14
+ def condition(table_name,fields)
15
+ end
16
+ end
17
+ end
18
+
19
+ if defined? Rails
20
+ Conditioner.enable if defined? ActiveRecord
21
+ end
@@ -0,0 +1,9 @@
1
+ module Conditioner
2
+ module ActiveRecordMixin
3
+ def conditioner(params=nil)
4
+ cnd = Conditioner::Condition.new(self)
5
+ cnd.extract params if params
6
+ cnd
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,75 @@
1
+ module Conditioner
2
+ class Condition < String
3
+ def initialize(model)
4
+ @model=model
5
+ @result=[]
6
+ @column_names=@model.column_names
7
+ @first_condition=true
8
+ yield self if block_given?
9
+ end
10
+
11
+ def and(*args)
12
+ add_condition('AND',*args)
13
+ end
14
+
15
+ def _and(*args)
16
+ add_condition('AND',*args)
17
+ end
18
+
19
+ def or(*args)
20
+ add_condition('OR',*args)
21
+ end
22
+
23
+ def _or(*args)
24
+ add_condition('OR',*args)
25
+ end
26
+
27
+ def add_condition(unit,*args)
28
+ result= []
29
+ unless @first_condition
30
+ result<<' '<<unit
31
+ else
32
+ @first_condition=false
33
+ end
34
+ result<<@model.send(:sanitize_sql_for_conditions, *args)
35
+ self<< result.join(" ")
36
+ self
37
+ end
38
+
39
+ def is_field?(field)
40
+ @column_names.include?(field.to_s) || (field.to_s =~ /^(from|to)_(\w*_(datetime|at))/ && @column_names.include?($2))
41
+ end
42
+
43
+ def c(field_name)
44
+ %Q[#{@model.table_name}.#{field_name}]
45
+ end
46
+
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
+ def extract(hash)
55
+ hash.each do |k,v|
56
+ next unless is_field?(k)
57
+ if v =~ /(\*$|^\*)/
58
+ _and(["#{c(k)} ILIKE ?",v.gsub(/(\*$|^\*)/,'%')])
59
+ elsif v =~ /(%$|^%)/
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)
70
+ end
71
+ end
72
+ self
73
+ end
74
+ end
75
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
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 ADDED
@@ -0,0 +1,14 @@
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 ADDED
@@ -0,0 +1,14 @@
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)
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+
4
+ class TestConditioner < Test::Unit::TestCase
5
+
6
+ def test_conditioner_creation
7
+ cnd = User.conditioner
8
+ assert_not_nil(cnd)
9
+ params={:name=>'nicola',:email=>'nicola@mail.com'}
10
+ cnd = User.conditioner(params)
11
+ assert_not_nil(cnd)
12
+ end
13
+
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
+ def test_extraction
21
+ cnd=User.conditioner(:updated_at=>'2009-01-01', :email=>'nicola@mail.com',:unexisting=>'value')
22
+ assert_match(/"users"."email"\s*=\s*E'nicola@mail.com'/, cnd)
23
+ assert_match(/users.updated_at BETWEEN E'2009-01-01 00:00' AND E'2009-01-01 23:59:59.999'/, cnd)
24
+ assert_no_match(/unexisting/, cnd)
25
+ end
26
+
27
+ def test_extract_from_and_to_prefixed_date_fields
28
+ cnd = User.conditioner :to_updated_at =>'2010-02-02', :from_updated_at=>'2009-01-01'
29
+ assert_match(/updated_at <= E'2010-02-02 23:59:59.999'/, cnd)
30
+ assert_match(/updated_at >= E'2009-01-01 00:00:00.000'/, cnd)
31
+ end
32
+
33
+ def test_ilike
34
+ cnd = User.conditioner :name=>'*nicola*'
35
+ assert_match(/name ILIKE E'%nicola%'/, cnd)
36
+ end
37
+ end
@@ -0,0 +1,82 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/conditioner'
4
+ require 'rubygems'
5
+ require 'active_record'
6
+
7
+ ActiveRecord::Base.logger = nil
8
+ class TestUtils
9
+ class<< self
10
+ def config
11
+ {
12
+ :adapter=>'postgresql',
13
+ :host=> 'localhost',
14
+ :database=>'unittest_conditioner',
15
+ :username=>'postgres',
16
+ :password=>'postgres',
17
+ :encoding=>'utf8'
18
+ }
19
+ end
20
+
21
+ #create test database
22
+ def ensure_test_database
23
+ connect_to_test_db
24
+ rescue
25
+ create_database
26
+ end
27
+
28
+ def create_database
29
+ connect_to_postgres_db
30
+ ActiveRecord::Base.connection.create_database(config[:database], config)
31
+ connect_to_test_db
32
+ rescue
33
+ $stderr.puts $!, *($!.backtrace)
34
+ $stderr.puts "Couldn't create database for #{config.inspect}"
35
+ end
36
+
37
+ def connect_to_test_db
38
+ ActiveRecord::Base.establish_connection(config)
39
+ ActiveRecord::Base.connection
40
+ end
41
+
42
+ def connect_to_postgres_db
43
+ ActiveRecord::Base.establish_connection(config.merge(:database => 'postgres', :schema_search_path => 'public'))
44
+ end
45
+
46
+ def drop_database
47
+ connect_to_postgres_db
48
+ ActiveRecord::Base.connection.drop_database config[:database]
49
+ end
50
+ end
51
+ end
52
+
53
+ class CreateUsers < ActiveRecord::Migration
54
+ def self.up
55
+ create_table :users do |t|
56
+ t.string :last_name, :limit => 25
57
+ t.string :first_name, :limit => 25
58
+ t.string :middle_name, :limit => 25
59
+ t.string :name, :limit => 25
60
+ t.string :login, :limit => 40
61
+ t.string :email, :limit => 100
62
+ t.string :crypted_password, :limit => 40
63
+ t.string :salt, :limit => 40
64
+ t.datetime :last_login_datetime
65
+ t.datetime :deleted_at
66
+ t.timestamps
67
+ end
68
+ end
69
+
70
+ def self.down
71
+ drop_table :users
72
+ end
73
+ end
74
+
75
+ Conditioner.enable
76
+
77
+ class User< ActiveRecord::Base
78
+ end
79
+
80
+ TestUtils.ensure_test_database
81
+ CreateUsers.migrate(:up) unless ActiveRecord::Base.connection.table_exists?('users')
82
+ #CreateUsers.migrate :down
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: conditioner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - niquola
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-03-03 00:00:00 +03:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ type: :runtime
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
58
+ executables: []
59
+
60
+ extensions: []
61
+
62
+ extra_rdoc_files:
63
+ - History.txt
64
+ - Manifest.txt
65
+ - PostInstall.txt
66
+ files:
67
+ - History.txt
68
+ - Manifest.txt
69
+ - PostInstall.txt
70
+ - README.rdoc
71
+ - Rakefile
72
+ - lib/conditioner.rb
73
+ - lib/conditioner/active_record_mixin.rb
74
+ - lib/conditioner/condition.rb
75
+ - script/console
76
+ - script/destroy
77
+ - script/generate
78
+ - test/test_conditioner.rb
79
+ - test/test_helper.rb
80
+ has_rdoc: true
81
+ homepage: http://github.com/niquola/conditioner
82
+ licenses: []
83
+
84
+ post_install_message: PostInstall.txt
85
+ rdoc_options:
86
+ - --main
87
+ - README.rdoc
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ version:
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ version:
102
+ requirements: []
103
+
104
+ rubyforge_project: conditioner
105
+ rubygems_version: 1.3.5
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Simple conditions builder for active_resource
109
+ test_files:
110
+ - test/test_conditioner.rb
111
+ - test/test_helper.rb