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 CHANGED
@@ -1,7 +1,13 @@
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, or install the gem and add it to your environment to add support for all your models.
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
 
@@ -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
- # 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 }" }
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dmichael-set-notation-helper
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.3"
4
+ version: "0.5"
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Michael