Class: Somemoji::EmojiCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/somemoji/emoji_collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(emojis = []) ⇒ EmojiCollection


6
7
8
# File 'lib/somemoji/emoji_collection.rb', line 6

def initialize(emojis = [])
  @emojis = emojis
end

Instance Method Details

#+(emoji_collection) ⇒ Somemoji::EmojiCollection

Compounds two emoji collections into a new one

Examples:

custom_emoji_collection = Somemoji.emoji_collection + Somemoji::EmojiCollection.new(
  [
    Somemoji::Emoji.new(code: "foo"),
    Somemoji::Emoji.new(code: "bar"),
  ]
)
custom_emoji_collection.find_by_code("foo").class #=> Somemoji::Emoji
custom_emoji_collection.find_by_code("bar").class #=> Somemoji::Emoji
custom_emoji_collection.find_by_code("100").class #=> Somemoji::Emoji

23
24
25
# File 'lib/somemoji/emoji_collection.rb', line 23

def +(emoji_collection)
  self.class.new(to_a + emoji_collection.to_a)
end

#character_patternRegexp


28
29
30
# File 'lib/somemoji/emoji_collection.rb', line 28

def character_pattern
  @character_pattern ||= ::Regexp.union(characters)
end

#charactersArray<String>


33
34
35
# File 'lib/somemoji/emoji_collection.rb', line 33

def characters
  index_by_character.keys
end

#code_patternRegexp


38
39
40
# File 'lib/somemoji/emoji_collection.rb', line 38

def code_pattern
  @code_pattern ||= /:(#{::Regexp.union(codes)}):/
end

#codesArray<String>


43
44
45
# File 'lib/somemoji/emoji_collection.rb', line 43

def codes
  index_by_code.keys
end

#each(&block) ⇒ Object

Note:

Implementation for Enumerable


48
49
50
# File 'lib/somemoji/emoji_collection.rb', line 48

def each(&block)
  @emojis.each(&block)
end

#filter_by_category(category) ⇒ Somemoji::EmojiCollection

Returns a emoji collection filtered by a given category pattern

Examples:

Somemoji.emoji_collection.filter_by_category("activity").count #=> 145
Somemoji.emoji_collection.filter_by_category("people").count #=> 570

57
58
59
60
61
62
63
# File 'lib/somemoji/emoji_collection.rb', line 57

def filter_by_category(category)
  ::Somemoji::EmojiCollection.new(
    select do |emoji|
      category === emoji.category
    end
  )
end

#filter_by_keyword(keyword) ⇒ Somemoji::EmojiCollection

Returns a emoji collection filtered by a given keyword pattern

Examples:

Somemoji.emoji_collection.filter_by_keyword("numbers").count #=> 13
Somemoji.emoji_collection.filter_by_keyword("vehicle").count #=> 31

70
71
72
73
74
75
76
77
78
# File 'lib/somemoji/emoji_collection.rb', line 70

def filter_by_keyword(keyword)
  ::Somemoji::EmojiCollection.new(
    select do |emoji|
      emoji.keywords.any? do |emoji_keyword|
        keyword === emoji_keyword
      end
    end
  )
end

#find_by_character(character) ⇒ Somemoji::Emoji?

Finds an emoji from an emoji character (a.k.a. unicode grapheme cluster)

Examples:

Somemoji.emoji_collection.find_by_character("👍").class #=> Somemoji::Emoji
Somemoji.emoji_collection.find_by_character("👎").class #=> Somemoji::Emoji

86
87
88
# File 'lib/somemoji/emoji_collection.rb', line 86

def find_by_character(character)
  index_by_character[character]
end

#find_by_code(code) ⇒ Somemoji::Emoji?

Finds an emoji from an emoji code

Examples:

Somemoji.emoji_collection.find_by_code("thumbsup").class #=> Somemoji::Emoji
Somemoji.emoji_collection.find_by_code("+1").class #=> Somemoji::Emoji
Somemoji.emoji_collection.find_by_code("undefined_code") #=> nil

97
98
99
# File 'lib/somemoji/emoji_collection.rb', line 97

def find_by_code(code)
  index_by_code[code]
end

#replace_character(string, &block) ⇒ String

Replaces emoji characters in a given string with a given block result

Examples:

Somemoji.emoji_collection.replace_character("I ❤ Emoji") do |emoji|
  %(<img alt="#{emoji.character}" class="emoji" src="/images/emoji/#{emoji.base_path}.png">)
end
#=> 'I <img alt="❤" class="emoji" src="/images/emoji/unicode/2764.png"> Emoji'

109
110
111
112
113
# File 'lib/somemoji/emoji_collection.rb', line 109

def replace_character(string, &block)
  string.gsub(character_pattern) do |character|
    block.call(find_by_character(character), character)
  end
end

#replace_code(string, &block) ⇒ String

Replaces emoji codes in a given string with a given block result

Examples:

Somemoji.emoji_collection.replace_code("I :heart: Emoji") do |emoji|
  %(<img alt="#{emoji.character}" class="emoji" src="/images/emoji/#{emoji.base_path}.png">)
end
#=> 'I <img alt="❤" class="emoji" src="/images/emoji/unicode/2764.png"> Emoji'

123
124
125
126
127
# File 'lib/somemoji/emoji_collection.rb', line 123

def replace_code(string, &block)
  string.gsub(code_pattern) do |matched_string|
    block.call(find_by_code(::Regexp.last_match(1)), matched_string)
  end
end