redis-messages 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/CHANGELOG ADDED
@@ -0,0 +1,2 @@
1
+ 0.1 (January 25, 2010)
2
+ Doing initial Redis messages generator, will be a swift move to 1.0
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2010 Seivan Heidari
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.
21
+ By reading this fine print your soul is now the exclusive property of WeDoBDD Productions and its Subsidiaries.
22
+ Unauthorized use of seivan gems, images, materials, souls, odors and oxygen is strongly discouraged. We know where you sleep.
23
+ Also, your mum & bring back prop8
data/README.rdoc ADDED
@@ -0,0 +1,21 @@
1
+ == Install
2
+
3
+ gem install redis-messages
4
+
5
+ == Rails 3
6
+
7
+ To use redis-messages with Rails 3 you will need to include it in your Gemfile.
8
+
9
+ group :development do
10
+ gem "redis-messages"
11
+ end
12
+
13
+ == Usage
14
+ == Included Generators
15
+ * rails g redis_messages : Will name keys and models as User and Message and the Redis constant to R
16
+
17
+ * rails g redis_messages letter user : Assuming the User model already exist, this will inject code into it and create a Letter model.
18
+
19
+ * rails g redis_messages letter player Red : Creates a Letter and Player model and names the Redis constant to Red
20
+
21
+ * And don't forget to run your migrations since we also utilize SQL here.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'cucumber'
4
+ require 'cucumber/rake/task'
5
+
6
+ Cucumber::Rake::Task.new(:features) do |t|
7
+ t.cucumber_opts = "features --format progress"
8
+ end
9
+
10
+ task :default => :features
@@ -0,0 +1,35 @@
1
+ Feature: Redis Messages Generator With Named Models
2
+ In order to send letters to multiple users
3
+ As a rails developer
4
+ I want to generate a messaging library using letters as a name
5
+
6
+
7
+ Scenario: Generate named messages
8
+ Given a new Rails app
9
+ When I run "rails g redis_messages letters players Red"
10
+ Then I should see the following files
11
+ | app/models/letter.rb |
12
+ # | app/helpers/messages_helper.rb |
13
+ | db/migrate |
14
+ And I should see "class Letter < ActiveRecord::Base" in file "app/models/letter.rb"
15
+ And I should see "belongs_to :player" in file "app/models/letter.rb"
16
+
17
+ And I should see "Red.smembers(Letter.receivers_for(self.id))" in file "app/models/letter.rb"
18
+ And I should see "Red.sadd(Letter.inbox_for(username), self.id)" in file "app/models/letter.rb"
19
+ And I should see "Red.sadd(Letter.receivers_for(self.id), username)" in file "app/models/letter.rb"
20
+
21
+ And I should see "def self.inbox_for(username)" in file "app/models/letter.rb"
22
+ And I should see "("letter:#{username}:inbox")" in file "app/models/letter.rb"
23
+
24
+ And I should see "def self.receivers_for(letter_id)" in file "app/models/letter.rb"
25
+ And I should see "("letter:#{letter_id}:receivers")" in file "app/models/letter.rb"
26
+
27
+ And I should see the following files
28
+ |app/models/player.rb|
29
+ And I should see "class Player < ActiveRecord::Base" in file "app/models/player.rb"
30
+ And I should see "has_many :letters" in file "app/models/player.rb"
31
+ And I should see "letter_ids = Red.smembers(Letter.inbox_for(self.username))" in file "app/models/player.rb"
32
+ And I should see "Letter.where(:visible => true, :id => letter_ids).order("created_at DESC")" in file "app/models/player.rb"
33
+
34
+ And I should see "self.letters(:visible => true)" in file "app/models/player.rb"
35
+ And I should not see file "temp_file.rb"
@@ -0,0 +1,48 @@
1
+ Feature: Redis Messages Generator
2
+ In order to send messages to multiple users
3
+ As a rails developer
4
+ I want to generate a messaging library
5
+
6
+ Scenario: Generate default messages
7
+ Given a new Rails app
8
+ When I run "rails g redis_messages"
9
+ Then I should see the following files
10
+ | app/models/message.rb |
11
+ # | app/helpers/messages_helper.rb |
12
+ | db/migrate |
13
+ And I should see "class Message < ActiveRecord::Base" in file "app/models/message.rb"
14
+ And I should see "belongs_to :user" in file "app/models/message.rb"
15
+
16
+ And I should see "def recievers" in file "app/models/message.rb"
17
+ And I should see "R.smembers(Message.receivers_for(self.id))" in file "app/models/message.rb"
18
+ And I should see "end" in file "app/models/message.rb"
19
+ And I should see "def send_to(receivers_usernames)" in file "app/models/message.rb"
20
+ And I should see "receivers_usernames.each do |username|" in file "app/models/message.rb"
21
+ And I should see "R.sadd(Message.inbox_for(username), self.id)" in file "app/models/message.rb"
22
+ And I should see "R.sadd(Message.receivers_for(self.id), username)" in file "app/models/message.rb"
23
+ And I should see "end" in file "app/models/message.rb"
24
+ And I should see "end" in file "app/models/message.rb"
25
+
26
+ And I should see "def self.inbox_for(username)" in file "app/models/message.rb"
27
+ And I should see "("message:#{username}:inbox")" in file "app/models/message.rb"
28
+ And I should see "end" in file "app/models/message.rb"
29
+
30
+ And I should see "def self.receivers_for(message_id)" in file "app/models/message.rb"
31
+ And I should see "("message:#{message_id}:receivers")" in file "app/models/message.rb"
32
+ And I should see "end" in file "app/models/message.rb"
33
+ And I should see "end" in file "app/models/message.rb"
34
+
35
+ And I should see the following files
36
+ |app/models/user.rb|
37
+ And I should see "class User < ActiveRecord::Base" in file "app/models/user.rb"
38
+ And I should see "has_many :messages" in file "app/models/user.rb"
39
+ And I should see "def inbox" in file "app/models/user.rb"
40
+ And I should see "message_ids = R.smembers(Message.inbox_for(self.username))" in file "app/models/user.rb"
41
+ And I should see "Message.where(:visible => true, :id => message_ids).order("created_at DESC")" in file "app/models/user.rb"
42
+ And I should see "end" in file "app/models/user.rb"
43
+ And I should see "def outbox" in file "app/models/user.rb"
44
+ And I should see "self.messages(:visible => true)" in file "app/models/user.rb"
45
+ And I should see "end" in file "app/models/user.rb"
46
+ And I should see "end" in file "app/models/user.rb"
47
+ And I should not see file "temp_file.rb"
48
+
@@ -0,0 +1,36 @@
1
+ Feature: Redis Messages Generator With Existing User Model
2
+ In order to send letters to multiple users
3
+ As a rails developer
4
+ I want to inject the messaging code into the existing User model
5
+
6
+
7
+ Scenario: Generate injected code into existing User Model
8
+ Given a new Rails app
9
+ When I run "rails g model user players username:string"
10
+ When I run "rails g redis_messages letters players Red"
11
+ Then I should see the following files
12
+ | app/models/letter.rb |
13
+ # | app/helpers/messages_helper.rb |
14
+ | db/migrate |
15
+ And I should see "class Letter < ActiveRecord::Base" in file "app/models/letter.rb"
16
+ And I should see "belongs_to :player" in file "app/models/letter.rb"
17
+
18
+ And I should see "Red.smembers(Letter.receivers_for(self.id))" in file "app/models/letter.rb"
19
+ And I should see "Red.sadd(Letter.inbox_for(username), self.id)" in file "app/models/letter.rb"
20
+ And I should see "Red.sadd(Letter.receivers_for(self.id), username)" in file "app/models/letter.rb"
21
+
22
+ And I should see "def self.inbox_for(username)" in file "app/models/letter.rb"
23
+ And I should see "("letter:#{username}:inbox")" in file "app/models/letter.rb"
24
+
25
+ And I should see "def self.receivers_for(letter_id)" in file "app/models/letter.rb"
26
+ And I should see "("letter:#{letter_id}:receivers")" in file "app/models/letter.rb"
27
+
28
+ And I should see the following files
29
+ |app/models/player.rb|
30
+ And I should see "class Player < ActiveRecord::Base" in file "app/models/player.rb"
31
+ And I should see "has_many :letters" in file "app/models/player.rb"
32
+ And I should see "letter_ids = Red.smembers(Letter.inbox_for(self.username))" in file "app/models/player.rb"
33
+ And I should see "Letter.where(:visible => true, :id => letter_ids).order("created_at DESC")" in file "app/models/player.rb"
34
+
35
+ And I should see "self.letters(:visible => true)" in file "app/models/player.rb"
36
+ And I should not see file "temp_file.rb"
@@ -0,0 +1,47 @@
1
+ When /^I run "([^\"]*)"$/ do |command|
2
+ system("cd #{@current_directory} && #{command}").should be_true
3
+ end
4
+
5
+ When /^I add "([^\"]*)" to file "([^\"]*)"$/ do |content, short_path|
6
+ path = File.join(@current_directory, short_path)
7
+ File.should exist(path)
8
+ File.open(path, 'a') { |f| f.write(content + "\n") }
9
+ end
10
+
11
+ When /^I replace "([^\"]*)" with "([^\"]*)" in file "([^\"]*)"$/ do |old_content, new_content, short_path|
12
+ path = File.join(@current_directory, short_path)
13
+ File.should exist(path)
14
+ content = File.read(path).gsub(old_content, new_content)
15
+ File.open(path, 'w') { |f| f.write(content) }
16
+ end
17
+
18
+ Then /^I should see file "([^\"]*)"$/ do |path|
19
+ File.should exist(File.join(@current_directory, path))
20
+ end
21
+
22
+ Then /^I should not see file "([^\"]*)"$/ do |path|
23
+ File.should_not exist(File.join(@current_directory, path))
24
+ end
25
+ Then /^I should see "(.*)" in file "([^\"]*)"$/ do |content, short_path|
26
+ path = File.join(@current_directory, short_path)
27
+ File.should exist(path)
28
+ File.readlines(path).join.should include(content)
29
+ end
30
+
31
+ Then /^I should see the following files$/ do |table|
32
+ table.raw.flatten.each do |path|
33
+ File.should exist(File.join(@current_directory, path))
34
+ end
35
+ end
36
+
37
+ Then /^I should see the following in file "([^\"]*)"$/ do |short_path, table|
38
+ path = File.join(@current_directory, short_path)
39
+ File.should exist(path)
40
+ table.raw.flatten.each do |content|
41
+ File.readlines(path).join.should include(content)
42
+ end
43
+ end
44
+
45
+ Then /^I should successfully run "([^\"]*)"$/ do |command|
46
+ system("cd #{@current_directory} && #{command}").should be_true
47
+ end
@@ -0,0 +1,6 @@
1
+ Given /^a new Rails app$/ do
2
+ FileUtils.mkdir_p("tmp")
3
+ system("rails new tmp/rails_app").should be_true
4
+ system("ln -s ../../../lib/generators tmp/rails_app/lib/generators").should be_true
5
+ @current_directory = File.expand_path("tmp/rails_app")
6
+ end
@@ -0,0 +1,6 @@
1
+ require 'cucumber'
2
+ require 'rspec'
3
+
4
+ Before do
5
+ FileUtils.rm_rf "tmp/rails_app"
6
+ end
@@ -0,0 +1,7 @@
1
+ RSpec::Matchers.define :exist do |path|
2
+ match do
3
+ File.exist?(path)
4
+ end
5
+ failure_message_for_should { "Expected #{path} to exist but no file found" }
6
+ failure_message_for_should_not { "Expected #{path} to not exist but file was found" }
7
+ end
@@ -0,0 +1,50 @@
1
+ Description:
2
+ Generates a user model, users controller, and sessions controller. The
3
+ users controller handles the registration and the sessions controller
4
+ handles authentication. This is similar to restful_authentication, but
5
+ simpler.
6
+
7
+ IMPORTANT: This generator uses the "title" helper method which is generated
8
+ by the nifty_layout generator. You may want to run that generator first.
9
+
10
+ Usage:
11
+ If you do not pass any arguments, the model name will default to "user", and
12
+ the authentication controller will default to "session". You can override
13
+ each of these respectively by passing one or two arguments. Either name can
14
+ be CamelCased or under_scored.
15
+
16
+ Make sure to setup the authlogic gem if you are using that option.
17
+
18
+ gem "authlogic" # in Gemfile
19
+
20
+ Examples:
21
+ rails generate nifty:authentication
22
+
23
+ Creates user model, users_controller, and sessions_controller.
24
+
25
+ rails generate nifty:authentication account
26
+
27
+ Creates account model, accounts_controller, and sessions_controller.
28
+
29
+ rails generate nifty:authentication Account UserSession
30
+
31
+ Creates account model, accounts_controller, and user_sessions_controller.
32
+
33
+ Methods:
34
+ There are several methods generated which you can use in your application.
35
+ Here's a common example of what you might add to your layout.
36
+
37
+ <% if logged_in? %>
38
+ Welcome <%= current_user.username %>! Not you?
39
+ <%= link_to "Log out", logout_path %>
40
+ <% else %>
41
+ <%= link_to "Sign up", signup_path %> or
42
+ <%= link_to "log in", login_path %>.
43
+ <% end %>
44
+
45
+ You can also restrict unregistered users from accessing a controller using
46
+ a before filter. For example.
47
+
48
+ before_filter :login_required, :except => [:index, :show]
49
+
50
+ See the generated file lib/authentication.rb for details.
@@ -0,0 +1,91 @@
1
+ require 'rails/generators/base'
2
+ require 'rails/generators/migration'
3
+
4
+ class RedisMessagesGenerator < Rails::Generators::Base #:nodoc:
5
+ include Rails::Generators::Migration
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ argument :message_name, :type => :string, :default => 'message', :banner => 'message_name'
9
+ argument :user_name, :type => :string, :default => 'user', :banner => 'user_name'
10
+ argument :redis_constant_name, :type => :string, :default => "R", :banner => "redis_constant_name"
11
+
12
+
13
+ def create_model_files
14
+ if File.exist?(File.join(Dir.pwd, "app", "models", "#{user_singular_name}.rb"))
15
+ template('user_injection_code.rb', "temp_file.rb") #I need to READ this with the erb in it
16
+ code = IO.read(File.join(Dir.pwd,"temp_file.rb"))
17
+ inject_into_class(File.join(Dir.pwd, "app", "models", "#{user_singular_name}.rb"), user_class_name, code)
18
+ File.delete(File.join(Dir.pwd,"temp_file.rb"))
19
+ else
20
+ template 'user.rb', "app/models/#{user_singular_name}.rb"
21
+ end
22
+ template 'message.rb', "app/models/#{message_singular_name}.rb"
23
+ end
24
+
25
+ def create_migration
26
+ migration_template 'message_migration.rb', "db/migrate/create_#{message_plural_name}.rb"
27
+ migration_template 'user_migration.rb', "db/migrate/create_#{user_plural_name}.rb" if File.exist?(File.join(Dir.pwd, "app", "models", "#{user_singular_name}.rb"))
28
+ end
29
+
30
+ def add_gems_and_initializer
31
+ unless File.read(destination_path("Gemfile")).include? "redis"
32
+ gem "redis"
33
+ template"redis.rb", "config/initializers/redis.rb"
34
+ end
35
+ end
36
+
37
+ private
38
+ def message_singular_name
39
+ message_name.underscore.singularize
40
+ end
41
+
42
+ def message_plural_name
43
+ message_singular_name.pluralize
44
+ end
45
+
46
+ def message_class_name
47
+ message_name.singularize.camelize
48
+ end
49
+
50
+ def message_plural_class_name
51
+ message_plural_name.camelize
52
+ end
53
+
54
+ def user_singular_name
55
+ user_name.underscore.singularize
56
+ end
57
+
58
+ def user_plural_name
59
+ user_singular_name.pluralize
60
+ end
61
+
62
+ def user_class_name
63
+ user_name.singularize.camelize
64
+ end
65
+
66
+ def user_plural_class_name
67
+ user_plural_name.camelize
68
+ end
69
+
70
+ def destination_path(path)
71
+ File.join(destination_root, path)
72
+ end
73
+
74
+ # FIXME: Should be proxied to ActiveRecord::Generators::Base
75
+ # Implement the required interface for Rails::Generators::Migration.
76
+
77
+ def self.next_migration_number(dirname) #:nodoc:
78
+ if ActiveRecord::Base.timestamped_migrations
79
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
80
+ else
81
+ "%.3d" % (current_migration_number(dirname) + 1)
82
+ end
83
+ end
84
+
85
+
86
+ #
87
+ # def self.banner
88
+ # "#{$0} seivan:#{generator_name} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]"
89
+ # end
90
+
91
+ end
@@ -0,0 +1,23 @@
1
+ class <%= message_class_name %> < ActiveRecord::Base
2
+ belongs_to :<%= user_singular_name %>
3
+
4
+ def recievers
5
+ <%= redis_constant_name %>.smembers(<%= message_class_name %>.receivers_for(self.id))
6
+ end
7
+
8
+ #You want to do this in a background job, may I recommend resque.
9
+ def send_to(receivers_usernames)
10
+ receivers_usernames.each do |username|
11
+ <%= redis_constant_name %>.sadd(<%= message_class_name %>.inbox_for(username), self.id)
12
+ <%= redis_constant_name %>.sadd(<%= message_class_name %>.receivers_for(self.id), username)
13
+ end
14
+ end
15
+
16
+ def self.inbox_for(username)
17
+ ("<%= message_singular_name %>:#{username}:inbox")
18
+ end
19
+
20
+ def self.receivers_for(<%= message_singular_name %>_id)
21
+ ("<%= message_singular_name %>:#{<%= message_singular_name %>_id}:receivers")
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ class Create<%= message_plural_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= message_plural_name %> do |t|
4
+ t.string :title
5
+ t.string :content
6
+ t.boolean :visible
7
+ t.integer :<%= user_singular_name %>_id
8
+ t.timestamps
9
+ end
10
+ end
11
+
12
+ def self.down
13
+ drop_table :<%= message_plural_name %>
14
+ end
15
+ end