Module: Rearmed::Enumerable

Defined in:
lib/rearmed/methods.rb

Class Method Summary collapse

Class Method Details

.natural_sort(collection, options = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rearmed/methods.rb', line 104

def self.natural_sort(collection, options={})
  if !collection.is_a?(::Enumerable)
    raise TypeError.new("Invalid object passed to #{__method__}, must be an Enumerable")
  elsif block_given?
    Rearmed::Exceptions::BlockFoundError
  else
    collection.sort do |a,b| 
      if options[:reverse] == true
        self._naturalize_str(b.to_s) <=> self._naturalize_str(a.to_s)
      else
        self._naturalize_str(a.to_s) <=> self._naturalize_str(b.to_s)
      end
    end
  end
end

.natural_sort_by(collection) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/rearmed/methods.rb', line 96

def self.natural_sort_by(collection)
  if collection.is_a?(::Enumerable) 
    collection.sort_by{|x| self._naturalize_str(yield(x))}
  else
    raise TypeError.new("Invalid object passed to #{__method__}, must be an Enumerable")
  end
end

.select_map(collection) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rearmed/methods.rb', line 120

def self.select_map(collection)
  if collection.is_a?(::Enumerable)
    if block_given?
      collection.select{|x| yield(x) }.map{|x| yield(x) }
    else
      collection.select.map
    end
  else
    raise TypeError.new("Invalid object passed to #{__method__}, must be an Enumerable")
  end
end