Class: Perc::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/perc/application.rb

Overview

Class for holding stuff you might find in a typical application

Defined Under Namespace

Classes: Config

Constant Summary collapse

LoggerLevels =
{
  'DEBUG' => Logger::DEBUG,
  'INFO' => Logger::INFO,
  'WARN' => Logger::WARN,
  'ERROR' => Logger::ERROR
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Application

Returns a new instance of Application.



34
35
36
37
38
39
40
41
42
# File 'lib/perc/application.rb', line 34

def initialize(options)
  @root = options.fetch(:root)
  @env = ActiveSupport::StringInquirer.new(options.fetch(:env))
  config do |c|
    Dir[root.join('app', '*')].each do |d|
      c.autoload_paths << d.to_s
    end
  end
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



30
31
32
# File 'lib/perc/application.rb', line 30

def env
  @env
end

#loggerObject (readonly)

Returns the value of attribute logger.



30
31
32
# File 'lib/perc/application.rb', line 30

def logger
  @logger
end

#rootObject (readonly)

Returns the value of attribute root.



30
31
32
# File 'lib/perc/application.rb', line 30

def root
  @root
end

Instance Method Details

#bootObject



70
71
72
73
74
75
# File 'lib/perc/application.rb', line 70

def boot
  initialize_logging
  setup_autoload_directories
  require_environment
  run_initializers
end

#commands {|cli| ... } ⇒ Object

Yields:

  • (cli)


83
84
85
86
87
88
# File 'lib/perc/application.rb', line 83

def commands
  require 'perc/cli_wrapper'
  cli = CliWrapper.new
  yield(cli) if block_given?
  cli
end

#config {|@config| ... } ⇒ Object

Yields:



77
78
79
80
81
# File 'lib/perc/application.rb', line 77

def config
  @config ||= Config.new
  yield(@config) if block_given?
  @config
end

#config_for(thing, required = true) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/perc/application.rb', line 57

def config_for(thing, required = true)
  file = root.join('config', "#{thing}.yml")
  if file.exist?
    require 'yaml'
    require 'erb'
    thing_parsed = (YAML.load(ERB.new(file.read).result) || {})[env]
    return (thing_parsed and thing_parsed.with_indifferent_access) unless (thing_parsed.nil? && required)
    raise Config::EnvironmentMissingError, "config_for(#{thing}) nil for env: #{env}"
  else
    raise Config::FileMissingError, "Could not load configuration. No such file: #{file}"
  end
end

#load_rakeObject



48
49
50
51
52
53
54
55
# File 'lib/perc/application.rb', line 48

def load_rake
  rake_files = Dir[File.expand_path('../tasks/*.rake', __FILE__)] +
    Dir[root.join('lib/tasks/*.rake')]

  rake_files.each do |f|
    load(f)
  end
end