Module: Bricks::RequirementsValidation

Included in:
Bricks
Defined in:
lib/bricks/requirements_validation.rb

Instance Method Summary collapse

Instance Method Details

#config_for_active_bricksObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bricks/requirements_validation.rb', line 52

def config_for_active_bricks
  raw_config = self.raw_config

  config = {}

  raw_config.each do |brick, opts|
    if opts.kind_of?(Hash)
      next unless opts['switch'] == true
      if activation = opts.delete('activate_if')
        if errors = requirement_errors(brick, activation)
          if defined?(ActiveRecord::Base) && logger = ActiveRecord::Base.logger
            ActiveRecord::Base.logger.warn "'#{brick}' not activated: #{errors.join(', ')}"
          end
          # puts "'#{brick}' not activated: #{errors.join(', ')}" if RAILS_ENV == 'development'
        end
      end
      config[brick] = opts unless errors
    else
      if opts == true || brick == 'zena' # zena always ON
        config[brick] = {}
      end
    end
  end
  config
end

#raw_configObject



43
44
45
46
47
48
49
50
# File 'lib/bricks/requirements_validation.rb', line 43

def raw_config
  @raw_config ||=
    if File.exist?("#{RAILS_ROOT}/config/bricks.yml")
      raw_config = YAML.load_file("#{RAILS_ROOT}/config/bricks.yml")[RAILS_ENV] || {}
    else
      raw_config = YAML.load_file("#{Zena::ROOT}/config/bricks.yml")[RAILS_ENV] || {}
    end
end

#requirement_errors(brick, requirements) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bricks/requirements_validation.rb', line 5

def requirement_errors(brick, requirements)
  current_stderr = $stderr
  $stderr = StringIO.new
  errors = []
  requirements.each do |k,v|
    case k
    when 'gem'
      v.split(',').each do |name|
        begin
          require name.strip
        rescue LoadError => err
          errors << "'#{name}' missing"
        end
      end
    when 'file'
      v.split(',').each do |name|
        unless File.exist?("#{RAILS_ROOT}/#{name}")
          errors << "'#{name}' missing"
        end
      end
    when 'adapter'
      db_config = File.join(RAILS_ROOT, 'config', 'database.yml')
      if File.exist?(db_config)
        config = YAML.load_file(db_config)
        adapter = config[RAILS_ENV]['adapter']
      else
        puts "No config/database.yml file, using 'mysql' as adapter to validate bricks"
        adapter = 'mysql'
      end
      unless v.split(',').map(&:strip).include?(adapter)
        errors << "'#{adapter}' not supported"
      end
    end
  end
  $stderr = current_stderr
  errors.empty? ? nil : errors
end

#runtime_requirement_errors(brick_name) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/bricks/requirements_validation.rb', line 78

def runtime_requirement_errors(brick_name)
  return ["'#{brick_name}' was not activated."] unless opts = Bricks::CONFIG[brick_name]
  if run_requirements = opts.delete('run_if')
    return requirement_errors(brick_name, run_requirements)
  end
  nil
end