Class: Emojidex::Collection

Inherits:
Object
  • Object
show all
Includes:
CollectionAssetInformation, CollectionCache
Defined in:
lib/emojidex/collection.rb

Overview

listing and search of standard UTF emoji

Direct Known Subclasses

Extended, Service, UTF

Instance Attribute Summary collapse

Attributes included from CollectionCache

#cache_path

Instance Method Summary collapse

Methods included from CollectionAssetInformation

#generate_checksums, #generate_paths, #get_checksums, #get_paths

Methods included from CollectionCache

#cache!, #cache_index, #setup_cache, #write_index

Constructor Details

#initialize(emoji_list = nil, local_load_path = nil) ⇒ Collection

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



18
19
20
21
22
# File 'lib/emojidex/collection.rb', line 18

def initialize(emoji_list = nil, local_load_path = nil)
  @emoji = {}
  load_local_collection(local_load_path) unless local_load_path.nil?
  add_emoji(emoji_list) unless emoji_list.nil?
end

Instance Attribute Details

#categoriesObject

Returns the value of attribute categories.



14
15
16
# File 'lib/emojidex/collection.rb', line 14

def categories
  @categories
end

#emojiObject

Returns the value of attribute emoji.



14
15
16
# File 'lib/emojidex/collection.rb', line 14

def emoji
  @emoji
end

#raster_source_pathObject

Returns the value of attribute raster_source_path.



14
15
16
# File 'lib/emojidex/collection.rb', line 14

def raster_source_path
  @raster_source_path
end

#source_pathObject

Returns the value of attribute source_path.



14
15
16
# File 'lib/emojidex/collection.rb', line 14

def source_path
  @source_path
end

#vector_source_pathObject

Returns the value of attribute vector_source_path.



14
15
16
# File 'lib/emojidex/collection.rb', line 14

def vector_source_path
  @vector_source_path
end

Instance Method Details

#add_emoji(list) ⇒ Object Also known as: <<

Adds emojis to the collection After add categories are updated



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/emojidex/collection.rb', line 100

def add_emoji(list)
  list.each do |moji_info|
    if moji_info.instance_of? Emojidex::Emoji
      @emoji[moji_info.code.to_sym] = moji_info.dup
    else
      emoji = Emojidex::Emoji.new moji_info
      @emoji[emoji.code.to_sym] = emoji
    end
  end
  categorize
  @emoji
end

#category(category_code) ⇒ Object

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



87
88
89
90
# File 'lib/emojidex/collection.rb', line 87

def category(category_code)
  categorized = @emoji.values.select { |moji| moji.category == category_code }
  Emojidex::Collection.new categorized
end

#category?(*category_codes) ⇒ Boolean

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

Returns:

  • (Boolean)


94
95
96
# File 'lib/emojidex/collection.rb', line 94

def category?(*category_codes)
  (category_codes.uniq - @categories).empty?
end

#collect(&block) ⇒ Object



48
49
50
# File 'lib/emojidex/collection.rb', line 48

def collect(&block)
  @emoji.values.collect(&block)
end

#each(&block) ⇒ Object

each override to map each functionality to the emoji hash values



35
36
37
# File 'lib/emojidex/collection.rb', line 35

def each(&block)
  @emoji.values.each(&block)
end

#find_by_code(code) ⇒ Object

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



66
67
68
# File 'lib/emojidex/collection.rb', line 66

def find_by_code(code)
  @emoji[code.gsub(/\s/, '_').to_sym]
end

#find_by_code_ja(code_ja) ⇒ Object Also known as: コード検索

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)



73
74
75
76
77
# File 'lib/emojidex/collection.rb', line 73

def find_by_code_ja(code_ja)
  each do |emoji|
    return emoji if emoji[:code_ja] == code_ja
  end
end

#find_by_moji(moji) ⇒ Object Also known as: 文字検索

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



54
55
56
57
58
59
60
# File 'lib/emojidex/collection.rb', line 54

def find_by_moji(moji)
  result = nil
  each do |emoji|
    result = emoji if emoji[:moji] == moji
  end
  result
end

#load_local_collection(path) ⇒ Object

Loads an emoji collection on local storage



25
26
27
28
29
30
31
32
# File 'lib/emojidex/collection.rb', line 25

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)
end

#map(&block) ⇒ Object



44
45
46
# File 'lib/emojidex/collection.rb', line 44

def map(&block)
  @emoji.values.map(&block)
end

#search(criteria = {}) ⇒ Object



81
82
83
# File 'lib/emojidex/collection.rb', line 81

def search(criteria = {})
  Emojidex::Collection.new _sub_search(@emoji.values.dup, criteria)
end

#select(&block) ⇒ Object

select override to map select functionality to the emoji hash values



40
41
42
# File 'lib/emojidex/collection.rb', line 40

def select(&block)
  @emoji.values.select(&block)
end