dmichael-set-notation-helper 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE.txt +20 -0
- data/README.markdown +36 -0
- data/init.rb +1 -0
- data/install.rb +1 -0
- data/lib/set_notation_helper.rb +91 -0
- data/rails/init.rb +5 -0
- data/uninstall.rb +1 -0
- metadata +59 -0
data/MIT-LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Matt Darby
|
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.markdown
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
SetNotationHelper
|
2
|
+
=================
|
3
|
+
|
4
|
+
A library providing set notation support for ActiveRecord models. Include this module in the models you want to support set notation finders and you are all set.
|
5
|
+
|
6
|
+
To use:
|
7
|
+
|
8
|
+
sound = Sound.in_set("{1,2,3}")
|
9
|
+
|
10
|
+
|
11
|
+
Installation
|
12
|
+
------------
|
13
|
+
|
14
|
+
sudo gem install set-notation-helper -s http://gems.github.com
|
15
|
+
|
16
|
+
Add the gem to your environment.rb file
|
17
|
+
|
18
|
+
config.gem "set-notation-helper", :lib => 'set_notation_helper'
|
19
|
+
|
20
|
+
|
21
|
+
Matching for sets in your routes
|
22
|
+
---------------------------
|
23
|
+
|
24
|
+
If you are interested in matching for sets in the routes you can do something like this
|
25
|
+
|
26
|
+
regexes = [
|
27
|
+
/%7B\s*([\d\-,(%20)*]*)\s*%7D/,
|
28
|
+
/\{\s*([\d\-,\s*]*)\s*\}/
|
29
|
+
]
|
30
|
+
|
31
|
+
set_regexes.each do |regex|
|
32
|
+
map.with_options( :controller => 'sounds', :action => 'index', :id => regex, :conditions => { :method => :get }) do |r|
|
33
|
+
r.connect "/#{resource}/:id.:format"
|
34
|
+
r.connect "/#{resource}/:id"
|
35
|
+
end
|
36
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/rails/init"
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module SetNotationHelper
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.class_eval do
|
5
|
+
extend SetNotationHelper::ClassMethods
|
6
|
+
|
7
|
+
# This scope accepts either a String in the form "{1,2,3}" **OR** an Integer
|
8
|
+
named_scope :in_set, lambda { |*args|
|
9
|
+
value = args.first
|
10
|
+
|
11
|
+
# A string input is either a set or an integer in disguise
|
12
|
+
unless value.is_a?(String) || value.is_a?(Integer)
|
13
|
+
logger.error "[SetNotationHelper] You have given the helper something it cant use: #{value.class}. No filter applied!"
|
14
|
+
return {}
|
15
|
+
end
|
16
|
+
|
17
|
+
if is_set?(value)
|
18
|
+
{ :conditions => "#{table_name}.id IN (#{ parse_set(value).to_a.join(',') })" }
|
19
|
+
else
|
20
|
+
{ :conditions => "#{table_name}.id = #{ value.to_i }" }
|
21
|
+
end
|
22
|
+
}
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def set_limit
|
28
|
+
100000
|
29
|
+
end
|
30
|
+
|
31
|
+
module ClassMethods
|
32
|
+
|
33
|
+
def is_set_or_range?(input)
|
34
|
+
if is_set?(input) or is_range?(input)
|
35
|
+
return true
|
36
|
+
end
|
37
|
+
return false
|
38
|
+
end
|
39
|
+
|
40
|
+
def is_range?(range)
|
41
|
+
if range && range.is_a?(String) && range.split('-').size > 1 && !match_set(range)
|
42
|
+
return true
|
43
|
+
end
|
44
|
+
return false
|
45
|
+
end
|
46
|
+
|
47
|
+
def match_set(set)
|
48
|
+
return if set.blank? || !set.is_a?(String)
|
49
|
+
set.match( /\{\s*(.*)\s*\}/ )
|
50
|
+
end
|
51
|
+
|
52
|
+
def is_set?(set)
|
53
|
+
if match_set(set)
|
54
|
+
return true
|
55
|
+
end
|
56
|
+
return false
|
57
|
+
end
|
58
|
+
|
59
|
+
def parse_set(set)
|
60
|
+
match = match_set(set)
|
61
|
+
if !match then return Set.new(set) end
|
62
|
+
|
63
|
+
set = match[1].split(',')
|
64
|
+
set.each_with_index{|element, i|
|
65
|
+
if is_range?(element)
|
66
|
+
set[i] = element.to_range.collect
|
67
|
+
set.flatten!
|
68
|
+
end
|
69
|
+
}
|
70
|
+
if set.size > set_limit
|
71
|
+
@messages[:info] = "Your resultset has been truncated to #{set_limit} results"
|
72
|
+
end
|
73
|
+
set = Set.new(set[0..set_limit-1])
|
74
|
+
return set
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
def convert_range_to_limits(input)
|
79
|
+
if !is_range?(input) then return false end
|
80
|
+
|
81
|
+
# turn this String into a Range
|
82
|
+
range = input.to_range
|
83
|
+
limit = range.collect.size
|
84
|
+
offset = range.first - 1
|
85
|
+
|
86
|
+
return limit, offset
|
87
|
+
end
|
88
|
+
|
89
|
+
end # ClassMethods
|
90
|
+
|
91
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
require 'set_notation_helper'
|
2
|
+
|
3
|
+
ActiveRecord::Base.send :include, SetNotationHelper
|
4
|
+
|
5
|
+
RAILS_DEFAULT_LOGGER.debug "** [SetNotationHelper] loaded. Let's say that our universe contains the numbers 1, 2, 3, and 4. Let A be the set containing the numbers 1 and 2; that is, A = {1, 2}"
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dmichael-set-notation-helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.2"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Michael
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-26 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A library providing set notation support for ActiveRecord models
|
17
|
+
email: david.michael@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- MIT-LICENSE.txt
|
26
|
+
- README.markdown
|
27
|
+
- init.rb
|
28
|
+
- install.rb
|
29
|
+
- lib/set_notation_helper.rb
|
30
|
+
- rails/init.rb
|
31
|
+
- uninstall.rb
|
32
|
+
has_rdoc: false
|
33
|
+
homepage: http://github.com/dmichael/set-notation-helper
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.2.0
|
55
|
+
signing_key:
|
56
|
+
specification_version: 2
|
57
|
+
summary: Find by set notation in Active Record
|
58
|
+
test_files: []
|
59
|
+
|