Code Monkey home page Code Monkey logo

looksy's Introduction

Looksy

Looksy allows you to add a caching layer to any object that responds to the following methods:

  • .all - returns a collection of objects
  • #id - returns a unique identifier for an object
  • #attributes - a hash containing attributes and their values which can be used to filter the collection of objects returned by the .all method.

Loosky was primarily designed to work with ActiveRecord models representing lookup tables in a database. It works by loading the collection, returned from the .all method, in cache and then filtering those records based on their attributes. If you think your collection is too big to store in cache then Looksy is not the right tool.

Installation

Add this line to your application's Gemfile:

gem 'looksy'

And then execute:

$ bundle

Or install it yourself as:

$ gem install looksy

Usage

Looksy was designed to work with ActiveRecord, but can work with any object that responds to .all, #id and #attributes. Simply include Looksy::Cacheable in the class that you want to cache.

class Category < ActiveRecord::Base
    include Looksy::Cacheable
end

Configuration

The Looksy::Cacheable module provides the following configuration methods:

class Category < ActiveRecord::Base
    include Looksy::Cacheable
    
    self.cache_store = MyCacheStore.new
    self.cache_key = 'my_cache_store_key'
    self.cache_options = { expires_in: 1.hour }
end

Defaults

The configuration methods above do have defaults and so do not require setting:

  • cache_store - defaults to Rails cache or an in-memory cache
  • cache_key - defaults to the classname/all
  • cache_options - defaults to an empty hash

Methods Added by Looksy::Cacheable

class Category < ActiveRecord:Base
    include Looksy::Cacheable
    
    # attributes - id, name, parent_id, status
end

Assuming the class above here are the methods you have available to you:

  • Category.fetch_all - returns the entire collection
  • Category.fetch_first - returns the first object in the collection
  • Category.fetch_last - returns the last object in the collection
  • Category.fetch_by_id(id) - returns the object matching the id
  • Category.fetch_all_by_status(status) - returns a collection filtered by status
  • Category.fetch_all_by_status_and_parent_id(status, parent_id) - returns a collection filtered by status and parent id
  • Category.fetch_by_name(name) - returns the first object matching the name
  • Category.fetch_by_parent_id_and_status(parent_id, status) - returns the first object matching the parent id and status
  • Category.fetch_last_by_parent_id(parent_id) - returns the last object matching the parent id
  • Category.fetch_last_by_parent_id_and_status(parent_id, status) - returns the last object matching the parent id and status

All methods above, after fetch_by_id, are dynamic methods that operate based on the attributes hash returned by an instance.

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

looksy's People

Contributors

artinboghosian avatar

Watchers

 avatar  avatar

looksy's Issues

License missing from gemspec

Some companies will only use gems with a certain license.
The canonical and easy way to check is via the gemspec
via e.g.

spec.license = 'MIT'
# or
spec.licenses = ['MIT', 'GPL-2']

There is even a License Finder to help companies ensure all gems they use
meet their licensing needs. This tool depends on license information being available in the gemspec.
Including a license in your gemspec is a good practice, in any case.

If you need help choosing a license, github has created a license picker tool

How did I find you?

I'm using a script to collect stats on gems, originally looking for download data, but decided to collect licenses too,
and make issues for missing ones as a public service :)
https://gist.github.com/bf4/5952053#file-license_issue-rb-L13 So far it's going pretty well.
I've written a blog post about it

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.