# bad_ass_extensions

Bad Ass Extensions are a set of standard library extensions and tools I use on a regular basis.

## Installing

gem install bad_ass_extensions

## Docs

### Array

#### sort_by_list A way of sorting a list by using a predetermined sort order of an attribute. For instance, if you had a list of User objects, you can sort them by passing in a list of ordered attributes, such as

User.all.sort_by_list(["Boss", "Manager", "Worker", "Janitor"])

#### frequency_list Takes an array of items and returns a list new array of how frequent those items are, for example:

> [1, 1, 3, 3, 4, 4, 4, 5].frequency_list

## OUTPUT
## [[1, 2], [3,2], [4,3], [5,1]]

#### uniquify Takes an array of items and returns a new list of of items based upon a block you pass in, for example:

> people << Person.new(:first_name => "Jason", :career => "Superman")
> people << Person.new(:first_name => "Mike", :career => "Engineer")
> people << Person.new(:first_name => "Aaron", :career => "Dog Walker")
> people << Person.new(:first_name => "Joseph", :career => "Dog Walker")
> people << Person.new(:first_name => "Allen", :career => "Superman")
> people.uniquify{|p| p.career}

## OUTPUT
## ["Jason", "Mike", "Aaron"] ## would return the Person object really

### Enumerable

#### group_count Probably my favorite of all… You can take an array of items and it groups them by whatever block you pass in, and then returns a hash where the keys are what you were grouping on, and the value is the count of items in the group.

> ['beholder', 'medusa', 'medusa', 'dog', 'dog', 'cat', 'person', 'dingo', 'bear', 'nharwal', 'nharwal', 'cyclops', 'dog'].group_count{|being| being }

## OUTPUT
## {"cat"=>1, "person"=>1, "medusa"=>2, "nharwal"=>2, "beholder"=>1, "dog"=>3, "bear"=>1, "dingo"=>1, "cyclops"=>1}

### Hash

#### rowized Just a clean way of displaying hash data

> hash = {"cat"=>1, "person"=>1, "medusa"=>2, "nharwal"=>2, "beholder"=>1, "dog"=>3, "bear"=>1, "dingo"=>1, "cyclops"=>1}
> puts hash.rowized

## OUTPUT
     cat: 1
  person: 1
 nharwal: 2
  medusa: 2
beholder: 1
     dog: 3
    bear: 1
 cyclops: 1
   dingo: 1

### Date Just added the ability to find differences between 2 dates

#### days_between(start, finish)

#### weeks_between(start, finish)

#### months_between(start, finish)

#### years_between(start, finish)

FROM: rails.lighthouseapp.com/projects/8994/tickets/879-finding-the-days-weeks-months-years-between-two-dates

### Histogram

Often I need to see how the data patterns are going from one day to the next… How many emails were sent out between 1 & 2pm, or How many orders were done by hour this day. I find that looking at histograms really allows you to see how things are behaving and gives great insight into your data.

This histogram class is just my simple attempt at getting data from a variety of different data structures (needs to be in some decent original form).

From the sample output, you can see that on the last day, there was some spike in activity during the early afternoon (Hours 13 and 14).

Just a useful tool in your arsenal of data mining.

Checkout: forrst.com/posts/Histogram_for_Command_Line_IRB_usage-1Zn for a display of usage

### RailsCookieDebugger

require 'rubygems'
require 'bad_ass_extensions'
cookie = "BAh7ByIQX2NzcmZfdG9rZW4iMWRDejZSMEw2cXcyMTBaWVhzRnZGSU92SFFGUHM1R2VrZUNTeHR4ODd5aUk9Ig9zZXNzaW9uX2lkIiU4ZWRlN2UwYWQ3NDhhMjVmNDUzNTYyYTRiY2MzMDc3Yw%3D%3D--05f7a04ebbe9fec2f70d8ad9faaddfedd7863912"
session = RailsCookieDebugger.show_session(cookie)
pp session

# Output
{"_csrf_token"=>"dCz6R0L6qw210ZYXsFvFIOvHQFPs5GekeCSxtx87yiI=",
 "session_id"=>"8ede7e0ad748a25f453562a4bcc3077c"}

## Contributing to bad_ass_extensions

## TODO

needs some serious testing

## Copyright

Copyright © 2011 Jason Amster. See LICENSE.txt for further details.