Module: Emoji

Extended by:
Emoji
Included in:
Emoji
Defined in:
lib/emoji.rb,
lib/emoji/character.rb

Defined Under Namespace

Classes: Character

Constant Summary collapse

NotFound =
Class.new(IndexError)

Instance Method Summary collapse

Instance Method Details

#allObject



17
18
19
20
21
22
# File 'lib/emoji.rb', line 17

def all
  return @all if defined? @all
  @all = []
  parse_data_file
  @all
end

#create(name) ⇒ Object

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



26
27
28
29
30
# File 'lib/emoji.rb', line 26

def create(name)
  emoji = Emoji::Character.new(name)
  self.all << edit_emoji(emoji) { yield emoji if block_given? }
  emoji
end

#data_fileObject



9
10
11
# File 'lib/emoji.rb', line 9

def data_file
  File.expand_path('../../db/emoji.json', __FILE__)
end

#edit_emoji(emoji) {|emoji| ... } ⇒ Object

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

Yields:

  • (emoji)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/emoji.rb', line 34

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) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/emoji.rb', line 50

def find_by_alias(name)
  names_index.fetch(name) {
    if block_given? then yield name
    else raise NotFound, "Emoji not found by name: %s" % name.inspect
    end
  }
end

#find_by_unicode(unicode) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/emoji.rb', line 58

def find_by_unicode(unicode)
  unicodes_index.fetch(unicode) {
    if block_given? then yield unicode
    else raise NotFound, "Emoji not found from unicode: %s" % Emoji::Character.hex_inspect(unicode)
    end
  }
end

#images_pathObject



13
14
15
# File 'lib/emoji.rb', line 13

def images_path
  File.expand_path("../../images", __FILE__)
end