Module: Ballast::Emoji::Utils

Defined in:
lib/ballast/emoji.rb

Overview

General utility methods.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Proc) url_mapper

Returns the URL mapper for the emojis.

Returns:

  • (Proc)

    The current URL mapper or a default one (which will return the relative URL unmodified).



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ballast/emoji.rb', line 13

module Utils
  attr_accessor :url_mapper

  # Returns the regular expression which matches all the known emojis.
  #
  # @return [Regexp] The regular expression which matches all the known emojis.
  def replace_regex
    @replace_regex ||= /(#{::Emoji.send(:unicodes_index).keys.join("|")})/
  end

  # Replaces all the emojis in the text using the requested mod.
  #
  # @param text [String] The text to manipulate.
  # @param mode [Symbol] The method to use when replacing icons.
  # @param options [Hash] The options to pass to the replacing method.
  # @return [String] The text with all emojis replaced.
  def replace(text, mode: :html, **options)
    mode = :markup unless mode && ::Emoji::Character.new(nil).respond_to?(mode)
    text.ensure_string.gsub(replace_regex) { invoke(::Emoji.find_by_unicode(Regexp.last_match[1]), mode, options) }
  end

  # Lists all the emoji known in a hash.
  #
  # @param keys_method [Symbol] The method to use for keys.
  # @param values_method [Symbol] The method to use for values.
  # @param options [Hash] The options to pass to all methods.
  # @return [Hash] A hash of all known emojis.
  def enumerate(keys_method: :markup, values_method: :html, **options)
    tester = ::Emoji::Character.new(nil)
    keys_method = :markup unless keys_method && tester.respond_to?(keys_method)
    values_method = :html unless values_method && tester.respond_to?(values_method)

    ::Emoji.all.reduce({}) { |accu, icon|
      accu[invoke(icon, keys_method, options)] = invoke(icon, values_method, options)
      accu
    }
  end

  # Returns the URL mapper for the emojis.
  #
  # @return [Proc] The current URL mapper or a default one (which will return the relative URL unmodified).
  def url_mapper
    @url_mapper || ->(url) { url }
  end

  # Returns a absolute URL for a emoji image.
  #
  # @param image [String] The relative URL of the emoji filename.
  # @return [String] The absolute URL of the emoji filename.
  def url_for(image)
    url_mapper.call(image)
  end

  private

  # :nodoc:
  def invoke(subject, method, options)
    subject.method(method).arity == 1 ? subject.send(method, options) : subject.send(method)
  end
end

Instance Method Details

- (Hash) enumerate(keys_method: :markup, values_method: :html, **options)

Lists all the emoji known in a hash.

Parameters:

  • keys_method (Symbol)

    The method to use for keys.

  • values_method (Symbol)

    The method to use for values.

  • options (Hash)

    The options to pass to all methods.

Returns:

  • (Hash)

    A hash of all known emojis.



40
41
42
43
44
45
46
47
48
49
# File 'lib/ballast/emoji.rb', line 40

def enumerate(keys_method: :markup, values_method: :html, **options)
  tester = ::Emoji::Character.new(nil)
  keys_method = :markup unless keys_method && tester.respond_to?(keys_method)
  values_method = :html unless values_method && tester.respond_to?(values_method)

  ::Emoji.all.reduce({}) { |accu, icon|
    accu[invoke(icon, keys_method, options)] = invoke(icon, values_method, options)
    accu
  }
end

- (String) replace(text, mode: :html, **options)

Replaces all the emojis in the text using the requested mod.

Parameters:

  • text (String)

    The text to manipulate.

  • mode (Symbol)

    The method to use when replacing icons.

  • options (Hash)

    The options to pass to the replacing method.

Returns:

  • (String)

    The text with all emojis replaced.



29
30
31
32
# File 'lib/ballast/emoji.rb', line 29

def replace(text, mode: :html, **options)
  mode = :markup unless mode && ::Emoji::Character.new(nil).respond_to?(mode)
  text.ensure_string.gsub(replace_regex) { invoke(::Emoji.find_by_unicode(Regexp.last_match[1]), mode, options) }
end

- (Regexp) replace_regex

Returns the regular expression which matches all the known emojis.

Returns:

  • (Regexp)

    The regular expression which matches all the known emojis.



19
20
21
# File 'lib/ballast/emoji.rb', line 19

def replace_regex
  @replace_regex ||= /(#{::Emoji.send(:unicodes_index).keys.join("|")})/
end

- (String) url_for(image)

Returns a absolute URL for a emoji image.

Parameters:

  • image (String)

    The relative URL of the emoji filename.

Returns:

  • (String)

    The absolute URL of the emoji filename.



62
63
64
# File 'lib/ballast/emoji.rb', line 62

def url_for(image)
  url_mapper.call(image)
end