Sha256: 546f31f93c7bee17bf8e432c96420510514d2f5aff9c42621714bf7b57cc430a

Contents?: true

Size: 1.61 KB

Versions: 5

Compression:

Stored size: 1.61 KB

Contents

#------------------------------------------------------------------------------
class Array

  # Extract options from a set of arguments. Removes and returns the last element in the array if it's a hash, otherwise returns a blank hash.
  #
  #   def options(*args)
  #     args.extract_options!
  #   end
  #
  #   options(1, 2)           # => {}
  #   options(1, 2, :a => :b) # => {:a=>:b}
  #------------------------------------------------------------------------------
  def extract_options!
    last.is_a?(::Hash) ? pop : {}
  end

  # Return the value that is closest to the value in the array, rounded down.
  # [0,5,7,8,11,16].closest_max(6) => 5
  # [0,5,7,8,11,16].closest_max(7) => 7
  #------------------------------------------------------------------------------
  def closest_max(value)
    self.select{|item| item <= value}.max
  end

  # able to join safe and unsafe strings
  #------------------------------------------------------------------------------
  def xss_aware_join(delimiter = '')
    ''.html_safe.tap do |str|
      each_with_index do |element, i|
        str << delimiter if i > 0
        str << element
      end
    end
  end

  # given an array of css classes/styles, join them into one string.
  # only join non-nil/non-empty strings, and return nil if the result
  # is an empty string (rails tag methods will not include the 
  # attribute if it is nil, which is desirable for cleaner html)
  #------------------------------------------------------------------------------
  def css_join(delimiter = '')
    str = self.flatten.delete_if {|x| x.nil? || x == ''}.join(' ')
    str == '' ? nil : str
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dm_ruby_extensions-1.0.8 lib/dm_ruby_extensions/extend_array.rb
dm_ruby_extensions-1.0.7 lib/dm_ruby_extensions/extend_array.rb
dm_ruby_extensions-1.0.6 lib/dm_ruby_extensions/extend_array.rb
dm_ruby_extensions-1.0.5 lib/dm_ruby_extensions/extend_array.rb
dm_ruby_extensions-1.0.0 lib/dm_ruby_extensions/extend_array.rb