dmichael-set-notation-helper 0.2 → 0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +3 -3
- data/lib/set_notation_helper.rb +19 -21
- metadata +1 -1
data/README.markdown
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
SetNotationHelper
|
2
2
|
=================
|
3
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.
|
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, or install the gem and add it to your environment to add support for all your models.
|
5
5
|
|
6
6
|
To use:
|
7
7
|
|
@@ -11,7 +11,7 @@ To use:
|
|
11
11
|
Installation
|
12
12
|
------------
|
13
13
|
|
14
|
-
sudo gem install set-notation-helper -s http://gems.github.com
|
14
|
+
sudo gem install dmichael-set-notation-helper -s http://gems.github.com
|
15
15
|
|
16
16
|
Add the gem to your environment.rb file
|
17
17
|
|
@@ -28,7 +28,7 @@ If you are interested in matching for sets in the routes you can do something li
|
|
28
28
|
/\{\s*([\d\-,\s*]*)\s*\}/
|
29
29
|
]
|
30
30
|
|
31
|
-
|
31
|
+
regexes.each do |regex|
|
32
32
|
map.with_options( :controller => 'sounds', :action => 'index', :id => regex, :conditions => { :method => :get }) do |r|
|
33
33
|
r.connect "/#{resource}/:id.:format"
|
34
34
|
r.connect "/#{resource}/:id"
|
data/lib/set_notation_helper.rb
CHANGED
@@ -1,27 +1,8 @@
|
|
1
1
|
module SetNotationHelper
|
2
2
|
|
3
3
|
def self.included(base)
|
4
|
-
base.
|
5
|
-
|
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
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
# base.send :include, ClassMethods
|
25
6
|
end
|
26
7
|
|
27
8
|
def set_limit
|
@@ -29,6 +10,23 @@ module SetNotationHelper
|
|
29
10
|
end
|
30
11
|
|
31
12
|
module ClassMethods
|
13
|
+
|
14
|
+
# This scope accepts either a String in the form "{1,2,3}" **OR** an Integer
|
15
|
+
named_scope :in_set, lambda { |*args|
|
16
|
+
value = args.first
|
17
|
+
|
18
|
+
# A string input is either a set or an integer in disguise
|
19
|
+
unless value.is_a?(String) || value.is_a?(Integer)
|
20
|
+
logger.error "[SetNotationHelper] You have given the helper something it cant use: #{value.class}. No filter applied!"
|
21
|
+
return {}
|
22
|
+
end
|
23
|
+
|
24
|
+
if is_set?(value)
|
25
|
+
{ :conditions => "#{table_name}.id IN (#{ parse_set(value).to_a.join(',') })" }
|
26
|
+
else
|
27
|
+
{ :conditions => "#{table_name}.id = #{ value.to_i }" }
|
28
|
+
end
|
29
|
+
}
|
32
30
|
|
33
31
|
def is_set_or_range?(input)
|
34
32
|
if is_set?(input) or is_range?(input)
|