module Emoji

Constants

VARIATION_SELECTOR_16

Public Instance Methods

all() click to toggle source
# File lib/emoji.rb, line 15
def all
  return @all if defined? @all
  @all = []
  parse_data_file
  @all
end
create(name) { |emoji| ... } click to toggle source

Public: Initialize an Emoji::Character instance and yield it to the block. The character is added to the `Emoji.all` set.

# File lib/emoji.rb, line 24
def create(name)
  emoji = Emoji::Character.new(name)
  self.all << edit_emoji(emoji) { yield emoji if block_given? }
  emoji
end
data_file() click to toggle source
# File lib/emoji.rb, line 7
def data_file
  File.expand_path('../../db/emoji.json', __FILE__)
end
edit_emoji(emoji) { |emoji| ... } click to toggle source

Public: Yield an emoji to the block and update the indices in case its aliases or unicode_aliases lists changed.

# File lib/emoji.rb, line 32
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
find_by_alias(name) click to toggle source

Public: Find an emoji by its aliased name. Return nil if missing.

# File lib/emoji.rb, line 49
def find_by_alias(name)
  names_index[name]
end
find_by_unicode(unicode) click to toggle source

Public: Find an emoji by its unicode character. Return nil if missing.

# File lib/emoji.rb, line 54
def find_by_unicode(unicode)
  unicodes_index[unicode]
end
images_path() click to toggle source
# File lib/emoji.rb, line 11
def images_path
  File.expand_path("../../images", __FILE__)
end

Private Instance Methods

names_index() click to toggle source
# File lib/emoji.rb, line 75
def names_index
  all unless defined? @all
  @names_index
end
parse_data_file() click to toggle source
# File lib/emoji.rb, line 61
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) }
      if raw = raw_emoji['emoji']
        unicodes = [raw, raw.sub(VARIATION_SELECTOR_16, '') + VARIATION_SELECTOR_16].uniq
        unicodes.each { |uni| emoji.add_unicode_alias(uni) }
      end
      raw_emoji.fetch('tags').each { |tag| emoji.add_tag(tag) }
    end
  end
end
unicodes_index() click to toggle source
# File lib/emoji.rb, line 80
def unicodes_index
  all unless defined? @all
  @unicodes_index
end