Files
gemoji/lib/emoji.rb
Jeremy Kemper f9aba615af Change find_by_* to return nil rather than raise an exception.
This is a breaking API change.

You probably have code that looks like
  if emoji = Emoji.find_by_alias($1) { nil }
that you can change to
  if emoji = Emoji.find_by_alias($1)
2014-07-18 09:16:21 -07:00

90 lines
2.2 KiB
Ruby

require 'emoji/character'
require 'json'
module Emoji
extend self
def data_file
File.expand_path('../../db/emoji.json', __FILE__)
end
def images_path
File.expand_path("../../images", __FILE__)
end
def all
return @all if defined? @all
@all = []
parse_data_file
@all
end
# Public: Initialize an Emoji::Character instance and yield it to the block.
# The character is added to the `Emoji.all` set.
def create(name)
emoji = Emoji::Character.new(name)
self.all << edit_emoji(emoji) { yield emoji if block_given? }
emoji
end
# Public: Yield an emoji to the block and update the indices in case its
# aliases or unicode_aliases lists changed.
def edit_emoji(emoji)
@names_index ||= Hash.new
@unicodes_index ||= Hash.new
yield emoji
emoji.aliases.each do |name|
@names_index[name] = emoji
end
emoji.unicode_aliases.each do |unicode|
@unicodes_index[unicode] = emoji
end
emoji
end
# Public: Find an emoji by its aliased name. Return nil if missing.
def find_by_alias(name)
names_index[name]
end
# Public: Find an emoji by its unicode character. Return nil if missing.
def find_by_unicode(unicode)
unicodes_index[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(nil) do |emoji|
raw_emoji.fetch('aliases').each { |name| emoji.add_alias(name) }
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
end
def names_index
all unless defined? @all
@names_index
end
def unicodes_index
all unless defined? @all
@unicodes_index
end
end