Class: Fontist::Indexes::BaseIndex
  
  
  
  
  
    - Inherits:
 
    - 
      Object
      
        
          - Object
 
          
            - Fontist::Indexes::BaseIndex
 
          
        
        show all
      
     
  
  
  
  
  
  
  
  
  
  
    - Defined in:
 
    - lib/fontist/indexes/base_index.rb
 
  
  
 
  
    
      Class Method Summary
      collapse
    
    
  
    
      Instance Method Summary
      collapse
    
    
  
  Constructor Details
  
    
  
  
    #initialize(data = {})  ⇒ BaseIndex 
  
  
  
  
    
Returns a new instance of BaseIndex.
   
 
  
  
    
      
35
36
37
38
39
40
41
42
43 
     | 
    
      # File 'lib/fontist/indexes/base_index.rb', line 35
def initialize(data = {})
  @index = {}
  data.each_pair do |key, paths|
    paths.each do |path|
      add_index_formula(key, IndexFormula.new(path))
    end
  end
end
     | 
  
 
  
 
  
    Class Method Details
    
      
  
  
    .from_yaml  ⇒ Object 
  
  
  
  
    
      
6
7
8
9
10
11
12
13
14
15
16
17
18
19 
     | 
    
      # File 'lib/fontist/indexes/base_index.rb', line 6
def self.from_yaml
  @from_yaml ||= begin
    unless Dir.exist?(Fontist.formulas_repo_path)
      raise Errors::MainRepoNotFoundError.new(
        "Please fetch formulas with `fontist update`.",
      )
    end
    rebuild unless File.exist?(path)
    data = YAML.load_file(path)
    new(data)
  end
end
     | 
  
 
    
      
  
  
    .path  ⇒ Object 
  
  
  
  
    
      
21
22
23 
     | 
    
      # File 'lib/fontist/indexes/base_index.rb', line 21
def self.path
  raise NotImplementedError, "Please define path of an index"
end 
     | 
  
 
    
      
  
  
    .rebuild  ⇒ Object 
  
  
  
  
    
      
29
30
31
32
33 
     | 
    
      # File 'lib/fontist/indexes/base_index.rb', line 29
def self.rebuild
  index = new
  index.build
  index.to_yaml
end 
     | 
  
 
    
      
  
  
    .reset_cache  ⇒ Object 
  
  
  
  
    
      
25
26
27 
     | 
    
      # File 'lib/fontist/indexes/base_index.rb', line 25
def self.reset_cache
  @from_yaml = nil
end 
     | 
  
 
    
   
  
    Instance Method Details
    
      
  
  
    
      
51
52
53 
     | 
    
      # File 'lib/fontist/indexes/base_index.rb', line 51
def add_formula(_formula)
  raise NotImplementedError, "Please define how to add formula to an index, use #add_index_formula"
end 
     | 
  
 
    
      
  
  
    
      
55
56
57
58
59 
     | 
    
      # File 'lib/fontist/indexes/base_index.rb', line 55
def add_index_formula(key_raw, index_formula)
  key = normalize_key(key_raw)
  @index[key] ||= []
  @index[key] << index_formula unless @index[key].include?(index_formula)
end 
     | 
  
 
    
      
  
  
    #build  ⇒ Object 
  
  
  
  
    
      
45
46
47
48
49 
     | 
    
      # File 'lib/fontist/indexes/base_index.rb', line 45
def build
  Formula.all.each do |formula|
    add_formula(formula)
  end
end
     | 
  
 
    
      
  
  
    
      
61
62
63 
     | 
    
      # File 'lib/fontist/indexes/base_index.rb', line 61
def load_formulas(key)
  index_formulas(key).map(&:to_full)
end 
     | 
  
 
    
      
  
  
    
      
65
66
67 
     | 
    
      # File 'lib/fontist/indexes/base_index.rb', line 65
def load_index_formulas(key)
  index_formulas(key)
end 
     | 
  
 
    
      
  
  
    #to_h  ⇒ Object 
  
  
  
  
    
      
75
76
77
78
79 
     | 
    
      # File 'lib/fontist/indexes/base_index.rb', line 75
def to_h
  @index.map do |key, index_formulas|
    [key, index_formulas.map(&:to_s)]
  end.to_h
end
     | 
  
 
    
      
  
  
    #to_yaml  ⇒ Object 
  
  
  
  
    
      
69
70
71
72
73 
     | 
    
      # File 'lib/fontist/indexes/base_index.rb', line 69
def to_yaml
  dir = File.dirname(self.class.path)
  FileUtils.mkdir_p(dir) unless File.exist?(dir)
  File.write(self.class.path, YAML.dump(to_h))
end 
     |