Class: CodelessCode::Catalog

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/codeless_code/catalog.rb

Overview

The entire collection of fables available to the application from a given data directory.

Instance Method Summary collapse

Constructor Details

#initialize(root_dir) ⇒ Catalog

Returns a new instance of Catalog

Parameters:

  • root_dir (Pathname)

    A directory that contains Fable files


25
26
27
# File 'lib/codeless_code/catalog.rb', line 25

def initialize(root_dir)
  @root_dir = root_dir
end

Instance Method Details

#fable_setsObject


54
55
56
# File 'lib/codeless_code/catalog.rb', line 54

def fable_sets
  language_sets.flat_map(&:fable_sets)
end

#fablesObject


58
59
60
# File 'lib/codeless_code/catalog.rb', line 58

def fables
  fable_sets.flat_map(&:fables)
end

#fetch(lang) ⇒ LanguageSet

Parameters:

  • lang (Symbol)

Returns:


41
42
43
44
45
46
47
48
# File 'lib/codeless_code/catalog.rb', line 41

def fetch(lang)
  if languages.include?(lang)
    LanguageSet.new(lang, root_dir: @root_dir)
  else
    raise LanguageSet::NotFoundError,
          format('No fables for language %p', lang)
  end
end

#language_setsObject


50
51
52
# File 'lib/codeless_code/catalog.rb', line 50

def language_sets
  languages.map { |lang| LanguageSet.new(lang, root_dir: @root_dir) }
end

#languagesEnumerable<Symbol>

Returns the codes of all available languages

Returns:

  • (Enumerable<Symbol>)

    the codes of all available languages


30
31
32
33
34
35
36
37
# File 'lib/codeless_code/catalog.rb', line 30

def languages
  @languages ||= @root_dir.glob('*-*')
                          .select(&:directory?)
                          .map { |dir| dir.basename.to_s.split('-').first }
                          .uniq
                          .map(&:to_sym)
                          .sort
end

#select(filter) ⇒ Object


62
63
64
# File 'lib/codeless_code/catalog.rb', line 62

def select(filter)
  fables.select { |fable| filter.call(fable) }.sort_by(&:number)
end