Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongo_coffee

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongo_coffee

  • 1.0.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

MongoCoffee

Gem Version

Map reduce your mongo documents writing in with coffeescript. As simple as it sounds! Finally you can organize and write your huge map/reduce/finalize functions in coffeescript syntax!

Setup and run!

Run mongo_coffee:config generator to setup config files on your rails project. Then you can edit them as your needs:

$ rails g mongo_coffee:config
      create  config/mongo_coffee.yml
      create  config/initializers/mongo_coffee.rb

On mongo_coffee.yml file you'll find some config you might change:

coffee_path: 'app/models/map_reduces'
coffee_extensions:
  - .js.coffee
  - .coffee

Usage

The MongoCoffee compiler engine will search on coffee_path path (see setup section above) for files matching the following convention: %name%_%map/reduce/finalize%.js.coffee, i.e in the example below the following files should be ready in coffee_path directory (could be in some subdirectory):

  • playing_stats_map.js.coffee
  • playing_stats_reduce.js.coffee
  • playing_stats_finalize.js.coffee

Where this may be the content of the files:

playing_stats_map.js.coffee

->
  emit
    artirst: @artist
    album: @album,
      plays: 1
      votes: 1

playing_stats_reduce.js.coffee

(key, values) ->
  result =
    plays: 0
    votes: 0
  

  for value in values
    result.plays += value.plays
    result.votes += value.votes

Have a look at the mongodb map/reduce documentation.

Caffeine to your model

Call any of your map/reduce coffeescript collection with:

Band.caffeine_map_reduce('playing_stats').out(inline: 1)

If you've defined a finalize coffesript function you can also use it with:

Band.caffeine_map_reduce('playing_stats').caffeine_finalize('playing_stats').out(inline: 1)

As you may think, a map/reduce action will generate a new collection on MongoDB so, you may create it's model for interaction with new documents:

class PlayingStat
  include Mongoid::Document
  store_in collection: 'playing_stats'

  field :value

  index({"_id.artist" => 1},{background: true})
  index({"_id.artist" => 1, "_id.album" => 1},{background: true})

  def artist
    _id["artist"]
  end

  def album
    _id["album"]
  end

  def total_plays
    value["plays"]
  end

  def total_votes
    value["votes"]
  end
end

License

This project uses MIT-LICENSE.

FAQs

Package last updated on 02 Nov 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc