mongoid_silo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid-silo.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 John
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.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Mongoid::Silo
2
+
3
+ Mongoid Silo creates and transparently manages static representations of a model - e.g. for creating Feeds or other diverse datastructures.
4
+
5
+ It's pretty opinionated, and thus must be awesome. It requires the latest Mongoid and Sidekiq, mainly because they are both awesome, secondly because it uses them.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'mongoid-silo'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install mongoid-silo
22
+
23
+ ## Usage
24
+
25
+ Include ```Mongoid::Silo``` in your model, and then declare your silos.
26
+
27
+ ```ruby
28
+ # The default usage creates a "default" silo, accessable through instance#default_silo that will call
29
+ # an instance#to_silo method to populate itself on save.
30
+ silo
31
+
32
+ # Or you can specify the silo name and the method that will be called to populate it, like so...
33
+ silo :feed, :make_my_feed
34
+ ```
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,16 @@
1
+ require 'mongoid'
2
+
3
+ class Silo
4
+ include Mongoid::Document
5
+ include Mongoid::Timestamps
6
+
7
+ field :item_class, type: String
8
+ field :item_id, type: String
9
+ field :silo_type, type: String
10
+ field :bag, type: Hash
11
+
12
+
13
+ index item_class: 1
14
+ index item_id: 1
15
+ index silo_type: 1
16
+ end
@@ -0,0 +1,37 @@
1
+ require 'sidekiq'
2
+
3
+ module MongoidSilo
4
+
5
+ class UpdateSiloWorker
6
+ include Sidekiq::Worker
7
+
8
+ def perform(item_id, item_class, name, mode="save", method=nil)
9
+ @item_id, @item_class = item_id, item_class
10
+
11
+ mode.to_s == "save" ? update_silo(name, method) : destroy_silo(name)
12
+ end
13
+
14
+
15
+ private
16
+ def update_silo name, method
17
+ puts "[SILO] Creating or Updating Silo for #{@item_class}::::#{@item_id}"
18
+ @item = @item_class.classify.constantize.send(:find, @item_id)
19
+ @silo = Silo.where(item_class: @item_class, item_id: @item_id).first
20
+ if @silo
21
+ @silo.set(:bag, @item.to_silo)
22
+ else
23
+ @silo = Silo.create(item_class: @item_class, item_id: @item_id, bag: @item.send(method), silo_type: name)
24
+ end
25
+ end
26
+
27
+ def destroy_silo name
28
+ puts "[SILO] Destroying Silo for #{@item_class}::::#{@item_id}"
29
+ @silo = Silo.where(item_class: @item_class, item_id: @item_id, silo_type: name).first
30
+ if @silo
31
+ @silo.destroy
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,55 @@
1
+ require 'active_support/concern'
2
+
3
+ module Mongoid
4
+ module Silo
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def silo name=:default, method=:to_silo
9
+ define_method "#{name}_silo" do
10
+ from_silo name
11
+ end
12
+
13
+ set_callback :save, :after do
14
+ update_silo name, method
15
+ end
16
+
17
+ set_callback :destroy, :after do
18
+ destroy_silo name
19
+ end
20
+ end
21
+ end
22
+
23
+ module InstanceMethods
24
+ def update_silo name, method
25
+ MongoidSilo::UpdateSiloWorker.perform_async(self.id.to_s, self.class.to_s, name, :save, method)
26
+ end
27
+
28
+ def destroy_silo name
29
+ MongoidSilo::UpdateSiloWorker.perform_async(self.id.to_s, self.class.to_s, name, :destroy)
30
+ end
31
+
32
+ def to_silo #OVERWRITE THIS IN YOUR MODELS, BITCHES
33
+ {}
34
+ end
35
+
36
+ def from_silo name="default"
37
+ if @bag && @bag[name]
38
+ @bag[name]
39
+ else
40
+ @bag = {}
41
+ @bag[name] ||= begin
42
+ silo = ::Silo.where(item_class: self.class.to_s, item_id: self.id.to_s, silo_type: name).first
43
+ silo.bag
44
+ rescue Mongoid::Errors::DocumentNotFound
45
+ {}
46
+ end
47
+ end
48
+ end
49
+
50
+ def json_from_silo
51
+ MultiJson.encode from_silo
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ module MongoidSilo
2
+ class Railtie < Rails::Railtie
3
+
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module MongoidSilo
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,8 @@
1
+ require "mongoid_silo/version"
2
+ require 'mongoid_silo/railtie' if defined?(Rails)
3
+ require 'mongoid/silo'
4
+ require 'models/silo'
5
+ require 'workers/mongoid_silo/update_silo_worker'
6
+
7
+ module MongoidSilo
8
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ app = File.expand_path('../app', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ $LOAD_PATH.unshift(app) unless $LOAD_PATH.include?(app)
6
+
7
+ require 'mongoid_silo/version'
8
+
9
+ Gem::Specification.new do |gem|
10
+ gem.name = "mongoid_silo"
11
+ gem.version = MongoidSilo::VERSION
12
+ gem.authors = ["John Maxwell"]
13
+ gem.email = ["john@musicglue.com"]
14
+ gem.description = %q{MongoidSilo gives a simple way to create static representations of models}
15
+ gem.summary = %q{MongoidSilo is a bit like a Grain Elevator, but without the grain.}
16
+ gem.homepage = "https://github.com/musicglue/mongoid_silo"
17
+
18
+ gem.files = `git ls-files`.split($/)
19
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
21
+ gem.require_paths = ["lib"]
22
+
23
+ gem.add_dependency 'activesupport', '~> 3.2.9'
24
+ gem.add_dependency 'mongoid', '~> 3.0.16'
25
+ gem.add_dependency 'sidekiq', '~> 2.6.1'
26
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_silo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - John Maxwell
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.9
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.9
30
+ - !ruby/object:Gem::Dependency
31
+ name: mongoid
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 3.0.16
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 3.0.16
46
+ - !ruby/object:Gem::Dependency
47
+ name: sidekiq
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 2.6.1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.6.1
62
+ description: MongoidSilo gives a simple way to create static representations of models
63
+ email:
64
+ - john@musicglue.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - app/models/silo.rb
75
+ - app/workers/mongoid_silo/update_silo_worker.rb
76
+ - lib/mongoid/silo.rb
77
+ - lib/mongoid_silo.rb
78
+ - lib/mongoid_silo/railtie.rb
79
+ - lib/mongoid_silo/version.rb
80
+ - mongoid_silo.gemspec
81
+ homepage: https://github.com/musicglue/mongoid_silo
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.24
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: MongoidSilo is a bit like a Grain Elevator, but without the grain.
105
+ test_files: []
106
+ has_rdoc: