dmichael-set-notation-helper 0.3 → 0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +7 -1
- data/lib/set_notation_helper.rb +26 -19
- metadata +1 -1
data/README.markdown
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
SetNotationHelper
|
2
2
|
=================
|
3
3
|
|
4
|
-
A library providing set notation support
|
4
|
+
A library providing set notation support via a named_scope for ActiveRecord models.
|
5
|
+
|
6
|
+
Have you ever wanted to manipulate a set of resources from the database from a URL but were not sure how? Let me suggest using set notation for the ID.
|
7
|
+
|
8
|
+
GET /sounds/{1,2,3}
|
9
|
+
|
10
|
+
Install the gem and add it to your environment to add support for all your models.
|
5
11
|
|
6
12
|
To use:
|
7
13
|
|
data/lib/set_notation_helper.rb
CHANGED
@@ -2,32 +2,39 @@ module SetNotationHelper
|
|
2
2
|
|
3
3
|
def self.included(base)
|
4
4
|
base.extend(ClassMethods)
|
5
|
-
# base.send :include, ClassMethods
|
6
5
|
end
|
7
6
|
|
8
|
-
def set_limit
|
9
|
-
100000
|
10
|
-
end
|
11
7
|
|
12
8
|
module ClassMethods
|
13
9
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
10
|
+
def inherited(subclass)
|
11
|
+
# base.send :include, ClassMethods
|
12
|
+
subclass.class_eval do
|
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
|
+
}
|
30
|
+
|
28
31
|
end
|
29
|
-
|
32
|
+
end
|
30
33
|
|
34
|
+
def set_limit
|
35
|
+
100000
|
36
|
+
end
|
37
|
+
|
31
38
|
def is_set_or_range?(input)
|
32
39
|
if is_set?(input) or is_range?(input)
|
33
40
|
return true
|