Module: SetNotationHelper

Defined in:
lib/set_notation_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
# File 'lib/set_notation_helper.rb', line 3

def self.included(base)
  # These methods are made available to both the class and instances
  base.extend(SetNotationHelper)
end

Instance Method Details

#convert_range_to_limits(input) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/set_notation_helper.rb', line 78

def convert_range_to_limits(input)        
  if !is_range?(input) then return false end

  # turn this String into a Range
  range  = input.to_range
  limit  = range.collect.size
  offset = range.first - 1

  return limit, offset
end

#inherited(subclass) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/set_notation_helper.rb', line 8

def inherited(subclass)
  # base.send :include, ClassMethods
  subclass.class_eval do
    # This scope accepts either a String in the form "{1,2,3}" **OR** an Integer
    named_scope :in_set, lambda { |*args|
      value = args.first

      # A string input is either a set or an integer in disguise
      unless value.is_a?(String) || value.is_a?(Integer)
        logger.error "[SetNotationHelper] You have given the helper something it cant use: #{value.class}. No filter applied!"
        return {} 
      end

      if is_set?(value)
        { :conditions => "#{table_name}.id IN (#{ parse_set(value).to_a.join(',') })" }
      else
        { :conditions => "#{table_name}.id = #{ value.to_i }" }
      end
    }
  end
end

#is_range?(range) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
# File 'lib/set_notation_helper.rb', line 41

def is_range?(range)
  if range && range.is_a?(String) && range.split('-').size > 1 && !match_set(range)
    return true
  end
  return false
end

#is_set?(set) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
# File 'lib/set_notation_helper.rb', line 53

def is_set?(set)
  if match_set(set)
    return true
  end
  return false
end

#is_set_or_range?(input) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'lib/set_notation_helper.rb', line 34

def is_set_or_range?(input)
  if is_set?(input) or is_range?(input)
    return true
  end
  return false
end

#match_set(set) ⇒ Object



48
49
50
51
# File 'lib/set_notation_helper.rb', line 48

def match_set(set)
  return if set.blank? || !set.is_a?(String)
  set.match( /\{\s*(.*)\s*\}/ )
end

#parse_set(set) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/set_notation_helper.rb', line 60

def parse_set(set)
  match = match_set(set)
  if !match then return Set.new(set) end

  set = match[1].split(',')
  set.each_with_index{|element, i|
    if is_range?(element)
      set[i] = element.to_range.collect
      set.flatten!
    end
  }
  if set.size > set_limit
    @messages[:info] = "Your resultset has been truncated to #{set_limit} results"
  end
  set = Set.new(set[0..set_limit-1])
  return set
end

#set_limitObject



30
31
32
# File 'lib/set_notation_helper.rb', line 30

def set_limit
  100000
end