Class: Emojidex::Data::Categories

Inherits:
Object
  • Object
show all
Defined in:
lib/emojidex/data/categories.rb

Overview

Holds a master list of categories

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(categories_json = nil) ⇒ Categories

Returns a new instance of Categories.



7
8
9
10
11
12
13
# File 'lib/emojidex/data/categories.rb', line 7

def initialize(categories_json = nil)
  if categories_json
    load_categories(categories_json)
  else
    load_standard_categories
  end
end

Instance Attribute Details

#categoriesObject

Returns the value of attribute categories.



5
6
7
# File 'lib/emojidex/data/categories.rb', line 5

def categories
  @categories
end

Instance Method Details

#collect(&block) ⇒ Object

collect override to map each functionality to the categories hash



31
32
33
# File 'lib/emojidex/data/categories.rb', line 31

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

#each(&block) ⇒ Object

each override to map each functionality to the categories hash



16
17
18
# File 'lib/emojidex/data/categories.rb', line 16

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

#load_categories(json) ⇒ Object

loads categories from a JSON hash object / JSON text



36
37
38
39
40
41
42
43
44
45
# File 'lib/emojidex/data/categories.rb', line 36

def load_categories(json)
  raw = JSON.parse(json, symbolize_names: true)
  raw = raw[:categories]

  @categories ||= {}
  raw.each do |category_info|
    category = Emojidex::Data::Category.new category_info
    @categories[category.code.to_sym] = category
  end
end

#load_standard_categoriesObject

loads standard categories local to the emojidex package *automatically called on initialize if no options are passed



49
50
51
# File 'lib/emojidex/data/categories.rb', line 49

def load_standard_categories
  load_categories(IO.read(File.expand_path('../../../../emoji/categories.json', __FILE__)))
end

#map(&block) ⇒ Object

map override to map each functionality to the categories hash



26
27
28
# File 'lib/emojidex/data/categories.rb', line 26

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

#select(&block) ⇒ Object

select override to map select functionality to the categories hash



21
22
23
# File 'lib/emojidex/data/categories.rb', line 21

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