mongo_coffee 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 547f3b0707e2398d5c0a152f388ac7829016ed66
4
+ data.tar.gz: c1f1810cce1be4064da94e419d701282730a1336
5
+ SHA512:
6
+ metadata.gz: bd37c3876a41c87371e201feae768d8a56e618d0658ef319f60f8f4974ac88254ce53028ffa7110d3be3b4f3e6410531272c964eb5bbe615bff8c6c96110dc3f
7
+ data.tar.gz: ee423dd995131c1d81401c764792bc96a6c32b049c9837a005945c536e94673f391ac8583b73a3a0a632fac7524c5d97e2ece3419663fa2e3092a0adf1a579b2
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ test/dummy/db/*.sqlite3
5
+ test/dummy/log/*.log
6
+ test/dummy/tmp/
7
+ test/dummy/.sass-cache
8
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Declare your gem's dependencies in mongocoffee.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # jquery-rails is used by the dummy application
9
+ gem "jquery-rails"
10
+
11
+ # Declare any dependencies that are still in development here instead of in
12
+ # your gemspec. These might include edge Rails or gems from your path or
13
+ # Git. Remember to move these dependencies to your gemspec before releasing
14
+ # your gem to rubygems.org.
15
+
16
+ # To use debugger
17
+ # gem 'debugger'
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Guillermo Guerrero
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # MongoCoffee
2
+
3
+ Map reduce your mongo documents writing in with coffeescript.
4
+ As simple as it sounds! Finally you can organize and write your huge map/reduce/finalize functions in coffeescript syntax!
5
+
6
+ ## Setup and run!
7
+ Run **mongo_coffee:config** generator to setup config files on your rails project.
8
+ Then you can edit them as your needs:
9
+
10
+ ```bash
11
+ $ rails g mongo_coffee:config
12
+ create config/mongo_coffee.yml
13
+ create config/initializers/mongo_coffee.rb
14
+ ```
15
+
16
+ On *mongo_coffee.yml* file you'll find some config you might change:
17
+
18
+ ```yaml
19
+ coffee_path: 'app/models/map_reduces'
20
+ coffee_extensions:
21
+ - .js.coffee
22
+ - .coffee
23
+ ```
24
+
25
+ ## Usage
26
+ The **MongoCoffee** compiler engine will search on **coffee_path** path (see *setup* section above) for files
27
+ matching the following convention: **%name%_%map/reduce/finalize%.js.coffee**, i.e in the example below the
28
+ following files should be ready in *coffee_path* directory (could be in some subdirectory):
29
+
30
+ - playing_stats_map.js.coffee
31
+ - playing_stats_reduce.js.coffee
32
+ - playing_stats_finalize.js.coffee
33
+
34
+ Where this may be the content of the files:
35
+
36
+ **playing_stats_map.js.coffee**
37
+
38
+ ```coffeescript
39
+ ->
40
+ emit
41
+ artirst: @artist
42
+ album: @album,
43
+ plays: 1
44
+ votes: 1
45
+ ```
46
+
47
+ **playing_stats_reduce.js.coffee**
48
+
49
+
50
+ ```coffeescript
51
+ (key, values) ->
52
+ result =
53
+ plays: 0
54
+ votes: 0
55
+
56
+
57
+ for value in values
58
+ result.plays += value.plays
59
+ result.votes += value.votes
60
+ ```
61
+
62
+ Have a look at [**the mongodb map/reduce documentation**](https://docs.mongodb.org/manual/core/map-reduce/).
63
+
64
+ ### Caffeine to your model
65
+ Call any of your map/reduce coffeescript collection with:
66
+
67
+ ```ruby
68
+ Band.caffeine_map_reduce('playing_stats').out(inline: 1)
69
+ ```
70
+
71
+ If you've defined a finalize coffesript function you can also use it with:
72
+
73
+ ```ruby
74
+ Band.caffeine_map_reduce('playing_stats').caffeine_finalize('playing_stats').out(inline: 1)
75
+ ```
76
+
77
+ 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:
78
+
79
+ ```ruby
80
+ class PlayingStat
81
+ include Mongoid::Document
82
+ store_in collection: 'playing_stats'
83
+
84
+ field :value
85
+
86
+ index({"_id.artist" => 1},{background: true})
87
+ index({"_id.artist" => 1, "_id.album" => 1},{background: true})
88
+
89
+ def artist
90
+ _id["artist"]
91
+ end
92
+
93
+ def album
94
+ _id["album"]
95
+ end
96
+
97
+ def total_plays
98
+ value["plays"]
99
+ end
100
+
101
+ def total_votes
102
+ value["votes"]
103
+ end
104
+ end
105
+ ```
106
+
107
+ ## License
108
+ This project uses [*MIT-LICENSE*](http://en.wikipedia.org/wiki/MIT_License).
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Mongocoffee'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rake/testtask'
29
+
30
+ Rake::TestTask.new(:test) do |t|
31
+ t.libs << 'lib'
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/*_test.rb'
34
+ t.verbose = false
35
+ end
36
+
37
+
38
+ task :default => :test
@@ -0,0 +1,9 @@
1
+ class MongoCoffee::ConfigGenerator < Rails::Generators::Base
2
+ source_root File.expand_path("../templates", __FILE__)
3
+
4
+ desc "Generate the mongo coffee config and initializer files in your project."
5
+ def copy_config_file
6
+ copy_file "mongo_coffee.yml", "config/mongo_coffee.yml"
7
+ copy_file "mongo_coffee.rb", "config/initializers/mongo_coffee.rb"
8
+ end
9
+ end
@@ -0,0 +1,2 @@
1
+ require 'mongo_coffee'
2
+ MongoCoffee::Compiler.setup! File.expand_path('../../mongo_coffee.yml', __FILE__)
@@ -0,0 +1,4 @@
1
+ coffee_path: 'app/models/map_reduces'
2
+ coffee_extensions:
3
+ - .js.coffee
4
+ - .coffee
@@ -0,0 +1,45 @@
1
+ require 'coffee-script'
2
+
3
+ module MongoCoffee
4
+
5
+ class CoffeeFileNotFound < StandardError
6
+ def initialize(filename)
7
+ message = "File '#{filename}' does not exists."
8
+ super message
9
+ end
10
+ end
11
+
12
+ class Compiler
13
+
14
+ class << self
15
+ def setup!(filename)
16
+ @config = Config.load filename
17
+ end
18
+
19
+ def compile(name, action)
20
+ @name = name
21
+ @action = action
22
+
23
+ extract
24
+ end
25
+
26
+ private
27
+ def filename
28
+ path = @config["coffee_path"]
29
+ extensions = @config["coffee_extensions"].join(',')
30
+
31
+ wildcard = Rails.root.join(path, "**/#{@name}_#{@action}{#{extensions}}")
32
+
33
+ files = Dir.glob(wildcard)
34
+ raise CoffeeFileNotFound, wildcard if files.empty?
35
+
36
+ files.first
37
+ end
38
+
39
+ def extract
40
+ CoffeeScript.compile(File.open(filename), bare: true).chomp.slice(1..-3)
41
+ end
42
+ end
43
+ end
44
+
45
+ end
@@ -0,0 +1,9 @@
1
+ require 'yaml'
2
+
3
+ module MongoCoffee
4
+ class Config
5
+ def self.load(filename)
6
+ YAML.load_file(filename)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module MongoCoffee
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,12 @@
1
+ module MongoCoffee::Mongoid
2
+ module Finders
3
+
4
+ def caffeine_map_reduce(filename)
5
+ compiled_map = MongoCoffee::Compiler.compile(filename, :map)
6
+ compiled_reduce = MongoCoffee::Compiler.compile(filename, :reduce)
7
+
8
+ map_reduce(compiled_map, compiled_reduce)
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module MongoCoffee::Mongoid
2
+ module MapReduce
3
+
4
+ def caffeine_finalize(filename)
5
+ compiled_finalize = MongoCoffee::Compiler.compile(filename, :finalize)
6
+
7
+ finalize compiled_finalize
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module MongoCoffee
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,17 @@
1
+ require 'mongo_coffee/version'
2
+ require 'mongo_coffee/engine'
3
+ require 'mongo_coffee/config'
4
+ require 'mongo_coffee/compiler'
5
+ require 'mongo_coffee/mongoid/finders'
6
+ require 'mongo_coffee/mongoid/map_reduce'
7
+
8
+
9
+ if defined? Mongoid
10
+ module Mongoid::Finders
11
+ include MongoCoffee::Mongoid::Finders
12
+ end
13
+
14
+ class Mongoid::Contextual::MapReduce
15
+ include MongoCoffee::Mongoid::MapReduce
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :mongocoffee do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,21 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ # Maintain your gem's version:
4
+ require "mongo_coffee/version"
5
+
6
+ # Describe your gem and declare its dependencies:
7
+ Gem::Specification.new do |s|
8
+ s.name = "mongo_coffee"
9
+ s.version = MongoCoffee::VERSION
10
+ s.authors = ["Guillermo Guerrero"]
11
+ s.email = ["g.guerrero.bus@gmail.com"]
12
+ s.homepage = "https://github.com/gguerrero/mongocoffee"
13
+ s.summary = "MongoDB mapReduce with coffeescript."
14
+ s.description = "Map reduce your mongo documents writing in with coffeescript."
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+
18
+ s.add_dependency "rails", "~> 3.2.14"
19
+ s.add_dependency "coffee-script", "~> 2.2.0"
20
+ s.add_dependency "mongoid", "~> 3.1.0"
21
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo_coffee
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Guillermo Guerrero
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.14
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.14
27
+ - !ruby/object:Gem::Dependency
28
+ name: coffee-script
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: mongoid
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.1.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.1.0
55
+ description: Map reduce your mongo documents writing in with coffeescript.
56
+ email:
57
+ - g.guerrero.bus@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - MIT-LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - lib/generators/mongo_coffee/config_generator.rb
68
+ - lib/generators/mongo_coffee/templates/mongo_coffee.rb
69
+ - lib/generators/mongo_coffee/templates/mongo_coffee.yml
70
+ - lib/mongo_coffee.rb
71
+ - lib/mongo_coffee/compiler.rb
72
+ - lib/mongo_coffee/config.rb
73
+ - lib/mongo_coffee/engine.rb
74
+ - lib/mongo_coffee/mongoid/finders.rb
75
+ - lib/mongo_coffee/mongoid/map_reduce.rb
76
+ - lib/mongo_coffee/version.rb
77
+ - lib/tasks/mongocoffee_tasks.rake
78
+ - mongo_coffee.gemspec
79
+ homepage: https://github.com/gguerrero/mongocoffee
80
+ licenses: []
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.4.8
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: MongoDB mapReduce with coffeescript.
102
+ test_files: []