wvanbergen-scoped_search 0.7.2 → 0.7.3

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,70 +1,6 @@
1
1
  require 'rubygems'
2
2
 
3
- load 'test/tasks.rake'
3
+ Dir['tasks/*.rake'].each { |file| load(file) }
4
4
 
5
5
  desc 'Default: run unit tests.'
6
- task :default => :test
7
-
8
- namespace :gem do
9
-
10
- desc "Sets the version and date of the scoped_search gem. Requires the VERSION environment variable."
11
- task :version => [:manifest] do
12
-
13
- require 'date'
14
-
15
- new_version = ENV['VERSION']
16
- raise "VERSION is required" unless /\d+(\.\d+)*/ =~ new_version
17
-
18
- spec_file = Dir['*.gemspec'].first
19
-
20
- spec = File.read(spec_file)
21
- spec.gsub!(/^(\s*s\.version\s*=\s*)('|")(.+)('|")(\s*)$/) { "#{$1}'#{new_version}'#{$5}" }
22
- spec.gsub!(/^(\s*s\.date\s*=\s*)('|")(.+)('|")(\s*)$/) { "#{$1}'#{Date.today.strftime('%Y-%m-%d')}'#{$5}" }
23
- File.open(spec_file, 'w') { |f| f << spec }
24
- end
25
-
26
- task :tag => [:version] do
27
-
28
- new_version = ENV['VERSION']
29
- raise "VERSION is required" unless /\d+(\.\d+)*/ =~ new_version
30
-
31
- sh "git add scoped_search.gemspec .manifest"
32
- sh "git commit -m \"Set gem version to #{new_version}\""
33
- sh "git push origin"
34
- sh "git tag -a \"scoped_search-#{new_version}\" -m \"Tagged version #{new_version}\""
35
- sh "git push --tags"
36
- end
37
-
38
- desc "Builds a ruby gem for scoped_search"
39
- task :build => [:manifest] do
40
- system %[gem build scoped_search.gemspec]
41
- end
42
-
43
- desc %{Update ".manifest" with the latest list of project filenames. Respect\
44
- .gitignore by excluding everything that git ignores. Update `files` and\
45
- `test_files` arrays in "*.gemspec" file if it's present.}
46
- task :manifest do
47
- list = Dir['**/*'].sort
48
- spec_file = Dir['*.gemspec'].first
49
- list -= [spec_file] if spec_file
50
-
51
- File.read('.gitignore').each_line do |glob|
52
- glob = glob.chomp.sub(/^\//, '')
53
- list -= Dir[glob]
54
- list -= Dir["#{glob}/**/*"] if File.directory?(glob) and !File.symlink?(glob)
55
- puts "excluding #{glob}"
56
- end
57
-
58
- if spec_file
59
- spec = File.read spec_file
60
- spec.gsub! /^(\s* s.(test_)?files \s* = \s* )( \[ [^\]]* \] | %w\( [^)]* \) )/mx do
61
- assignment = $1
62
- bunch = $2 ? list.grep(/^test.*_test\.rb$/) : list
63
- '%s%%w(%s)' % [assignment, bunch.join(' ')]
64
- end
65
-
66
- File.open(spec_file, 'w') {|f| f << spec }
67
- end
68
- File.open('.manifest', 'w') {|f| f << list.join("\n") }
69
- end
70
- end
6
+ task :default => :test
data/lib/scoped_search.rb CHANGED
@@ -9,36 +9,39 @@ module ScopedSearch
9
9
  end
10
10
 
11
11
  # Creates a named scope in the class it was called upon
12
- def searchable_on(*fields)
13
- if fields.first.class.to_s == 'Hash'
14
- if fields.first.has_key?(:only)
15
- fields = fields.first[:only]
16
- elsif fields.first.has_key?(:except)
17
- fields = self.column_names.collect { |column|
12
+ def searchable_on(*fields)
13
+ # Make sure that the table to be searched actually exists
14
+ if self.table_exists?
15
+ if fields.first.class.to_s == 'Hash'
16
+ if fields.first.has_key?(:only)
17
+ fields = fields.first[:only]
18
+ elsif fields.first.has_key?(:except)
19
+ fields = self.column_names.collect { |column|
18
20
  fields.first[:except].include?(column.to_sym) ? nil : column.to_sym }.compact
21
+ end
19
22
  end
20
- end
21
-
22
- assoc_models = self.reflections.collect { |m| m[0] }
23
- assoc_fields = fields - self.column_names.collect { |column| column.to_sym }
24
- fields -= assoc_fields
25
-
26
- assoc_groupings = {}
27
- assoc_models.each do |assoc_model|
28
- assoc_groupings[assoc_model] = []
29
- assoc_fields.each do |assoc_field|
30
- unless assoc_field.to_s.match(/^#{assoc_model.to_s}_/).nil?
31
- assoc_groupings[assoc_model] << assoc_field.to_s.sub(/^#{assoc_model.to_s}_/, '').to_sym
23
+
24
+ assoc_models = self.reflections.collect { |m| m[0] }
25
+ assoc_fields = fields - self.column_names.collect { |column| column.to_sym }
26
+ fields -= assoc_fields
27
+
28
+ assoc_groupings = {}
29
+ assoc_models.each do |assoc_model|
30
+ assoc_groupings[assoc_model] = []
31
+ assoc_fields.each do |assoc_field|
32
+ unless assoc_field.to_s.match(/^#{assoc_model.to_s}_/).nil?
33
+ assoc_groupings[assoc_model] << assoc_field.to_s.sub(/^#{assoc_model.to_s}_/, '').to_sym
34
+ end
32
35
  end
33
36
  end
37
+
38
+ assoc_groupings = assoc_groupings.delete_if {|group, field_group| field_group.empty?}
39
+
40
+ self.cattr_accessor :scoped_search_fields, :scoped_search_assoc_groupings
41
+ self.scoped_search_fields = fields
42
+ self.scoped_search_assoc_groupings = assoc_groupings
43
+ self.named_scope :search_for, lambda { |keywords| self.build_scoped_search_conditions(keywords) }
34
44
  end
35
-
36
- assoc_groupings = assoc_groupings.delete_if {|group, field_group| field_group.empty?}
37
-
38
- self.cattr_accessor :scoped_search_fields, :scoped_search_assoc_groupings
39
- self.scoped_search_fields = fields
40
- self.scoped_search_assoc_groupings = assoc_groupings
41
- self.named_scope :search_for, lambda { |keywords| self.build_scoped_search_conditions(keywords) }
42
45
  end
43
46
 
44
47
  # Build a hash that is used for the named_scope search_for.
@@ -0,0 +1,177 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/tasklib'
4
+ require 'date'
5
+
6
+ module Rake
7
+
8
+ class GithubGem < TaskLib
9
+
10
+ attr_accessor :name
11
+ attr_accessor :specification
12
+
13
+ def self.define_tasks!
14
+ gem_task_builder = Rake::GithubGem.new
15
+ gem_task_builder.register_all_tasks!
16
+ end
17
+
18
+
19
+ def initialize
20
+ reload_gemspec!
21
+ end
22
+
23
+ def register_all_tasks!
24
+ namespace(:gem) do
25
+ desc "Updates the file lists for this gem"
26
+ task(:manifest) { manifest_task }
27
+
28
+ desc "Builds a ruby gem for #{@name}"
29
+ task(:build => [:manifest]) { build_task }
30
+
31
+ desc "Installs the ruby gem for #{@name} locally"
32
+ task(:install => [:build]) { install_task }
33
+
34
+ desc "Uninstalls the ruby gem for #{@name} locally"
35
+ task(:uninstall) { uninstall_task }
36
+
37
+ desc "Releases a new version of #{@name}"
38
+ task(:release) { release_task }
39
+ end
40
+ end
41
+
42
+
43
+
44
+ protected
45
+
46
+ def reload_gemspec!
47
+ raise "No gemspec file found!" if gemspec_file.nil?
48
+ spec = File.read(gemspec_file)
49
+ @specification = eval(spec)
50
+ @name = specification.name
51
+ end
52
+
53
+ def run_command(command)
54
+ lines = []
55
+ IO.popen(command) { |f| lines = f.readlines }
56
+ return lines
57
+ end
58
+
59
+ def git_modified?(file)
60
+ return !run_command('git status').detect { |line| Regexp.new(Regexp.quote(file)) =~ line }.nil?
61
+ end
62
+
63
+ def git_commit_file(file, message, branch = nil)
64
+ verify_current_branch(branch) unless branch.nil?
65
+ if git_modified?(file)
66
+ sh "git add #{file}"
67
+ sh "git commit -m \"#{message}\""
68
+ else
69
+ raise "#{file} is not modified and cannot be committed!"
70
+ end
71
+ end
72
+
73
+ def git_create_tag(tag_name, message)
74
+ sh "git tag -a \"#{tag_name}\" -m \"#{message}\""
75
+ end
76
+
77
+ def git_push(remote = 'origin', branch = 'master', options = [])
78
+ verify_clean_status(branch)
79
+ options_str = options.map { |o| "--#{o}"}.join(' ')
80
+ sh "git push #{options_str} #{remote} #{branch}"
81
+ end
82
+
83
+ def gemspec_version=(new_version)
84
+ spec = File.read(gemspec_file)
85
+ spec.gsub!(/^(\s*s\.version\s*=\s*)('|")(.+)('|")(\s*)$/) { "#{$1}'#{new_version}'#{$5}" }
86
+ spec.gsub!(/^(\s*s\.date\s*=\s*)('|")(.+)('|")(\s*)$/) { "#{$1}'#{Date.today.strftime('%Y-%m-%d')}'#{$5}" }
87
+ File.open(gemspec_file, 'w') { |f| f << spec }
88
+ reload_gemspec!
89
+ end
90
+
91
+ def gemspec_date=(new_date)
92
+ spec = File.read(gemspec_file)
93
+ spec.gsub!(/^(\s*s\.date\s*=\s*)('|")(.+)('|")(\s*)$/) { "#{$1}'#{new_date.strftime('%Y-%m-%d')}'#{$5}" }
94
+ File.open(gemspec_file, 'w') { |f| f << spec }
95
+ reload_gemspec!
96
+ end
97
+
98
+ def gemspec_file
99
+ @gemspec_file ||= Dir['*.gemspec'].first
100
+ end
101
+
102
+ def verify_current_branch(branch)
103
+ run_command('git branch').detect { |line| /^\* (.+)/ =~ line }
104
+ raise "You are currently not working in the master branch!" unless branch == $1
105
+ end
106
+
107
+ def verify_clean_status(on_branch = nil)
108
+ sh "git fetch"
109
+ lines = run_command('git status')
110
+ raise "You don't have the most recent version available. Run git pull first." if /^\# Your branch is behind/ =~ lines[1]
111
+ raise "You are currently not working in the #{on_branch} branch!" unless on_branch.nil? || (/^\# On branch (.+)/ =~ lines.first && $1 == on_branch)
112
+ raise "Your master branch contains modifications!" unless /^nothing to commit \(working directory clean\)/ =~ lines.last
113
+ end
114
+
115
+ def verify_version(new_version)
116
+ newest_version = run_command('git tag').map { |tag| tag.split(name + '-').last }.compact.map { |v| Gem::Version.new(v) }.max
117
+ raise "This version number (#{new_version}) is not higher than the highest tagged version (#{newest_version})" if !newest_version.nil? && newest_version >= Gem::Version.new(new_version.to_s)
118
+ end
119
+
120
+ def manifest_task
121
+ verify_current_branch('master')
122
+
123
+ list = Dir['**/*'].sort
124
+ list -= [gemspec_file]
125
+
126
+ if File.exist?('.gitignore')
127
+ File.read('.gitignore').each_line do |glob|
128
+ glob = glob.chomp.sub(/^\//, '')
129
+ list -= Dir[glob]
130
+ list -= Dir["#{glob}/**/*"] if File.directory?(glob) and !File.symlink?(glob)
131
+ end
132
+ end
133
+
134
+ # update the spec file
135
+ spec = File.read(gemspec_file)
136
+ spec.gsub! /^(\s* s.(test_)?files \s* = \s* )( \[ [^\]]* \] | %w\( [^)]* \) )/mx do
137
+ assignment = $1
138
+ bunch = $2 ? list.grep(/^test.*_test\.rb$/) : list
139
+ '%s%%w(%s)' % [assignment, bunch.join(' ')]
140
+ end
141
+
142
+ File.open(gemspec_file, 'w') { |f| f << spec }
143
+ reload_gemspec!
144
+ end
145
+
146
+ def build_task
147
+ sh "gem build #{gemspec_file}"
148
+ end
149
+
150
+ def install_task
151
+ raise "#{name} .gem file not found" unless File.exist?("#{name}-#{specification.version}.gem")
152
+ sh "gem install #{name}-#{specification.version}.gem"
153
+ end
154
+
155
+ def uninstall_task
156
+ raise "#{name} .gem file not found" unless File.exist?("#{name}-#{specification.version}.gem")
157
+ sh "gem uninstall #{name}"
158
+ end
159
+
160
+ def release_task
161
+ verify_clean_status('master')
162
+ verify_version(ENV['VERSION'] || @specification.version)
163
+
164
+ # update gemspec file
165
+ self.gemspec_version = ENV['VERSION'] if Gem::Version.correct?(ENV['VERSION'])
166
+ self.gemspec_date = Date.today
167
+ manifest_task
168
+ git_commit_file(gemspec_file, "Updated #{gemspec_file} for release of version #{@specification.version}") if git_modified?(gemspec_file)
169
+
170
+ # create tag and push changes
171
+ git_create_tag("#{@name}-#{@specification.version}", "Tagged version #{@specification.version}")
172
+ git_push('origin', 'master', [:tags])
173
+ end
174
+ end
175
+ end
176
+
177
+ Rake::GithubGem.define_tasks!
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wvanbergen-scoped_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Willem van Bergen
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2008-10-24 00:00:00 -07:00
13
+ date: 2008-11-24 00:00:00 -08:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -37,11 +37,13 @@ files:
37
37
  - lib/scoped_search/query_conditions_builder.rb
38
38
  - lib/scoped_search/query_language_parser.rb
39
39
  - lib/scoped_search/reg_tokens.rb
40
+ - tasks
41
+ - tasks/github-gem.rake
42
+ - tasks/test.rake
40
43
  - test
41
44
  - test/query_conditions_builder_test.rb
42
45
  - test/query_language_test.rb
43
46
  - test/search_for_test.rb
44
- - test/tasks.rake
45
47
  - test/test_helper.rb
46
48
  has_rdoc: false
47
49
  homepage: http://github.com/wvanbergen/scoped_search/wikis
File without changes