emotions 0.0.1 → 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,2 @@
1
1
  source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in emotions.gemspec
4
2
  gemspec
data/README.md CHANGED
@@ -1,29 +1,71 @@
1
1
  # Emotions
2
2
 
3
- TODO: Write a gem description
3
+ Emotions is a Ruby library that allows ActiveRecord records to express (and hopefully store) emotions about other records.
4
4
 
5
5
  ## Installation
6
6
 
7
- Add this line to your application's Gemfile:
7
+ Add this line to your applications Gemfile:
8
8
 
9
- gem 'emotions'
9
+ ```ruby
10
+ gem 'emotions'
11
+ ```
10
12
 
11
13
  And then execute:
12
14
 
13
- $ bundle
15
+ ```bash
16
+ $ bundle
17
+ ```
14
18
 
15
- Or install it yourself as:
19
+ Run the migration to add the `emotions` table:
16
20
 
17
- $ gem install emotions
21
+ ```bash
22
+ $ rails generate emotions:install
23
+ ```
18
24
 
19
25
  ## Usage
20
26
 
21
- TODO: Write usage instructions here
27
+ Configure the allowed emotions.
22
28
 
23
- ## Contributing
29
+ ```ruby
30
+ Emotions.configure do |config|
31
+ config.emotions = [:happy, :sad]
32
+ end
33
+ ```
24
34
 
25
- 1. Fork it
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
35
+ Create a couple of models.
36
+
37
+ ```ruby
38
+ class Picture < ActiveRecord::Base
39
+ acts_as_emotive
40
+ end
41
+
42
+ class User < ActiveRecord::Base
43
+ acts_as_emotional
44
+ end
45
+ ```
46
+
47
+ Express emotions towards other records.
48
+
49
+ ```ruby
50
+ user = User.find(1)
51
+ picture = Picture.find(2)
52
+
53
+ user.happy_about!(picture)
54
+ user.sad_about!(picture)
55
+ user.emotions_about(picture)
56
+ # => [:happy, :sad]
57
+
58
+ user.happy_about?(picture)
59
+ # => true
60
+
61
+ user.no_longer_sad_about!(picture)
62
+ user.sad_about?(picture)
63
+ # => false
64
+
65
+ picture.happy_about.map(&:emotional)
66
+ # => [#<User id=1>]
67
+
68
+ user.express!(:sad, picture)
69
+ user.sad_about?(picure)
70
+ # => true
71
+ ```
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Emotions::VERSION
9
9
  spec.authors = ["Rémi Prévost"]
10
10
  spec.email = ["remi@exomel.com"]
11
- spec.description = ""
12
- spec.summary = ""
13
- spec.homepage = ""
11
+ spec.description = 'Emotions is a Ruby library that allows ActiveRecord records to express (and hopefully store) emotions about other records.'
12
+ spec.summary = 'Emotions is a Ruby library that allows ActiveRecord records to express (and hopefully store) emotions about other records.'
13
+ spec.homepage = 'https://github.com/remiprev/emotions'
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "rails", ">= 3.0.0"
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.3"
22
24
  spec.add_development_dependency "rake"
23
25
  end
@@ -1,5 +1,17 @@
1
1
  require "emotions/version"
2
+ require "emotions/emotion"
3
+ require "emotions/emotive"
4
+ require "emotions/emotional"
2
5
 
3
6
  module Emotions
4
- # Your code goes here...
7
+ def self.configure
8
+ @configuration = OpenStruct.new
9
+ yield(@configuration)
10
+ end
11
+
12
+ def self.emotions
13
+ @configuration.emotions ||= []
14
+ end
5
15
  end
16
+
17
+ require 'emotions/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
@@ -0,0 +1,16 @@
1
+ module Emotions
2
+ class Emotion < ActiveRecord::Base
3
+ self.table_name = 'emotions'
4
+
5
+ # Validations
6
+ validates :emotional, presence: true
7
+ validates :emotive, presence: true
8
+ validates_each :emotion do |record, attr, value|
9
+ record.errors.add attr, 'invalid' unless Emotions.emotions.include?(value.try(:to_sym))
10
+ end
11
+
12
+ # Associations
13
+ belongs_to :emotional, polymorphic: true
14
+ belongs_to :emotive, polymorphic: true
15
+ end
16
+ end
@@ -0,0 +1,53 @@
1
+ module Emotions
2
+ module Emotional
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def acts_as_emotional
7
+ has_many :emotions, as: :emotional, class_name: 'Emotions::Emotion'
8
+
9
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
10
+ def _emotions_about(emotive)
11
+ self.emotions.where(emotive_id: emotive.id, emotive_type: emotive.class.name)
12
+ end
13
+
14
+ def emotions_about(emotive)
15
+ _emotions_about(emotive).map(&:emotion).map(&:to_sym)
16
+ end
17
+
18
+ def express!(emotion, emotive)
19
+ send("\#{emotion}_about!", emotive)
20
+ end
21
+
22
+ def no_longer_express!(emotion, emotive)
23
+ send("no_longer_\#{emotion}_about!", emotive)
24
+ end
25
+ RUBY
26
+
27
+ Emotions.emotions.each do |emotion|
28
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
29
+ def #{emotion}_about?(emotive)
30
+ #{emotion}_about(emotive).exists?
31
+ end
32
+
33
+ def #{emotion}_about?(emotive)
34
+ #{emotion}_about(emotive).exists?
35
+ end
36
+
37
+ def #{emotion}_about!(emotive)
38
+ #{emotion}_about(emotive).first_or_create!
39
+ end
40
+
41
+ def no_longer_#{emotion}_about!(emotive)
42
+ #{emotion}_about(emotive).destroy
43
+ end
44
+
45
+ def #{emotion}_about(emotive)
46
+ _emotions_about(emotive).where(emotion: #{emotion.to_s.inspect})
47
+ end
48
+ RUBY
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,25 @@
1
+ module Emotions
2
+ module Emotive
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def acts_as_emotive
7
+ has_many :emotions, as: :emotive, class_name: 'Emotions::Emotion'
8
+
9
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
10
+ def emotional_about
11
+ self.emotions.includes(:emotional)
12
+ end
13
+ RUBY
14
+
15
+ Emotions.emotions.each do |emotion|
16
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
17
+ def #{emotion}_about
18
+ emotional_about.where(emotion: #{emotion.to_s.inspect})
19
+ end
20
+ RUBY
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ require 'emotions'
2
+ require 'rails'
3
+
4
+ module Emotions
5
+ class Railtie < Rails::Railtie
6
+ initializer "emotions.active_record" do |app|
7
+ ActiveSupport.on_load :active_record do
8
+ include Emotions::Emotive
9
+ include Emotions::Emotional
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module Emotions
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1"
3
3
  end
@@ -0,0 +1,3 @@
1
+ Description:
2
+ The emotions:install generator generates a migration that will
3
+ create the `emotions` table in your database.
@@ -0,0 +1,25 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module Emotions
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ # Implement the required interface for Rails::Generators::Migration.
11
+ # taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
12
+ def self.next_migration_number(dirname)
13
+ if ActiveRecord::Base.timestamped_migrations
14
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
15
+ else
16
+ "%.3d" % (current_migration_number(dirname) + 1)
17
+ end
18
+ end
19
+
20
+ def create_migration_file
21
+ migration_template 'migration.rb', 'db/migrate/emotions_migration.rb'
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ class AddEmotions < ActiveRecord::Migration
2
+ def up
3
+ create_table :emotions do |t|
4
+ # The emotional model
5
+ t.string :emotional_type
6
+ t.integer :emotional_id
7
+
8
+ # The emotive model
9
+ t.string :emotive_type
10
+ t.integer :emotive_id
11
+
12
+ # The type of emotion
13
+ t.string :emotion
14
+
15
+ t.timestamps
16
+ end
17
+
18
+ add_index :emotions, [:emotional_type, :emotional_id, :emotive_type, :emotive_id, :emotion], unique: true, name: 'index_emotions_by_emotional_emotive_and_emotion'
19
+ add_index :emotions, [:emotional_type, :emotional_id, :emotive_type, :emotive_id], name: 'index_emotions_by_emotional_and_emotive'
20
+ add_index :emotions, [:emotive_type, :emotive_id, :emotion], name: 'index_emotions_by_emotional_and_emotive'
21
+ end
22
+
23
+ def down
24
+ drop_table :emotions
25
+ end
26
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: emotions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: '0.1'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,6 +11,22 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2013-05-20 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
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.0.0
14
30
  - !ruby/object:Gem::Dependency
15
31
  name: bundler
16
32
  requirement: !ruby/object:Gem::Requirement
@@ -43,7 +59,8 @@ dependencies:
43
59
  - - ! '>='
44
60
  - !ruby/object:Gem::Version
45
61
  version: '0'
46
- description: ''
62
+ description: Emotions is a Ruby library that allows ActiveRecord records to express
63
+ (and hopefully store) emotions about other records.
47
64
  email:
48
65
  - remi@exomel.com
49
66
  executables: []
@@ -57,8 +74,15 @@ files:
57
74
  - Rakefile
58
75
  - emotions.gemspec
59
76
  - lib/emotions.rb
77
+ - lib/emotions/emotion.rb
78
+ - lib/emotions/emotional.rb
79
+ - lib/emotions/emotive.rb
80
+ - lib/emotions/railtie.rb
60
81
  - lib/emotions/version.rb
61
- homepage: ''
82
+ - lib/generators/emotions/USAGE
83
+ - lib/generators/emotions/install_generator.rb
84
+ - lib/generators/emotions/templates/migration.rb
85
+ homepage: https://github.com/remiprev/emotions
62
86
  licenses:
63
87
  - MIT
64
88
  post_install_message:
@@ -73,7 +97,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
73
97
  version: '0'
74
98
  segments:
75
99
  - 0
76
- hash: 3174883291997258005
100
+ hash: -16871718108920318
77
101
  required_rubygems_version: !ruby/object:Gem::Requirement
78
102
  none: false
79
103
  requirements:
@@ -82,11 +106,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
106
  version: '0'
83
107
  segments:
84
108
  - 0
85
- hash: 3174883291997258005
109
+ hash: -16871718108920318
86
110
  requirements: []
87
111
  rubyforge_project:
88
112
  rubygems_version: 1.8.23
89
113
  signing_key:
90
114
  specification_version: 3
91
- summary: ''
115
+ summary: Emotions is a Ruby library that allows ActiveRecord records to express (and
116
+ hopefully store) emotions about other records.
92
117
  test_files: []