dmichael-set-notation-helper 0.5 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -21,7 +21,7 @@ Installation
21
21
 
22
22
  Add the gem to your environment.rb file
23
23
 
24
- config.gem "set-notation-helper", :lib => 'set_notation_helper'
24
+ config.gem "dmichael-set-notation-helper", :lib => 'set_notation_helper'
25
25
 
26
26
 
27
27
  Matching for sets in your routes
@@ -1,96 +1,88 @@
1
1
  module SetNotationHelper
2
2
 
3
3
  def self.included(base)
4
- base.extend(ClassMethods)
5
- end
4
+ # These methods are made available to both the class and instances
5
+ base.extend(SetNotationHelper)
6
+ end
7
+
8
+ def inherited(subclass)
9
+ # base.send :include, ClassMethods
10
+ subclass.class_eval do
11
+ # This scope accepts either a String in the form "{1,2,3}" **OR** an Integer
12
+ named_scope :in_set, lambda { |*args|
13
+ value = args.first
6
14
 
15
+ # A string input is either a set or an integer in disguise
16
+ unless value.is_a?(String) || value.is_a?(Integer)
17
+ logger.error "[SetNotationHelper] You have given the helper something it cant use: #{value.class}. No filter applied!"
18
+ return {}
19
+ end
7
20
 
8
- module ClassMethods
9
-
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
-
31
- end
32
- end
33
-
34
- def set_limit
35
- 100000
36
- end
37
-
38
- def is_set_or_range?(input)
39
- if is_set?(input) or is_range?(input)
40
- return true
41
- end
42
- return false
21
+ if is_set?(value)
22
+ { :conditions => "#{table_name}.id IN (#{ parse_set(value).to_a.join(',') })" }
23
+ else
24
+ { :conditions => "#{table_name}.id = #{ value.to_i }" }
25
+ end
26
+ }
43
27
  end
44
-
45
- def is_range?(range)
46
- if range && range.is_a?(String) && range.split('-').size > 1 && !match_set(range)
47
- return true
48
- end
49
- return false
28
+ end
29
+
30
+ def set_limit
31
+ 100000
32
+ end
33
+
34
+ def is_set_or_range?(input)
35
+ if is_set?(input) or is_range?(input)
36
+ return true
50
37
  end
51
-
52
- def match_set(set)
53
- return if set.blank? || !set.is_a?(String)
54
- set.match( /\{\s*(.*)\s*\}/ )
38
+ return false
39
+ end
40
+
41
+ def is_range?(range)
42
+ if range && range.is_a?(String) && range.split('-').size > 1 && !match_set(range)
43
+ return true
55
44
  end
56
-
57
- def is_set?(set)
58
- if match_set(set)
59
- return true
60
- end
61
- return false
45
+ return false
46
+ end
47
+
48
+ def match_set(set)
49
+ return if set.blank? || !set.is_a?(String)
50
+ set.match( /\{\s*(.*)\s*\}/ )
51
+ end
52
+
53
+ def is_set?(set)
54
+ if match_set(set)
55
+ return true
62
56
  end
63
-
64
- def parse_set(set)
65
- match = match_set(set)
66
- if !match then return Set.new(set) end
57
+ return false
58
+ end
59
+
60
+ def parse_set(set)
61
+ match = match_set(set)
62
+ if !match then return Set.new(set) end
67
63
 
68
- set = match[1].split(',')
69
- set.each_with_index{|element, i|
70
- if is_range?(element)
71
- set[i] = element.to_range.collect
72
- set.flatten!
73
- end
74
- }
75
- if set.size > set_limit
76
- @messages[:info] = "Your resultset has been truncated to #{set_limit} results"
64
+ set = match[1].split(',')
65
+ set.each_with_index{|element, i|
66
+ if is_range?(element)
67
+ set[i] = element.to_range.collect
68
+ set.flatten!
77
69
  end
78
- set = Set.new(set[0..set_limit-1])
79
- return set
80
- end
81
-
82
-
83
- def convert_range_to_limits(input)
84
- if !is_range?(input) then return false end
85
-
86
- # turn this String into a Range
87
- range = input.to_range
88
- limit = range.collect.size
89
- offset = range.first - 1
90
-
91
- return limit, offset
70
+ }
71
+ if set.size > set_limit
72
+ @messages[:info] = "Your resultset has been truncated to #{set_limit} results"
92
73
  end
74
+ set = Set.new(set[0..set_limit-1])
75
+ return set
76
+ end
77
+
78
+ def convert_range_to_limits(input)
79
+ if !is_range?(input) then return false end
93
80
 
94
- end # ClassMethods
81
+ # turn this String into a Range
82
+ range = input.to_range
83
+ limit = range.collect.size
84
+ offset = range.first - 1
95
85
 
86
+ return limit, offset
87
+ end
96
88
  end
data/rails/init.rb CHANGED
@@ -1,5 +1,10 @@
1
- require 'set_notation_helper'
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'set_notation_helper')
2
2
 
3
- ActiveRecord::Base.send :include, SetNotationHelper
3
+ ActiveRecord::Base.send(:include, SetNotationHelper)
4
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}"
5
+ RAILS_DEFAULT_LOGGER.debug <<-STR
6
+ ** [SetNotationHelper] loaded. \n
7
+ Let's say that our universe contains the numbers 1, 2, 3, and 4. \n
8
+ Let A be the set containing the numbers 1 and 2; \n
9
+ that is, A = {1, 2}
10
+ STR
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.5"
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Michael