Project

uber_array

0.0
No commit activity in last 3 years
No release in over 3 years
Enables SQL-like 'where' syntax for arrays of Hashes or Objects
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.10
~> 10.1
~> 3.1
 Project Readme

#uber_array

UberArray is an Array based datatype that enables SQL-like where syntax for arrays of Hashes or Objects.

In Hash based arrays each element is a Hash with symbols or strings as keys. In Object based arrays each element is an Object-derived instance that responds to attribute names instead of keys.

Installation

Add this line to your application's Gemfile:

gem 'uber_array'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install uber_array

Usage

require 'uber_array'

# Array of Hash elements with strings as keys
items = [
  { 'name' => 'Jack', 'score' => 999, 'active' => false },
  { 'name' => 'Jake', 'score' => 888, 'active' => true  },
  { 'name' => 'John', 'score' => 777, 'active' => true  }
]

uber_items = UberArray.new(items)

uber_items.where('name' => 'John')
uber_items.where('name' => /Ja/i)
uber_items.like('ja')
uber_items.where('name' => %w(Dave John Tom))
uber_items.where('score' => 999)
uber_items.where('score' => ->(s){s > 900})
uber_items.where('active' => true, 'score' => 800..900)
uber_items.map_by('name')

Player = Struct.new(:name, :score, :active)

# Array of Object-like Struct elements with attributes
objs = [
  Player.new('Jack', 999, false),
  Player.new('Jake', 888, true),
  Player.new('John', 777, true)
]

uber_objs = UberArray.new(objs, :primary_key => :__name__)

uber_objs.where(:__name__ => 'John')
uber_objs.where(:__name__ => /Ja/i)
uber_objs.like('ja')
uber_objs.where(:__name__ => %w(Dave John Tom))
uber_objs.where(:__score__ => 999)
uber_objs.where(:__score__ => ->(s){s > 900})
uber_objs.where(:__active__ => true, :__score__ => 800..900)

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request