Module: BigBand::BasicExtensions::ClassMethods

Defined in:
lib/big_band/basic_extensions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#guessed_root=(value) ⇒ Object (writeonly)

Sets the attribute guessed_root

Parameters:

  • value

    the value to set the attribute guessed_root to.



18
19
20
# File 'lib/big_band/basic_extensions.rb', line 18

def guessed_root=(value)
  @guessed_root = value
end

#rootObject

The application’s root directory. BigBand will guess if missing.



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/big_band/basic_extensions.rb', line 93

def root
  return ".".expand_path unless app_file?
  return @root if @root
  @guessed_root ||= begin
    dir = app_file.expand_path.dirname
    if dir.basename == "lib" and not (dir / "lib").directory?
      dir.dirname
    else
      dir
    end
  end
end

Instance Method Details

#inheritedObject

Disable automatically running this class as soon it is subclassed.



87
88
89
90
# File 'lib/big_band/basic_extensions.rb', line 87

def inherited
  super
  @run = false
end

#register(*extensions, &block) ⇒ Object

More advacend register:

  • If an exntesion is registered twice, the registered hook will only be called once.



56
57
58
59
# File 'lib/big_band/basic_extensions.rb', line 56

def register(*extensions, &block)
  extensions.reject! { |e| self.extensions.include? e }
  super(*extensions, &block)
end

#root?Boolean

Returns true if the #root is known.

Returns:

  • (Boolean)


107
108
109
# File 'lib/big_band/basic_extensions.rb', line 107

def root?
  !!@root || app_file?
end

#root_glob(*args, &block) ⇒ Object

Like root_path, but does return an array instead of a string. Optionally takes a block that will be called for each entry once.

Example:

class Foo < BigBand
  root_glob("app", "{models,views,controllers}", "*.rb") { |file| load file }
end


76
77
78
# File 'lib/big_band/basic_extensions.rb', line 76

def root_glob(*args, &block)
  Dir.glob(root_path(*args), &block)
end

#root_path(*args) ⇒ Object

Short hand so you can skip those ugly File.expand_path(File.join(File.dirname(__FILE__), …)) lines.



63
64
65
66
67
# File 'lib/big_band/basic_extensions.rb', line 63

def root_path(*args)
  relative = File.join(*args)
  return relative if relative.expand_path == relative
  root.expand_path / relative
end

#run!(options = {}) ⇒ Object

Extended #run!, offers an extandable option parser for BigBand with the same standard options as the one of Sinatra#Default (see #run_option_parser).



127
128
129
130
131
# File 'lib/big_band/basic_extensions.rb', line 127

def run!(options = {})
  run_option_parser.parse!(ARGV.dup) unless ARGV.empty?
  @running = true
  super(options)
end

#run?Boolean

Whether or not to start a webserver.

Returns:

  • (Boolean)


81
82
83
84
# File 'lib/big_band/basic_extensions.rb', line 81

def run?
  @run ||= true
  @run and !@running and app_file? and $0.expand_path == app_file.expand_path
end

#run_option_parserObject

Option parser for #run!



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/big_band/basic_extensions.rb', line 112

def run_option_parser
  @run_option_parser ||= begin
    require 'optparse'
    OptionParser.new do |op|
      op.on('-x')        {       set :lock, true }
      op.on('-e env')    { |val| set :environment, val.to_sym }
      op.on('-s server') { |val| set :server, val }
      op.on('-p port')   { |val| set :port, val.to_i }
    end 
  end
end

#set(key, value = self, &block) ⇒ Object

More advanced set:

  • Adds set_#key and set_value hooks to set.

  • Merges the old value with the new one, if both are hashes:

    set :haml, :format => :html5, :escape_html => true
    set :haml, :excape_html => false
    haml # => { :format => :html5, :escape_html => false }
    
  • Allowes passing a block:

    set(:foo) { Time.now }
    
  • Defines a helper to access #key and #key? unless a helper/method with that name already exists.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/big_band/basic_extensions.rb', line 29

def set(key, value = self, &block)
  # FIXME: refactor, refactor, refactor
  if block_given?
    raise ArgumentError, "both a value and a block given" if value != self
    value = block
  end
  symbolized = (key.to_sym if key.respond_to? :to_sym)
  old_value = (send(symbolized) if symbolized and respond_to? symbolized)
  value = old_value.merge value if value.is_a? Hash and old_value.is_a? Hash
  super(key, value)
  if symbolized
    method_names = instance_methods.map { |m| m.to_s }
    define_method(key)       { self.class.send(key)       } unless method_names.include? key.to_s
    define_method("#{key}?") { self.class.send("#{key}?") } unless method_names.include? "#{key}?"
  end
  # HACK: Sinatra::Base.set uses recursion and in the final step value always
  # is a Proc. Also, if value is a Proc no step ever follows. I abuse this to
  # invoke the hooks only once per set.
  if value.is_a? Proc
    invoke_hook "set_#{key}", self
    invoke_hook :set_value, self, key
  end
  self
end