mongo_method_storable 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 46becd59b1203a6408011c32a7c96b154ecc1eb3
4
+ data.tar.gz: 18ad6716bfb66c16573a66af0b2fa1018c1e2b8f
5
+ SHA512:
6
+ metadata.gz: 3b0b7a0468f02a2ff3713afa1e890fdd72e98b13fdbe0c04b5ca2fb3c41e9d6fa6bd3941a88c2889a42d7b6c2ad5d31c1ffbafd45a8a9a44944a5c62369302e3
7
+ data.tar.gz: c0dedda0f01ce4d4faee33788c4a880bfb923c60c90dc46d8bbb994f1aa6b601eb643051bc6688c46b492144c00db1ddba3be9dcb08f6dce19babb3171299f95
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongo_method_storable.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Andy Pickler
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ # MongoMethodStorable
2
+
3
+ MongoMethodStorage allows you to store the output of methods in a Mongoid::Document class into MongoDB dynamic fields. It also lazily stores the output, in that the value is only stored the first time the method is called on a given object. Subsequent calls to the method will just read the attribute value from the document, eliminating the "cost" of executing the method.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mongo_method_storable'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mongo_method_storable
20
+
21
+ ## Usage
22
+
23
+ `include MongoMethodStorable`
24
+
25
+ ```ruby
26
+ def some_method
27
+ # complex/expensive algorithm
28
+ end
29
+ mongo_store :some_method
30
+ ```
31
+
32
+ ## Limitations
33
+
34
+ 1. Methods can take no arguments (currently)
35
+ 2. There is no option (currently) to force the method to be evaluated after the first time.
36
+ 3. No tests have been written on this code (caveat: it *is* being used in at least one production application)
37
+ 4. This is my first "real" published Ruby gem
38
+ 5. A bunch of other limitations I'm sure I'll think of over time
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it ( https://github.com/BigGillyStyle/mongo_method_storable/fork )
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create a new Pull Request
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ Dir.glob('tasks/**/*.rake').each(&method(:import))
@@ -0,0 +1,33 @@
1
+ require "mongo_method_storable/version"
2
+ # require "mongoid/attributes"
3
+
4
+ module MongoMethodStorable
5
+ MONGO_STORE_PREFIX = '_old_mstore_'
6
+
7
+ def mongo_store(method_name)
8
+ old_method_name = "#{MONGO_STORE_PREFIX + method_name.to_s}".to_sym
9
+ raise AlreadyAliasedError.new("Already aliased #{method_name}") if method_defined?(old_method_name)
10
+ alias_method old_method_name, method_name
11
+
12
+ if instance_method(method_name).arity == 0
13
+ define_method method_name do
14
+ attr_val = read_attribute method_name
15
+ return attr_val if attr_val
16
+ attr_val = send old_method_name
17
+ write_attribute(method_name, attr_val)
18
+ save!
19
+ attr_val
20
+ end
21
+ else
22
+ # deal with this case when I need to
23
+ end
24
+ end
25
+
26
+ def storable_methods
27
+ self.instance_methods.
28
+ select {|m| self.instance_method(m).name.to_s.start_with? MONGO_STORE_PREFIX}.
29
+ map {|m| m.slice(MONGO_STORE_PREFIX.length..m.length).to_sym}
30
+ end
31
+
32
+ class AlreadyAliasedError < RuntimeError; end
33
+ end
@@ -0,0 +1,3 @@
1
+ module MongoMethodStorable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mongo_method_storable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mongo_method_storable"
8
+ spec.version = MongoMethodStorable::VERSION
9
+ spec.authors = ["Andy Pickler"]
10
+ spec.email = ["andy.pickler@gmail.com"]
11
+ spec.summary = %q{Store the output of methods in a Mongoid::Document class into MongoDB dynamic fields.}
12
+ spec.description = %q{Store the output of methods in a Mongoid::Document class into MongoDB dynamic fields. Lazily evaluates methods and stores the output in document...subsequent calls simply read the attribute value and do not run the method.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.1.0"
24
+ spec.add_runtime_dependency "mongoid", "~> 4.0.0"
25
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe MongoMethodStorable do
4
+
5
+ describe '::mongo_store' do
6
+
7
+ it 'stores the output of a method in a Mongoid document' do
8
+ pending 'me gettimg the time to implement tests'
9
+ fail
10
+ end
11
+
12
+ end
13
+
14
+ describe '::storable_methods' do
15
+
16
+ it 'lists all the methods that use ::mongo_store' do
17
+ pending 'me gettimg the time to implement tests'
18
+ fail
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,2 @@
1
+ require 'mongo_method_storable'
2
+
@@ -0,0 +1,7 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec) do |task|
4
+ task.rspec_opts = ['--color', '--format', 'progress']
5
+ end
6
+
7
+ task :default => :spec
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo_method_storable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andy Pickler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.1.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: mongoid
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 4.0.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 4.0.0
69
+ description: Store the output of methods in a Mongoid::Document class into MongoDB
70
+ dynamic fields. Lazily evaluates methods and stores the output in document...subsequent
71
+ calls simply read the attribute value and do not run the method.
72
+ email:
73
+ - andy.pickler@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/mongo_method_storable.rb
84
+ - lib/mongo_method_storable/version.rb
85
+ - mongo_method_storable.gemspec
86
+ - spec/mongo_method_storable_spec.rb
87
+ - spec/spec_helper.rb
88
+ - tasks/rspec.rake
89
+ homepage: ''
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.4.2
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Store the output of methods in a Mongoid::Document class into MongoDB dynamic
113
+ fields.
114
+ test_files:
115
+ - spec/mongo_method_storable_spec.rb
116
+ - spec/spec_helper.rb