class Granify::Controller::Base

Attributes

default_method[RW]
helper[RW]
methods_require_internet[RW]
model[RW]

Public Instance Methods

can_exec?(command = nil, name = nil) click to toggle source

Determines if the command can execute

# File lib/controller.rb, line 45
def can_exec?(command = nil, name = nil)
  @model = Granify::Model.const_get(command.capitalize).new rescue nil
  @helper = Granify::Helper.const_get(command.capitalize).new rescue nil
  @methods_require_internet = []

  # get user-defined methods to use as a fallback
  user_defined_methods = []

  # timeout is the first system defined method after the user defined
  # ones
  for i in 0..public_methods.index(:to_json) -1
    user_defined_methods.push(public_methods[i].to_sym)
  end

  @default_method = name || user_defined_methods.first || :sample

  if !respond_to? default_method.to_sym, true
    Notify.error("Command not found: #{name}")
  end

  true
end
exec(args = []) click to toggle source

Handle the request

# File lib/controller.rb, line 21
def exec(args = [])
  self.send(@default_method.to_sym)
end
post_exec(total_errors = 0, total_warnings = 0, total_files = 0) click to toggle source

Perform post-run cleanup tasks, such as deleting old logs

# File lib/controller.rb, line 26
def post_exec(total_errors = 0, total_warnings = 0, total_files = 0)
  # TODO: do post-test evaluation
  # total_errors = @model.data.log.faults[:errors]
  # total_warnings = @model.data.log.faults[:warnings]

  # if @model.data.log.total_files_processed
  #   total_files = @model.data.log.total_files_processed
  # end

  if @@options[:verbose]
    Notify.info("Results:\nCommand: granify #{$request.controller} #{$request.command} #{($request.flags && $request.flags.size > 0 ? $request.flags.to_json : '')}\n#{total_errors} errors and #{total_warnings} warnings in #{total_files} source files.\nTotal execution time: #{Helper::Time.human_readable(@last_model.start, Time.now)}")
  end

  if Logs.dirty?
    Logs.clean
  end
end
pre_exec() click to toggle source

Perform pre-run tasks

# File lib/controller.rb, line 9
def pre_exec
  OptionParser.new do |opt|
    opt.banner = "granify controller command [...-flags]"

    opt.on("-v", "--verbose", "Verbose output") do |v|
      # short output
      @@options[:verbose] = v
    end
  end.parse!
end
required_modules(*modules) click to toggle source
# File lib/controller.rb, line 73
def required_modules(*modules)
  auto_load_required(modules).each do |type, hash|
    # Make each auto-loaded module available as an instance variable
    # i.e. [:hound]: @hound_controller, @hound_model, @hound_helper
    # i.e. [:test]: @test_controller, @test_model, @test_helper
    # Only files that exist and can be called are loaded
    hash.each do |key, value|
      instance_variable_set("@#{key}_#{type}".to_sym, value)
      @last_model = instance_variable_get("@#{key}_model")
    end
  end
end
sample() click to toggle source

default method called by exec if no argument is passed

# File lib/controller.rb, line 69
def sample
  Notify.warning("Method not implemented");
end

Private Instance Methods

auto_load_required(modules = []) click to toggle source

autoload and instantiate required libraries, models and helpers

# File lib/controller.rb, line 88
def auto_load_required(modules = [])
  loaded = {:controller => {}, :helper => {}, :model => {}}
  
  begin
    modules.each do |mod|
      if File.exists? "#{Granify::INSTALLED_DIR}/lib/controllers/#{mod}.rb"
        require "#{Granify::INSTALLED_DIR}/lib/controllers/#{mod}.rb"
        
        loaded[:controller][mod] = Granify::Controller.const_get(mod.capitalize).new
      else
        raise StandardError, "Controller not found: #{mod}"
      end

      if File.exists? "#{Granify::INSTALLED_DIR}/lib/helpers/#{mod}.rb"
        require "#{Granify::INSTALLED_DIR}/lib/helpers/#{mod}.rb"
        loaded[:helper][mod] = Granify::Helper.const_get(mod.capitalize).new

        # auto-instantiate new instance of helper for the new instance of the controller
        loaded[:controller][mod].helper = loaded[:helper][mod]
      end

      if File.exists? "#{Granify::INSTALLED_DIR}/lib/models/#{mod}.rb"
        require "#{Granify::INSTALLED_DIR}/lib/models/#{mod}.rb"
        loaded[:model][mod] = Granify::Model.const_get(mod.capitalize).new

        # auto-instantiate new instance of model for the new instance of the controller
        loaded[:controller][mod].model = loaded[:model][mod]
      else
        loaded[:controller][mod].model = Model::Base.new
      end
    end

    loaded
  rescue StandardError => e
    Notify.error(e.message)
  end
end