Class: Lono::Configset::Register::Base

Inherits:
AbstractBase show all
Includes:
EvaluateFile, Dsl
Defined in:
lib/lono/configset/register/base.rb

Direct Known Subclasses

Blueprint, Project

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EvaluateFile

#evaluate_file

Methods included from Dsl

#configset, #source

Methods inherited from AbstractBase

#initialize, #reinitialize, #template_path

Methods included from Blueprint::Root

#find_blueprint_root, #set_blueprint_root

Constructor Details

This class inherits a constructor from Lono::AbstractBase

Class Method Details

.append(registry) ⇒ Object



112
113
114
# File 'lib/lono/configset/register/base.rb', line 112

def append(registry)
  self.configsets << registry unless has?(registry)
end

.clear!Object



103
104
105
106
# File 'lib/lono/configset/register/base.rb', line 103

def clear!
  self.configsets = []
  self.validations = []
end

.has?(registry) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/lono/configset/register/base.rb', line 116

def has?(registry)
  configsets.detect { |r| r.name == registry.name && r.args == registry.args }
end

.prepend(registry) ⇒ Object



108
109
110
# File 'lib/lono/configset/register/base.rb', line 108

def prepend(registry)
  self.configsets.unshift(registry) unless has?(registry)
end

Instance Method Details

#finder_classObject

Used in Base#validate!



30
31
32
33
34
35
36
37
# File 'lib/lono/configset/register/base.rb', line 30

def finder_class
  case self
  when Lono::Configset::Register::Blueprint
    Lono::Finder::Blueprint::Configset
  when Lono::Configset::Register::Project
    Lono::Finder::Configset
  end
end

#jade_typeObject



25
26
27
# File 'lib/lono/configset/register/base.rb', line 25

def jade_type
  finder_class.to_s.sub('Lono::Finder::','').underscore
end

#jadifyObject



19
20
21
22
23
# File 'lib/lono/configset/register/base.rb', line 19

def jadify
  self.class.configsets.each do |registry|
    Lono::Jade.new(registry.name, jade_type, registry)
  end
end

#pretty_trace(caller_line) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lono/configset/register/base.rb', line 81

def pretty_trace(caller_line)
  md = caller_line.match(/(.*\.rb):(\d+):/)
  path, error_line_number = md[1], md[2].to_i

  context = 5 # lines of context
  top, bottom = [error_line_number-context-1, 0].max, error_line_number+context-1

  puts "Showing file: #{path}"
  lines = IO.read(path).split("\n")
  spacing = lines.size.to_s.size
  lines[top..bottom].each_with_index do |line_content, index|
    current_line_number = top+index+1
    if current_line_number == error_line_number
      printf("%#{spacing}d %s\n".color(:red), current_line_number, line_content)
    else
      printf("%#{spacing}d %s\n", current_line_number, line_content)
    end
  end
end

#registerObject



14
15
16
17
# File 'lib/lono/configset/register/base.rb', line 14

def register
  evaluate
  jadify
end

#show_errors_and_exit!(errors) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/lono/configset/register/base.rb', line 65

def show_errors_and_exit!(errors)
  errors.each do |error_type, registry|
    name, caller_line = registry.name, registry.caller_line
    case error_type
    when :finder_missing
      puts "ERROR: Configset with name #{name} not found. Please double check Gemfile and configs/#{@blueprint}/configsets files.".color(:red)
      pretty_trace(caller_line)
    when :resource_missing
      puts "ERROR: Configset with name #{name} does not specify resource. The resource key is required.".color(:red)
      pretty_trace(caller_line)
      raise
    end
  end
  exit 1
end

#store_for_validation(registry) ⇒ Object

Store to be able to provide the validation errors altogether later.



40
41
42
43
44
45
46
47
# File 'lib/lono/configset/register/base.rb', line 40

def store_for_validation(registry)
  # save caller line to use later for pointing to exactly line
  caller_line = caller.grep(/evaluate_file/).first
  registry.caller_line = caller_line
  # huge performance improvement by only validating the first configset registration of duplicate gems
  names = self.class.validations.map {|r| r.name }
  self.class.validations << registry unless names.include?(registry.name)
end

#validate!Object

Validate the configset before building templates. So user finds out about errors early.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lono/configset/register/base.rb', line 50

def validate!
  errors = []
  self.class.validations.each do |registry|
    config = finder_class.find(registry.name) # finder_class implemented in subclass
    errors << [:finder_missing, registry] unless config

    if registry.depends_on.nil? && registry.resource.nil?
      errors << [:resource_missing, registry]
    end
  end

  return if errors.empty? # all good
  show_errors_and_exit!(errors)
end