lib/tanuki_emoji/character.rb in tanuki_emoji-0.11.0 vs lib/tanuki_emoji/character.rb in tanuki_emoji-0.12.0

- old
+ new

@@ -23,10 +23,24 @@ attr_reader :name, :codepoints, :codepoints_alternates, :alpha_code, :aliases, :ascii_aliases, :description, :category attr_accessor :unicode_version, :sort_key, :noto_image + # Ensure alpha code is formatted with colons + # + # @param [String] alpha_code + # @return [String] formatted alpha code + def self.format_alpha_code(alpha_code) + alpha_code.to_s.match?(ALPHA_CODE_REGEXP) ? alpha_code.to_s : ":#{alpha_code}:" + end + + def self.format_name(raw_name) + matched = raw_name.match(ALPHA_CODE_REGEXP) + + matched ? matched['alpha_text'] : raw_name + end + # @param [String] name # @param [String] codepoints # @param [String] alpha_code # @param [String] description # @param [String] category @@ -70,10 +84,22 @@ return if ascii_aliases.include?(ascii_string) ascii_aliases << ascii_string end + # Replace the current alpha_code + # + # @param [String] alpha_code + def replace_alpha_code(alpha_code) + formatted_code = self.class.format_alpha_code(alpha_code) + + aliases.delete(formatted_code) + + @name = self.class.format_name(alpha_code) + @alpha_code = formatted_code + end + # Return a Hex formatted version of the Unicode code points # # @return [String] Hex formatted version of the unicode def hex(codepoint = nil) codepoint ? unicode_to_hex(codepoint).join('-') : unicode_to_hex(codepoints).join('-') @@ -90,12 +116,11 @@ # regional flags. if flag? name = noto_image unless name - # The two character code is only found in the aliases - aliases.each do |item| + ([alpha_code] + aliases).each do |item| name = item.tr(':', '').sub('flag_', '') break if name.length == 2 end end @@ -127,23 +152,9 @@ codepoints_alternates == other.codepoints_alternates && alpha_code == other.alpha_code && aliases == other.aliases && ascii_aliases == other.ascii_aliases && description == other.description - end - - # Ensure alpha code is formatted with colons - # - # @param [String] alpha_code - # @return [String] formatted alpha code - def self.format_alpha_code(alpha_code) - alpha_code.to_s.match?(ALPHA_CODE_REGEXP) ? alpha_code.to_s : ":#{alpha_code}:" - end - - def self.format_name(raw_name) - matched = raw_name.match(ALPHA_CODE_REGEXP) - - matched ? matched['alpha_text'] : raw_name end private # Return each codepoint converted to its hex value as string