class Kilt::ObjectCollection

Attributes

values[RW]

Public Class Methods

new(values = []) click to toggle source
# File lib/kilt/object_collection.rb, line 11
def initialize(values = [])
  @values = values
end

Public Instance Methods

[](key) click to toggle source
# File lib/kilt/object_collection.rb, line 15
def [](key)
  @values[key]
end
empty?() click to toggle source
# File lib/kilt/object_collection.rb, line 53
def empty?
  @values.empty?
end
group(key) click to toggle source
# File lib/kilt/object_collection.rb, line 29
def group(key)
  # create an empty hash
  ret = {}
  
  # loop through all items in @values
  @values.each do |object|
    # for each item, check to see if the hash already has a value for this key
    if ret[object[key]]
      # if it does, add it to the value, which should be an array
      a = ret[object[key]]
      a << object
    else
      # if it doesn't, create an array first, add this loop item to it, and add the
      # array as the value for that hash key
      a = []
      a << object
      ret[object[key]] = a
    end
  end
  
  # return the hash
  ret
end
order(key = 'name', direction = 'ASC') click to toggle source
# File lib/kilt/object_collection.rb, line 19
def order(key = 'name', direction = 'ASC')
  @values = @values.sort_by { |hash|
    is_int?(hash[key]) ? hash[key].to_i : hash[key]
  }
  if direction == 'DESC'
    @values.reverse!
  end
  return self
end

Private Instance Methods

is_int?(str) click to toggle source
# File lib/kilt/object_collection.rb, line 59
def is_int?(str)
  begin
    !!Integer(str)
  rescue ArgumentError, TypeError
    false
  end
end