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 +1 -1
- data/lib/set_notation_helper.rb +73 -81
- data/rails/init.rb +8 -3
- metadata +1 -1
data/README.markdown
CHANGED
data/lib/set_notation_helper.rb
CHANGED
@@ -1,96 +1,88 @@
|
|
1
1
|
module SetNotationHelper
|
2
2
|
|
3
3
|
def self.included(base)
|
4
|
-
|
5
|
-
|
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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
-
|
53
|
-
|
54
|
-
|
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
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
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
|
-
|
65
|
-
|
66
|
-
|
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
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
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
|
-
|
79
|
-
|
80
|
-
|
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
|
-
|
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
|
3
|
+
ActiveRecord::Base.send(:include, SetNotationHelper)
|
4
4
|
|
5
|
-
RAILS_DEFAULT_LOGGER.debug
|
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
|