streamit 0.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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 Lin He
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,2 @@
1
+ = Streamit (Under development)
2
+
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rake'
10
+ require 'rake/rdoctask'
11
+
12
+ require 'rake/testtask'
13
+
14
+ Rake::TestTask.new(:test) do |t|
15
+ t.libs << 'lib'
16
+ t.libs << 'test'
17
+ t.pattern = 'test/**/*_test.rb'
18
+ t.verbose = false
19
+ end
20
+
21
+ task :default => :test
22
+
23
+ Rake::RDocTask.new(:rdoc) do |rdoc|
24
+ rdoc.rdoc_dir = 'rdoc'
25
+ rdoc.title = 'Streamit'
26
+ rdoc.options << '--line-numbers' << '--inline-source'
27
+ rdoc.rdoc_files.include('README.rdoc')
28
+ rdoc.rdoc_files.include('lib/**/*.rb')
29
+ end
@@ -0,0 +1,35 @@
1
+ class StreamitGenerator < Rails::Generators::NamedBase
2
+
3
+ source_root File.expand_path("../templates", __FILE__)
4
+
5
+ class_option :migration, :type => :boolean, :default => true
6
+
7
+ def invoke_model
8
+ invoke "model", [name].concat(migration_columns),
9
+ :timestamps => false, :test_framework => false, :migration => options.migration?
10
+ end
11
+
12
+ def add_model_config
13
+ inject_into_class "app/models/#{file_name}.rb", class_name, <<-CONTENT
14
+ include Streamit::ORM::#{Rails::Generators.options[:rails][:orm].to_s.camelize}
15
+ CONTENT
16
+ end
17
+
18
+ def add_application_config
19
+ inject_into_class "config/application.rb", "Application", <<-CONTENT
20
+ # Set stream store
21
+ config.streamit.set_store = lambda { ::#{class_name} }
22
+
23
+ CONTENT
24
+ end
25
+
26
+ def create_stream_view
27
+ copy_file "_stream.html.erb", "app/views/streamit/streams/_stream.html.erb"
28
+ end
29
+
30
+ protected
31
+
32
+ def migration_columns
33
+ %w(stream_type:string started_at:datetime actor_id:integer actor_type:string subject_id:integer subject_type:string receiver_id:integer receiver_type:string)
34
+ end
35
+ end
@@ -0,0 +1,4 @@
1
+ TODO:
2
+ <%= content_tag_for(:li, stream) do %>
3
+ <%= stream.actor %> <%= stream.stream_type_in_words %> <%= stream.subject %> to <%= stream.receiver %> <%= time_ago_in_words(stream.started_at) %> ago
4
+ <% end %>
@@ -0,0 +1,57 @@
1
+ module Streamit
2
+ module DSL
3
+ extend ActiveSupport::Concern
4
+
5
+ # stream :create, :actor => :watcher,
6
+ # :receiver => :watched_item
7
+ #
8
+ # stream :update, :attributes => :image_url,
9
+ #
10
+ module ClassMethods
11
+ def stream(action, options={})
12
+ options.assert_valid_keys(:stream_type, :actor, :subject, :receiver, :attributes)
13
+ raise ArgumentError, ":create or :update required as first parameter" \
14
+ unless [:create, :update].any? {|sym| sym == action }
15
+
16
+ table_name = self.model_name.downcase.pluralize
17
+ attributes = options[:attributes]
18
+
19
+ attr_name = if action == :update
20
+ case attributes
21
+ when Array, nil
22
+ :default
23
+ when Symbol
24
+ attributes
25
+ end
26
+ end
27
+
28
+ stream_type = attr_name.nil? ? "streamit.#{table_name}.#{action}" :
29
+ "streamit.#{table_name}.#{action}.#{attr_name}"
30
+
31
+ method_name = :"_#{stream_type.gsub('.', '_')}"
32
+ define_method(method_name) do
33
+ stream_options = [:actor, :receiver, :subject].inject({}) do |memo, sym|
34
+ memo[sym] = options[sym] ? send(options[sym]) : (sym == :actor ? self : nil)
35
+ memo
36
+ end.merge(:stream_type => stream_type, :started_at => Time.now)
37
+
38
+ attr_changed = if action == :update
39
+ case attributes
40
+ when nil
41
+ changed?
42
+ when Array
43
+ attributes.any? { |attr| send(:"#{attr}_changed?") }
44
+ when Symbol
45
+ send(:"#{attributes}_changed?")
46
+ end
47
+ end
48
+
49
+ Streamit.save_stream!(stream_options) if (action != :update || attr_changed)
50
+ end
51
+
52
+ send(:"after_#{action}", method_name)
53
+ end
54
+ end
55
+
56
+ end # DSL
57
+ end # Streamit
@@ -0,0 +1,17 @@
1
+ module Streamit
2
+ class Engine < Rails::Engine
3
+ config.streamit = ActiveSupport::OrderedOptions.new
4
+
5
+ initializer "streamit.active_record" do
6
+ ActiveSupport.on_load(:active_record) do
7
+ include Streamit::DSL
8
+ end
9
+ end
10
+
11
+ initializer "streamit.set_store" do |app|
12
+ if app.config.streamit.set_store
13
+ Streamit.set_store(&app.config.streamit.set_store)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ en:
2
+ streamit:
3
+ users:
4
+ create: "registered"
@@ -0,0 +1,23 @@
1
+ module Streamit
2
+ module ORM
3
+ module ActiveRecord
4
+ extend ActiveSupport::Concern
5
+ include Streamit::Store
6
+
7
+ included do
8
+ belongs_to :actor, :polymorphic => true
9
+ belongs_to :subject, :polymorphic => true
10
+ belongs_to :receiver, :polymorphic => true
11
+
12
+ default_scope order("started_at DESC")
13
+ scope :ago, lambda { |num| where ["started_at > ?", Time.now - num.to_i.seconds] }
14
+ scope :after, lambda { |id| where("id > #{id}") }
15
+ end
16
+
17
+ def save_stream!
18
+ save!
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ module Streamit
2
+ module Store
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def save_stream!(args)
7
+ store = new(args)
8
+ store.save_stream!
9
+ end
10
+ end
11
+
12
+ def stream_type_in_words
13
+ I18n.t(stream_type)
14
+ end
15
+
16
+ def save_stream!
17
+ raise NotImplementedError
18
+ end
19
+ end
20
+ end
data/lib/streamit.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'active_support/core_ext/hash'
2
+
3
+ module Streamit
4
+ autoload :DSL, 'streamit/dsl'
5
+ autoload :Store, 'streamit/store'
6
+
7
+ module ORM
8
+ autoload :ActiveRecord, 'streamit/orm/active_record'
9
+ end
10
+
11
+ # Set which store to use.
12
+ #
13
+ # Streamit.set_store { Stream }
14
+ #
15
+ def self.set_store(&block)
16
+ singleton_class.send :define_method, :store, &block
17
+ end
18
+
19
+ # Place holder for the store.
20
+ def self.store; end
21
+
22
+ def self.save_stream!(args)
23
+ store.save_stream!(args)
24
+ end
25
+ end
26
+
27
+ require 'streamit/engine'
28
+
29
+ require 'active_support/i18n'
30
+ I18n.load_path << File.expand_path('../streamit/locales/en.yml', __FILE__)
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: streamit
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 0
9
+ version: 0.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Lin He
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-07 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Provide feed stream like feature to applications
22
+ email:
23
+ - he9lin@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/generators/streamit_generator.rb
32
+ - lib/generators/templates/_stream.html.erb
33
+ - lib/streamit/dsl.rb
34
+ - lib/streamit/engine.rb
35
+ - lib/streamit/locales/en.yml
36
+ - lib/streamit/orm/active_record.rb
37
+ - lib/streamit/store.rb
38
+ - lib/streamit.rb
39
+ - MIT-LICENSE
40
+ - Rakefile
41
+ - README.rdoc
42
+ has_rdoc: true
43
+ homepage:
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.7
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Provide feed stream like feature
74
+ test_files: []
75
+