emojimmy 0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c98273697d88126f8b5e4c7eab080ffe7da2d4b6
4
- data.tar.gz: dd302c23d485e6f963f19d5f1d0114a044a6e404
3
+ metadata.gz: ddff8b1900275b1b84108e6c5618684c1b83960a
4
+ data.tar.gz: bb3395a5113d4da945df785e92d4ddcb0d5cedb6
5
5
  SHA512:
6
- metadata.gz: df29c9cda1078629248d5f72b1cf1367c0215d47a0dc52decddf69c480e451a51351f57a4cbf45cc7439d49f7946916728c20952124ef7e3ebe02dbe01731c5f
7
- data.tar.gz: 9cafb78e4d067bcd4d19caa4b8dc8e657d2523411fc325884264782d6d6da1bfef2a201e6ecd397d5551103fecc7466f35c584e05464d9e0fdec4761dafbb85f
6
+ metadata.gz: 3a64f89dfcc71793fab9043b9d9832580762ee7516ccb8389f160af966279efcac2637ddc3f6b63a2e97ec239657b380e9e47e0bce972b69ad427d1e030f15ee
7
+ data.tar.gz: d3731889f19f1a9d9bea442bbaa0c7e56b2590977d1e681cf15be144ef7cb29b40b9b14c4b9e6718ca3ddd892dea712e2af17cedebef9828b0fe2f95b6687342
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/emojimmy.gemspec CHANGED
@@ -18,8 +18,11 @@ 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 'activerecord', '>= 3.0.0'
22
+
21
23
  spec.add_development_dependency 'bundler'
22
24
  spec.add_development_dependency 'rake'
23
25
  spec.add_development_dependency 'rspec'
24
26
  spec.add_development_dependency 'coveralls'
27
+ spec.add_development_dependency 'sqlite3'
25
28
  end
@@ -0,0 +1,19 @@
1
+ module Emojimmy
2
+ module Mixin
3
+ def self.inject_methods(model, attributes)
4
+ attributes.each do |attribute|
5
+ model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
6
+ # Before saving the record, convert the attribute value
7
+ before_save do
8
+ self.#{attribute} = Emojimmy.emoji_to_text(self.#{attribute})
9
+ end
10
+
11
+ # When calling the attribute name, convert its value
12
+ def #{attribute}
13
+ Emojimmy.text_to_emoji(read_attribute(#{attribute.inspect}))
14
+ end
15
+ RUBY
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module Emojimmy
2
- VERSION = '0.1'
2
+ VERSION = '0.1.1'
3
3
  end
data/lib/emojimmy.rb CHANGED
@@ -1,5 +1,11 @@
1
1
  require 'emojimmy/version'
2
2
 
3
+ # Dependencies
4
+ require 'active_record'
5
+
6
+ # Modules
7
+ require 'emojimmy/mixin'
8
+
3
9
  module Emojimmy
4
10
  # Load emoji data from config/emoji.txt and build the `text_to_emoji`
5
11
  # and `emoji_to_text` hash tables
@@ -35,3 +41,12 @@ module Emojimmy
35
41
  end
36
42
  end
37
43
  end
44
+
45
+ class ActiveRecord::Base
46
+ def self.stores_emoji_characters(options = {})
47
+ return unless table_exists?
48
+
49
+ options[:in] ||= []
50
+ Emojimmy::Mixin.inject_methods(self, options[:in])
51
+ end
52
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe Emojimmy::Mixin do
4
+ describe :inject_methods do
5
+ before do
6
+ run_migration do
7
+ create_table(:comments, force: true) do |t|
8
+ t.text :body
9
+ end
10
+ end
11
+
12
+ spawn_emojimmy_model 'Comment', in: [:body]
13
+ end
14
+
15
+ describe :InstanceMethod do
16
+ subject { Comment.create(body: body) }
17
+
18
+ context 'with a single emoji' do
19
+ let(:body) { "Hello, \xF0\x9F\x98\x81 world!" }
20
+ it { should be_persisted }
21
+ its(:body) { should eql body }
22
+ end
23
+
24
+ context 'with multiple emoji' do
25
+ let(:body) { "Hello, \xF0\x9F\x98\x81\xF0\x9F\x98\x81 \xF0\x9F\x98\x8D world!" }
26
+ it { should be_persisted }
27
+ its(:body) { should eql body }
28
+ end
29
+
30
+ context 'without any emoji' do
31
+ let(:body) { "Hello, boring world!" }
32
+ it { should be_persisted }
33
+ its(:body) { should eql body }
34
+ end
35
+ end
36
+
37
+ describe :Callback do
38
+ subject { Comment.create(body: body) }
39
+ let(:persisted_body) { subject.read_attribute(:body) }
40
+
41
+ context 'with a single emoji' do
42
+ let(:body) { "Hello, \xF0\x9F\x98\x81 world!" }
43
+ it { should be_persisted }
44
+ it { expect(persisted_body).to eql "Hello, {U+1F601} world!" }
45
+ end
46
+
47
+ context 'with multiple emoji' do
48
+ let(:body) { "Hello, \xF0\x9F\x98\x81\xF0\x9F\x98\x81 \xF0\x9F\x98\x8D world!" }
49
+ it { should be_persisted }
50
+ it { expect(persisted_body).to eql "Hello, {U+1F601}{U+1F601} {U+1F60D} world!" }
51
+ end
52
+
53
+ context 'without any emoji' do
54
+ let(:body) { "Hello, boring world!" }
55
+ it { should be_persisted }
56
+ it { expect(persisted_body).to eql "Hello, boring world!" }
57
+ end
58
+ end
59
+ end
60
+ end
data/spec/spec_helper.rb CHANGED
@@ -4,10 +4,23 @@ require 'coveralls'
4
4
  Coveralls.wear!
5
5
 
6
6
  require 'rspec'
7
+ require 'sqlite3'
7
8
  require 'emojimmy'
8
9
 
10
+ # Require our macros and extensions
11
+ Dir[File.expand_path('../../spec/support/macros/**/*.rb', __FILE__)].map(&method(:require))
12
+
9
13
  RSpec.configure do |config|
14
+ # Include our macros
15
+ config.include DatabaseMacros
16
+ config.include ModelMacros
17
+
10
18
  config.before :suite do
11
19
  Emojimmy.initialize!
12
20
  end
21
+
22
+ config.before :each do
23
+ adapter = ENV['DB_ADAPTER'] || 'sqlite3'
24
+ setup_database(adapter: adapter, database: 'emojimmy_test')
25
+ end
13
26
  end
@@ -0,0 +1,9 @@
1
+ class DatabaseAdapter
2
+ def initialize(opts = {})
3
+ @database = opts[:database]
4
+ end
5
+
6
+ def establish_connection!
7
+ ActiveRecord::Base.establish_connection(database_configuration)
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ require_relative 'database_adapter'
2
+
3
+ class Sqlite3Adapter < DatabaseAdapter
4
+ def database_configuration
5
+ {
6
+ adapter: 'sqlite3',
7
+ database: ':memory:'
8
+ }
9
+ end
10
+
11
+ def reset_database!
12
+ ActiveRecord::Base.connection.execute("select 'drop table ' || name || ';' from sqlite_master where type = 'table';")
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ module DatabaseMacros
2
+ # Run migrations in the test database
3
+ def run_migration(&block)
4
+ # Create a new migration class
5
+ klass = Class.new(ActiveRecord::Migration)
6
+
7
+ # Create a new `up` that executes the argument
8
+ klass.send(:define_method, :up) { self.instance_exec(&block) }
9
+
10
+ # Create a new instance of it and execute its `up` method
11
+ klass.new.up
12
+ end
13
+
14
+ def setup_database(opts = {})
15
+ adapter = "#{opts[:adapter].capitalize}Adapter".constantize.new(database: opts[:database])
16
+ adapter.establish_connection!
17
+ adapter.reset_database!
18
+
19
+ # Silence everything
20
+ ActiveRecord::Base.logger = ActiveRecord::Migration.verbose = false
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ module ModelMacros
2
+ # Create a new Emojimmy-powered model
3
+ def spawn_emojimmy_model(klass_name, options = {}, &block)
4
+ spawn_model klass_name do
5
+ stores_emoji_characters options
6
+ instance_exec(&block) if block
7
+ end
8
+ end
9
+
10
+ protected
11
+
12
+ # Create a new model class
13
+ def spawn_model(klass_name, parent_klass = ActiveRecord::Base, &block)
14
+ Object.instance_eval { remove_const klass_name } if Object.const_defined?(klass_name)
15
+ Object.const_set(klass_name, Class.new(parent_klass))
16
+ Object.const_get(klass_name).class_eval(&block) if block_given?
17
+ end
18
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: emojimmy
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rémi Prévost
@@ -10,6 +10,20 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2013-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +80,20 @@ dependencies:
66
80
  - - '>='
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
69
97
  description: Emojimmy makes it possible to store emoji characters in datastore that
70
98
  don’t support 4-Byte UTF-8 Unicode encoding.
71
99
  email:
@@ -75,6 +103,7 @@ extensions: []
75
103
  extra_rdoc_files: []
76
104
  files:
77
105
  - .gitignore
106
+ - .rspec
78
107
  - .travis.yml
79
108
  - Gemfile
80
109
  - LICENSE.md
@@ -85,9 +114,15 @@ files:
85
114
  - gemfiles/Gemfile.activerecord-3.2.x
86
115
  - gemfiles/Gemfile.activerecord-4.0
87
116
  - lib/emojimmy.rb
117
+ - lib/emojimmy/mixin.rb
88
118
  - lib/emojimmy/version.rb
119
+ - spec/emojimmy/mixin_spec.rb
89
120
  - spec/emojimmy_spec.rb
90
121
  - spec/spec_helper.rb
122
+ - spec/support/macros/database/database_adapter.rb
123
+ - spec/support/macros/database/sqlite3_adapter.rb
124
+ - spec/support/macros/database_macros.rb
125
+ - spec/support/macros/model_macros.rb
91
126
  homepage: http://open.mirego.com/emojimmy
92
127
  licenses:
93
128
  - BSD 3-Clause
@@ -114,5 +149,10 @@ specification_version: 4
114
149
  summary: Emojimmy makes it possible to store emoji characters in datastore that don’t
115
150
  support 4-Byte UTF-8 Unicode encoding.
116
151
  test_files:
152
+ - spec/emojimmy/mixin_spec.rb
117
153
  - spec/emojimmy_spec.rb
118
154
  - spec/spec_helper.rb
155
+ - spec/support/macros/database/database_adapter.rb
156
+ - spec/support/macros/database/sqlite3_adapter.rb
157
+ - spec/support/macros/database_macros.rb
158
+ - spec/support/macros/model_macros.rb