Sha256: 5daac341f39573af4e820c49fe2029fa4e695305ded7b6ef00d77bcc6c159450

Contents?: true

Size: 1.98 KB

Versions: 3

Compression:

Stored size: 1.98 KB

Contents

module EmojiData

  class EmojiChar
    def initialize(emoji_hash)
      # work around inconsistency in emoji.json for now by just setting a blank
      # array for instance value, and let it get overriden in main
      # deserialization loop if variable is present.
      @variations = []

      # http://stackoverflow.com/questions/1615190/declaring-instance-variables-iterating-over-a-hash
      emoji_hash.each do |k,v|
        instance_variable_set("@#{k}",v)
        eigenclass = class<<self; self; end
        eigenclass.class_eval { attr_reader k }
      end
    end

    # Returns a version of the character for rendering to screen.
    #
    # By default this will now use the variant encoding if it exists.
    def char(options = {})
      options = {variant_encoding: true}.merge(options)
      #decide whether to use the normal unified ID or the variant for encoding to str
      target = (self.variant? && options[:variant_encoding]) ? self.variant : @unified
      EmojiChar::unified_to_char(target)
    end

    # Return ALL known possible string encodings of the emoji char.
    #
    # Mostly useful for doing find operations when you need them all.
    def chars
      results = [self.char({variant_encoding: false})]
      @variations.each do |variation|
        results << EmojiChar::unified_to_char(variation)
      end
      @chars ||= results
    end

    # Public: Is the character represented by a doublebyte unicode codepoint in unicode?
    def doublebyte?
      @unified.match(/-/)
    end

    # does the emojichar have an alternate variant encoding?
    def variant?
      @variations.length > 0
    end

    # return whatever is the most likely variant ID for the emojichar
    # for now, there can only be one, so just return first.
    # (in the future, there may be multiple variants, who knows!)
    def variant
      @variations.first
    end

    alias_method :to_s, :char

    protected
    def self.unified_to_char(cps)
      cps.split('-').map { |i| i.hex }.pack("U*")
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
emoji_data-0.1.0 lib/emoji_data/emoji_char.rb
emoji_data-0.1.0.rc2 lib/emoji_data/emoji_char.rb
emoji_data-0.1.0.rc1 lib/emoji_data/emoji_char.rb