map_reduced 0.0.1

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.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Scott Burton
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.rdoc ADDED
@@ -0,0 +1,30 @@
1
+ = map_reduced
2
+
3
+ Easy templating for your MongoDB map_reduce runs.
4
+
5
+ class User
6
+ include MapReduced::Document
7
+
8
+ map_reduce :count_followers, ...
9
+ functions :find_thing
10
+ end
11
+
12
+ ## User.run_count_followers
13
+ #
14
+ # => #<Mongo::Collection:0x00000100e17a50 @name="tmp.mr.mapreduce_1283030647_26", @db=#<Mongo::DB:0x00000100cbddb0 ......
15
+
16
+ In this situation, you'd have a directory called "user" in your function templates path (defaults to "app/functions" in Rails, "./functions" elsewhere) with "count_followers_map.js.erb", "count_followers_reduce.js.erb", and "find_thing.js.erb".
17
+
18
+ == Note on Patches/Pull Requests
19
+
20
+ * Fork the project.
21
+ * Make your feature addition or bug fix.
22
+ * Add tests for it. This is important so I don't break it in a
23
+ future version unintentionally.
24
+ * Commit, do not mess with rakefile, version, or history.
25
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
26
+ * Send me a pull request. Bonus points for topic branches.
27
+
28
+ == Copyright
29
+
30
+ Copyright (c) 2010 Scott Burton. See LICENSE for details.
@@ -0,0 +1,11 @@
1
+ require 'map_reduced'
2
+ require 'map_reduced/config'
3
+ require 'map_reduced/document'
4
+ require 'standard_additions/string'
5
+ require 'mongo'
6
+ require 'erb'
7
+ require 'pathname'
8
+
9
+ module MapReduced
10
+ VERSION = "0.0.1"
11
+ end
@@ -0,0 +1,17 @@
1
+ module MapReduced
2
+ class Config
3
+ class << self
4
+
5
+ attr_reader :db
6
+
7
+ def database(string)
8
+ @db = Mongo::Connection.new.db(string)
9
+ end
10
+
11
+ def template_path
12
+ defined?(Rails) ? Rails.root + "app/functions" : Pathname.new(File.expand_path("./functions"))
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,82 @@
1
+ module MapReduced
2
+ module Document
3
+ module ClassMethods
4
+
5
+ def map_reduce(*names)
6
+ names.each do |name|
7
+ self.class_eval %Q{
8
+ def self.run_#{name}
9
+ begin
10
+ setup_functions
11
+ collection.map_reduce(map("#{name}"), reduce("#{name}"))
12
+ ensure
13
+ teardown_functions
14
+ end
15
+ end
16
+ }
17
+ end
18
+ end
19
+
20
+ def db
21
+ MapReduced::Config.db
22
+ end
23
+
24
+ def collection
25
+ db.collection(collection_name)
26
+ end
27
+
28
+ private
29
+
30
+ def collection_name
31
+ "#{self.to_s.downcase}s"
32
+ end
33
+
34
+ def collection_name=(name)
35
+ @@collection_name = name
36
+ end
37
+
38
+ def map(function)
39
+ function_string("#{function}_map")
40
+ end
41
+
42
+ def reduce(function)
43
+ function_string("#{function}_reduce")
44
+ end
45
+
46
+ def functions(*names)
47
+ @@functions = names
48
+ end
49
+
50
+ def setup_functions
51
+ @@functions.each do |function_name|
52
+ db.add_stored_function(function_name.to_s, function_string(function_name))
53
+ end
54
+ end
55
+
56
+ def teardown_functions
57
+ @@functions.each {|name| db.remove_stored_function(name.to_s)}
58
+ end
59
+
60
+ def function_string(name)
61
+ template = File.read(path_to_function(name)).gsub(/[\n\t\s]+/, " ")
62
+ ERB.new(template).result(binding)
63
+ end
64
+
65
+ def path_to_function(function)
66
+ MapReduced::Config.template_path.to_s + "/#{self.name.split('::').last.underscore}/#{function}.js.erb"
67
+ end
68
+ end
69
+
70
+ module InstanceMethods
71
+
72
+ end
73
+
74
+ def self.included(receiver)
75
+ receiver.class_eval %Q{
76
+ @@functions = []
77
+ }
78
+ receiver.extend ClassMethods
79
+ receiver.send :include, InstanceMethods
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,5 @@
1
+ class String
2
+ def underscore
3
+ self.scan(/[A-Z][a-z0-9]*/).map(&:downcase).join("_")
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: map_reduced
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Scott Burton
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-28 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Easily add MongoDB mapreduce functions and runners to your Mongo ORM classes
36
+ email:
37
+ - scottburton11@gmail.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - lib/map_reduced/config.rb
46
+ - lib/map_reduced/document.rb
47
+ - lib/map_reduced.rb
48
+ - lib/standard_additions/string.rb
49
+ - LICENSE
50
+ - README.rdoc
51
+ has_rdoc: true
52
+ homepage: http://github.com/scottburton11/map_reduced
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options: []
57
+
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 23
75
+ segments:
76
+ - 1
77
+ - 3
78
+ - 6
79
+ version: 1.3.6
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.3.7
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Easily add MongoDB mapreduce functions and runners to your Mongo ORM classes
87
+ test_files: []
88
+