class Emojidex::Data::Collection

listing and search of standard UTF emoji

Attributes

categories[RW]
emoji[RW]
r18[RW]
raster_source_path[RW]
source_path[RW]
vector_source_path[RW]

Public Class Methods

new(opts = {}) click to toggle source

Initialize Collection. You can pass a list of emoji to seed the collection

# File lib/emojidex/data/collection.rb, line 23
def initialize(opts = {})
  @emoji = {}
  @raster_source_path = @vector_source_path = @source_path = nil
  @r18 = opts[:r18] || false
  if opts.include? :cache_path
    setup_cache(opts[:cache_path])
    opts.delete :cache_path
  else
    setup_cache
  end
  load_local_collection(opts[:local_load_path]) if opts.include? :local_load_path
  add_emoji(opts[:emoji]) if opts.include? :emoji
  @emoji
end

Public Instance Methods

<<(list)
Alias for: add_emoji
add_emoji(list) click to toggle source

Adds emojis to the collection After add categories are updated

# File lib/emojidex/data/collection.rb, line 127
def add_emoji(list)
  _add_list(list)
  categorize
  associate_variants
  associate_customizations
  condense_moji_code_data
  @emoji
end
Also aliased as: <<
category(category_code) click to toggle source

Get all emoji from this collection that are part of the specified category Returns a new collection of only emoji in the specified category

# File lib/emojidex/data/collection.rb, line 114
def category(category_code)
  categorized = @emoji.values.select { |moji| moji.category == category_code }
  Emojidex::Data::Collection.new(emoji: categorized, r18: @r18)
end
category?(*category_codes) click to toggle source

Check to see if there are emoji in this collection which have the specified categories Returns true if there are emoji for all secified categories within this collection

# File lib/emojidex/data/collection.rb, line 121
def category?(*category_codes)
  (category_codes.uniq - @categories).empty?
end
collect(&block) click to toggle source
# File lib/emojidex/data/collection.rb, line 67
def collect(&block)
  @emoji.values.collect(&block)
end
each(&block) click to toggle source

each override to map each functionality to the emoji hash values

# File lib/emojidex/data/collection.rb, line 54
def each(&block)
  @emoji.values.each(&block)
end
emojis() click to toggle source
# File lib/emojidex/data/collection.rb, line 49
def emojis
  @emoji.values
end
find_by_code(code) click to toggle source

Gets the emoji with the specified code Returns the Emoji object or nil if no emoji with that code is found

# File lib/emojidex/data/collection.rb, line 84
def find_by_code(code)
  @emoji[code.gsub(/\s/, '_').to_sym]
end
find_by_code_ja(code_ja) click to toggle source

Locates emoji by Japanese code (original Japanese emoji name [絵文字名]) Only applies to collections that contain JA codes, this function is mapped to find_by_code for all other implementations (such as client)

# File lib/emojidex/data/collection.rb, line 91
def find_by_code_ja(code_ja)
  each do |m|
    return m if m[:code_ja] == code_ja
  end
  nil
end
Also aliased as: コード検索
find_by_moji(moji) click to toggle source

Retreives an Emoji object by the actual moji code/character code Will likely only return moji from UTF collection

# File lib/emojidex/data/collection.rb, line 73
def find_by_moji(moji)
  each do |m|
    return m if m[:moji] == moji
  end
  nil
end
Also aliased as: 文字検索
find_by_unicode(unicode) click to toggle source
# File lib/emojidex/data/collection.rb, line 100
def find_by_unicode(unicode)
  unicode = unicode.downcase
  each do |m|
    return m if m[:unicode] == unicode
  end
  nil
end
load_local_collection(path) click to toggle source

Loads an emoji collection on local storage

# File lib/emojidex/data/collection.rb, line 39
def load_local_collection(path)
  @source_path = File.expand_path(path)
  @vector_source_path = @source_path if @vector_source_path.nil?
  @raster_source_path = @source_path if @raster_source_path.nil?
  json = IO.read(@source_path + '/emoji.json')
  list = JSON.parse(json, symbolize_names: true)
  add_emoji(list)
  generate_paths
end
map(&block) click to toggle source
# File lib/emojidex/data/collection.rb, line 63
def map(&block)
  @emoji.values.map(&block)
end
remove_emoji(code) click to toggle source
# File lib/emojidex/data/collection.rb, line 138
def remove_emoji(code)
  emoji.delete(code)
  @emoji
end
select(&block) click to toggle source

select override to map select functionality to the emoji hash values

# File lib/emojidex/data/collection.rb, line 59
def select(&block)
  @emoji.values.select(&block)
end
コード検索(code_ja)
Alias for: find_by_code_ja
文字検索(moji)
Alias for: find_by_moji

Private Instance Methods

_add_list(list) click to toggle source
# File lib/emojidex/data/collection.rb, line 145
def _add_list(list)
  return if list.nil?
  list.each do |moji_info|
    if moji_info.instance_of? Emojidex::Data::Emoji
      next if @r18 == false && moji_info.r18 == true
      @emoji[moji_info.code.to_sym] = moji_info.dup
    else
      next if @r18 == false && moji_info.include?(:r18) && moji_info[:r18] == true
      emoji = Emojidex::Data::Emoji.new moji_info
      emoji.paths = get_paths(emoji)
      @emoji[Emojidex.escape_code(emoji.code.to_s).to_sym] = emoji
    end
  end
end
associate_customizations() click to toggle source
# File lib/emojidex/data/collection.rb, line 178
def associate_customizations
  @emoji.values.each do |emoji_obj|
    emoji_obj.combinations.each do |combo|
      @emoji[combo.base.to_sym].add_customization(combo) if @emoji.include? combo.base.to_sym
    end
  end
end
associate_variants() click to toggle source
# File lib/emojidex/data/collection.rb, line 166
def associate_variants
  @emoji.values.each do |emoji_obj|
    next unless emoji_obj.code.match(/\(.*\)$/) # this emoji is a variant
    # check for base
    base_code = emoji_obj.code.sub(/\(.*\)$/, '').to_sym
    if @emoji.key? base_code
      @emoji[base_code].variants << emoji_obj.code.to_sym
      emoji_obj.base = base_code
    end
  end
end
categorize() click to toggle source

Makes a list of all categories which contain emoji in this collection

# File lib/emojidex/data/collection.rb, line 161
def categorize
  @categories = @emoji.values.map(&:category)
  @categories.uniq!
end