mind_reader 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Rodrigo Manhães
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,106 @@
1
+ = MindReader: Easy searching for ActiveRecord applications
2
+
3
+ Given the following objects:
4
+
5
+ @robin = Customer.create! :name => 'Damian Wayne',
6
+ :address => 'Wayne Manor, Gotham City',
7
+ :age => 14
8
+ @batman = Customer.create! :name => 'Dick Grayson',
9
+ :address => 'Wayne Manor, Gotham City',
10
+ :sidekick_id => @robin.id,
11
+ :age => 26
12
+ @superman = Customer.create! :name => 'Kal-El',
13
+ :address => 'Kandor, New Krypton',
14
+ :age => 36
15
+
16
+
17
+ Find by one field:
18
+
19
+ reader = MindReader.new(Customer)
20
+ result = reader.execute(:address => 'Wayne Manor, Gotham City')
21
+ result.should have(2).bat_heroes
22
+ result.should include(@batman, @robin)
23
+
24
+
25
+ Find by multiple fields:
26
+
27
+ reader = MindReader.new(Customer)
28
+ reader.execute(:address => 'Wayne Manor, Gotham City',
29
+ :name => 'Dick Grayson').should == [@batman]
30
+
31
+
32
+ String fields are evaluated partially:
33
+
34
+ result = reader.execute(:name => 'ay')
35
+ result.should have(2).super_heroes
36
+ result.should include(@batman, @robin)
37
+
38
+
39
+ Find with lookup:
40
+
41
+ reader = MindReader.new(Customer) do |r|
42
+ r.sidekick_id(:lookup => :sidekick_name) {|name| Customer.find_by_name(name).id }
43
+ end
44
+ reader.execute(:sidekick_name => 'Damian Wayne').should == [@batman]
45
+
46
+
47
+ Find given a range:
48
+
49
+ reader = MindReader.new(Customer) do |r|
50
+ r.age :range => :start_age..:end_age
51
+ end
52
+ reader.execute('start_age' => 25, 'end_age' => 36).should == [@batman, @superman]
53
+
54
+
55
+ Blank fields are ignored:
56
+
57
+ reader = MindReader.new(Customer)
58
+ reader.execute(:name => 'Kal-El', :address => '').should == [@superman]
59
+
60
+
61
+ If all fields are blank, returns nil:
62
+
63
+ reader = MindReader.new(Customer)
64
+ reader.execute(:name => '', :address => '').should be_nil
65
+
66
+
67
+ unless you say you want all objects:
68
+
69
+ reader = MindReader.new(Customer)
70
+ reader.retrieve_all_when_no_value_is_given = true
71
+ result = reader.execute(:name => '', :address => '')
72
+ result.should have(3).super_heroes
73
+ result.should include(@batman, @robin, @superman)
74
+
75
+
76
+ mind_reader supports converters for incoming data. For sake of example, imagine
77
+ you have to enter a date in a different format (here, dd/mm/yyyy), and you want
78
+ to convert it to default yyyy-mm-dd::
79
+
80
+ def convert_to_default(br_date)
81
+ br_date =~ /(\d+)\/(\d+)\/(\d+)/
82
+ "%s-%s-%s" % [$3, $2, $1]
83
+ end
84
+
85
+ reader = MindReader.new(SuperHero) do |r|
86
+ r.date_of_heroic_birth :range => :from_date..:to_date, :converter => lambda {|d| convert_to_default(d) }
87
+ end
88
+ reader.execute('from_date' => '01/04/2010', 'to_date' => '30/04/2010').should == [@batman]
89
+
90
+
91
+ == How to install
92
+
93
+
94
+ $ gem install mind_reader
95
+
96
+
97
+ == How to run specs
98
+
99
+
100
+ $ rake spec
101
+
102
+
103
+ == Copyright
104
+
105
+ Copyright (c) 2011 Rodrigo Manhães. See LICENSE.txt for further details.
106
+
data/lib/mind_reader.rb CHANGED
@@ -29,6 +29,7 @@ class MindReader
29
29
 
30
30
  def process
31
31
  remove_blanks
32
+ run_converters
32
33
  handle_lookup
33
34
  handle_range
34
35
  handle_partials
@@ -38,6 +39,25 @@ class MindReader
38
39
  @pairs.reject! {|k, v| v.blank?}
39
40
  end
40
41
 
42
+ def run_converters
43
+ configs.each do |c|
44
+ if c[:args].has_key?(:converter)
45
+ if c[:args].has_key?(:range)
46
+ apply_converter(c, c[:args][:range].begin.to_s)
47
+ apply_converter(c, c[:args][:range].end.to_s)
48
+ else
49
+ apply_converter(c, c[:field])
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ def apply_converter(config, field)
56
+ callable = config[:args][:converter]
57
+ value = @pairs[field]
58
+ @pairs[field] = callable.call(value)
59
+ end
60
+
41
61
  def handle_partials
42
62
  string_keys = @pairs.map {|field, value|
43
63
  field if string_field?(field) && value.present?
@@ -63,13 +83,15 @@ class MindReader
63
83
  range = c[:args][:range]
64
84
  start_field, end_field = range.begin.to_s, range.end.to_s
65
85
  start_value, end_value = @pairs.delete(start_field), @pairs.delete(end_field)
66
- @pairs.merge! c[:field] => start_value..end_value
86
+ if start_value.present? && end_value.present?
87
+ @pairs.merge! c[:field] => start_value..end_value
88
+ end
67
89
  end
68
90
  end
69
91
  end
70
92
 
71
93
  def string_field?(field)
72
- @klass.columns_hash[field.to_s].try(:type) == :string
94
+ [:string, :text].include? @klass.columns_hash[field.to_s].try(:type)
73
95
  end
74
96
 
75
97
  class ConfigurableObject
metadata CHANGED
@@ -1,80 +1,105 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: mind_reader
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 1
9
- version: 0.0.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
12
- - "Rodrigo Manh\xC3\xA3es"
7
+ authors:
8
+ - Rodrigo Manhães
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2010-07-08 00:00:00 -03:00
12
+ date: 2011-06-01 00:00:00.000000000 -03:00
18
13
  default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
21
16
  name: activerecord
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ requirement: &13369940 !ruby/object:Gem::Requirement
24
18
  none: false
25
- requirements:
19
+ requirements:
26
20
  - - ~>
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 3
30
- - 0
31
- - 0
21
+ - !ruby/object:Gem::Version
32
22
  version: 3.0.0
33
23
  type: :runtime
34
- version_requirements: *id001
24
+ prerelease: false
25
+ version_requirements: *13369940
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &13369400 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *13369400
37
+ - !ruby/object:Gem::Dependency
38
+ name: steak
39
+ requirement: &13368940 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.0.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *13368940
48
+ - !ruby/object:Gem::Dependency
49
+ name: sqlite3-ruby
50
+ requirement: &13367240 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 1.3.0
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *13367240
59
+ - !ruby/object:Gem::Dependency
60
+ name: database_cleaner
61
+ requirement: &13366760 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: 0.6.0
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *13366760
35
70
  description: Easy searching for ActiveRecord applications
36
- email:
37
- - rmanhaes@gmail.com
71
+ email: rmanhaes@gmail.com
38
72
  executables: []
39
-
40
73
  extensions: []
41
-
42
74
  extra_rdoc_files: []
43
-
44
- files:
45
- - mind_reader.gemspec
75
+ files:
46
76
  - lib/mind_reader.rb
77
+ - README.rdoc
78
+ - LICENSE.txt
47
79
  has_rdoc: true
48
80
  homepage: http://github.com/rodrigomanhaes/mind_reader
49
81
  licenses: []
50
-
51
82
  post_install_message:
52
- rdoc_options: []
53
-
54
- require_paths:
83
+ rdoc_options:
84
+ - --charset=UTF-8
85
+ require_paths:
55
86
  - lib
56
- required_ruby_version: !ruby/object:Gem::Requirement
87
+ required_ruby_version: !ruby/object:Gem::Requirement
57
88
  none: false
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- segments:
62
- - 0
63
- version: "0"
64
- required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
94
  none: false
66
- requirements:
67
- - - ">="
68
- - !ruby/object:Gem::Version
69
- segments:
70
- - 0
71
- version: "0"
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
72
99
  requirements: []
73
-
74
- rubyforge_project: mind_reader
75
- rubygems_version: 1.3.7
100
+ rubyforge_project:
101
+ rubygems_version: 1.6.2
76
102
  signing_key:
77
103
  specification_version: 3
78
104
  summary: Easy searching for ActiveRecord applications
79
105
  test_files: []
80
-
data/mind_reader.gemspec DELETED
@@ -1,20 +0,0 @@
1
- # encoding: utf-8
2
-
3
- Gem::Specification.new do |s|
4
- s.name = %q{mind_reader}
5
- s.version = '0.0.1'
6
-
7
- s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
8
- s.authors = ['Rodrigo Manhães']
9
- s.date = %{2010-07-08}
10
- s.description = %{Easy searching for ActiveRecord applications}
11
- s.email = ['rmanhaes@gmail.com']
12
- s.files = ['mind_reader.gemspec', 'lib/mind_reader.rb']
13
- s.require_paths = ['lib']
14
- s.rubyforge_project = %q{mind_reader}
15
- s.rubygems_version = %q{1.3.7}
16
- s.summary = %q{Easy searching for ActiveRecord applications}
17
- s.add_dependency 'activerecord', '~>3.0.0'
18
- s.homepage = 'http://github.com/rodrigomanhaes/mind_reader'
19
- end
20
-