class Object

Rails 4.2.4

Constants

APP_ROOT

Public Class Methods

require_dir(dir) click to toggle source

General

# File lib/a_little_less.rb, line 123
def self.require_dir dir
    Dir[APP_ROOT + "/#{ dir }/*.rb"].each do |file|
        require file
    end
end
setup() click to toggle source
# File lib/a_little_less.rb, line 129
def self.setup
    I18n.config.available_locales = :en
    require_dir "app/controllers"
    require_dir "app/models" if setup_db
    if File.exists? "lib"
        require_dir "lib"
        require_dir "lib/*"
    end
end
setup_db() click to toggle source

DB

# File lib/a_little_less.rb, line 141
def self.setup_db
    if File.exists? DB_CONF
        db_conf = YAML.load_file DB_CONF
        ActiveRecord::Base.configurations["db"] = db_conf[AlittleLess.env.name]
        true
    end
end

Public Instance Methods

blank?() click to toggle source

An object is blank if it’s false, empty, or a whitespace string. For example, ”, ‘ ’, nil, [], and {} are all blank.

This simplifies

address.nil? || address.empty?

to

address.blank?

@return [true, false]

# File lib/vendor/rails_blank.rb, line 18
def blank?
  respond_to?(:empty?) ? !!empty? : !self
end
in?(another_object) click to toggle source

Returns true if this object is included in the argument. Argument must be any object which responds to #include?. Usage:

characters = ["Konata", "Kagami", "Tsukasa"]
"Konata".in?(characters) # => true

This will throw an ArgumentError if the argument doesn’t respond to #include?.

# File lib/vendor/rails_inclusion.rb, line 10
def in?(another_object)
  another_object.include?(self)
rescue NoMethodError
  raise ArgumentError.new("The parameter passed to #in? must respond to #include?")
end
log(*args;)
Alias for: logi
logd(*args;) click to toggle source
# File lib/a_little_less/global_logger.rb, line 62
def logd *args; GlobalLogger.instance.log :debug,       *args; end
loge(*args;) click to toggle source
# File lib/a_little_less/global_logger.rb, line 65
def loge *args; GlobalLogger.instance.log :error,       *args; end
logf(*args;) click to toggle source
# File lib/a_little_less/global_logger.rb, line 66
def logf *args; GlobalLogger.instance.log :fatal,       *args; end
logi(*args;) click to toggle source
# File lib/a_little_less/global_logger.rb, line 63
def logi *args; GlobalLogger.instance.log :info,        *args; end
Also aliased as: log
logu(*args;) click to toggle source
# File lib/a_little_less/global_logger.rb, line 67
def logu *args; GlobalLogger.instance.log :unknown, *args; end
logw(*args;) click to toggle source
# File lib/a_little_less/global_logger.rb, line 64
def logw *args; GlobalLogger.instance.log :warn,        *args; end
presence() click to toggle source

Returns the receiver if it’s present otherwise returns nil. object.presence is equivalent to

object.present? ? object : nil

For example, something like

state   = params[:state]   if params[:state].present?
country = params[:country] if params[:country].present?
region  = state || country || 'US'

becomes

region = params[:state].presence || params[:country].presence || 'US'

@return [Object]

# File lib/vendor/rails_blank.rb, line 45
def presence
  self if present?
end
presence_in(another_object) click to toggle source

Returns the receiver if it’s included in the argument otherwise returns nil. Argument must be any object which responds to #include?. Usage:

params[:bucket_type].presence_in %w( project calendar )

This will throw an ArgumentError if the argument doesn’t respond to #include?.

@return [Object]

# File lib/vendor/rails_inclusion.rb, line 24
def presence_in(another_object)
  self.in?(another_object) ? self : nil
end
present?() click to toggle source

An object is present if it’s not blank.

@return [true, false]

# File lib/vendor/rails_blank.rb, line 25
def present?
  !blank?
end
run_safe() { || ... } click to toggle source
# File lib/a_little_less/global.rb, line 2
def run_safe
    begin
        yield
    rescue => e
        loge e
    end
end