Permalink
Browse files

Change emoji creation API to take name as 1st argument

It's more likely that someone will want to add a new emoji with a custom
image rather than a character that has a Unicode representation.

Also move the VARIATION_SELECTOR_16 logic outside of Emoji::Character
since it doesn't need to be concerned with it.
  • Loading branch information...
1 parent 65031cf commit 3935375ca8472cb34b6cbab92baa0180167ce6f9 @mislav mislav committed Jul 3, 2014
Showing with 31 additions and 32 deletions.
  1. +6 −6 README.md
  2. +5 −4 db/dump.rb
  3. +14 −4 lib/emoji.rb
  4. +4 −16 lib/emoji/character.rb
  5. +2 −2 test/emoji_test.rb
View
@@ -85,19 +85,19 @@ Adding new emoji
You can add new emoji characters to the `Emoji.all` list:
```ruby
-emoji = Emoji.create("\u{266b}") do |char|
- char.add_alias "music"
- char.add_unicode_alias "\u{266a}"
+emoji = Emoji.create("music") do |char|
+ char.add_alias "song"
+ char.add_unicode_alias "\u{266b}"
char.add_tag "notes"
end
emoji.name #=> "music"
emoji.raw #=> "♫"
emoji.image_filename #=> "unicode/266b.png"
-# Creating custom emoji (not in Unicode spec):
-emoji = Emoji.create(nil) do |char|
- char.add_alias "music"
+# Creating custom emoji (no Unicode aliases):
+emoji = Emoji.create("music") do |char|
+ char.add_tag "notes"
end
emoji.custom? #=> true
View
@@ -52,17 +52,18 @@ def add_reference(code)
trap(:PIPE) { abort }
items = []
+variation = Emoji::VARIATION_SELECTOR_16
for emoji in Emoji.all
- unicodes = emoji.unicode_aliases
- unicodes = unicodes[1..-1] if emoji.variation?
+ unicodes = emoji.unicode_aliases.dup
item = {}
unless emoji.custom?
- variation_codepoint = Emoji::Character::VARIATION_SELECTOR_16.codepoints[0]
+ variation_codepoint = variation.codepoints[0]
chars = emoji.raw.codepoints.map { |code| UnicodeCharacter.fetch(code) unless code == variation_codepoint }.compact
- item[:emoji] = emoji.raw
+ unicodes.select { |u| u.index(variation) }.each { |u| unicodes.delete(u.sub(variation, '')) }
+ item[:emoji] = unicodes.shift
item[:unicodes] = unicodes if unicodes.any?
item[:description] = chars.map(&:description).join(' + ')
end
View
@@ -23,8 +23,8 @@ def all
# Public: Initialize an Emoji::Character instance and yield it to the block.
# The character is added to the `Emoji.all` set.
- def create(raw)
- emoji = Emoji::Character.new(raw)
+ def create(name)
+ emoji = Emoji::Character.new(name)
self.all << edit_emoji(emoji) { yield emoji }
emoji
end
@@ -64,12 +64,22 @@ def find_by_unicode(unicode)
end
private
+ VARIATION_SELECTOR_16 = "\u{fe0f}".freeze
+
def parse_data_file
raw = File.open(data_file, 'r:UTF-8') { |data| JSON.parse(data.read) }
raw.each do |raw_emoji|
- self.create(raw_emoji['emoji']) do |emoji|
+ self.create(nil) do |emoji|
raw_emoji.fetch('aliases').each { |name| emoji.add_alias(name) }
- raw_emoji.fetch('unicodes', []).each { |uni| emoji.add_unicode_alias(uni) }
+ unicodes = Array(raw_emoji['emoji']) + raw_emoji.fetch('unicodes', [])
+ unicodes.each { |uni|
+ emoji.add_unicode_alias(uni)
+ # Automatically add a representation of this emoji without the variation
+ # selector to unicode aliases:
+ if uni.index(VARIATION_SELECTOR_16)
+ emoji.add_unicode_alias(uni.sub(VARIATION_SELECTOR_16, ''))
+ end
+ }
raw_emoji.fetch('tags').each { |tag| emoji.add_tag(tag) }
end
end
View
@@ -1,7 +1,5 @@
module Emoji
class Character
- VARIATION_SELECTOR_16 = "\u{fe0f}".freeze
-
# Inspect individual Unicode characters in a string by dumping its
# codepoints in hexadecimal format.
def self.hex_inspect(str)
@@ -20,9 +18,7 @@ def add_alias(name)
aliases << name
end
- # A list of Unicode strings that uniquely refer to this emoji. By default,
- # this list includes the emoji's Unicode representation without the
- # variation selector character.
+ # A list of Unicode strings that uniquely refer to this emoji.
attr_reader :unicode_aliases
# Raw Unicode string for an emoji. Nil if emoji is non-standard.
@@ -40,25 +36,17 @@ def add_tag(tag)
tags << tag
end
- def initialize(raw)
- @aliases = []
- @unicode_aliases = Array(raw)
+ def initialize(name)
+ @aliases = Array(name)
+ @unicode_aliases = []
@tags = []
-
- # Automatically add a representation of this emoji without the variation
- # selector to unicode aliases:
- add_unicode_alias(raw.sub(VARIATION_SELECTOR_16, '')) if variation?
end
def inspect
hex = '(%s)' % hex_inspect unless custom?
%(#<#{self.class.name}:#{name}#{hex}>)
end
- def variation?
- !custom? && raw.index(VARIATION_SELECTOR_16)
- end
-
def hex_inspect
self.class.hex_inspect(raw)
end
View
@@ -86,8 +86,8 @@ class EmojiTest < TestCase
end
test "create" do
- emoji = Emoji.create("\u{266b}") do |char|
- char.add_alias "music"
+ emoji = Emoji.create("music") do |char|
+ char.add_unicode_alias "\u{266b}"
char.add_unicode_alias "\u{266a}"
char.add_tag "notes"
char.add_tag "eighth"

0 comments on commit 3935375

Please sign in to comment.