Module: Minitrain::ClassMethods

Defined in:
lib/minitrain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#after_blockObject (readonly)

Returns the value of attribute after_block.



14
15
16
# File 'lib/minitrain.rb', line 14

def after_block
  @after_block
end

#before_blockObject (readonly)

Returns the value of attribute before_block.



14
15
16
# File 'lib/minitrain.rb', line 14

def before_block
  @before_block
end

#helpers_blockObject (readonly)

Returns the value of attribute helpers_block.



14
15
16
# File 'lib/minitrain.rb', line 14

def helpers_block
  @helpers_block
end

Instance Method Details

#after(&block) ⇒ Object



17
# File 'lib/minitrain.rb', line 17

def after(&block); @after_block = block; end

#before(&block) ⇒ Object



15
# File 'lib/minitrain.rb', line 15

def before(&block); @before_block = block; end

#dispatcher(&block) ⇒ Object



18
# File 'lib/minitrain.rb', line 18

def dispatcher(&block); @dispatcher_block = block; end

#dispatcher_blockObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/minitrain.rb', line 19

def dispatcher_block
  @dispatcher_block || proc{
    @path_atoms = @r.path_info.split('/').find_all{|s| s!=''}
    @action, *@action_arguments = @path_atoms
    @action = @action.gsub(/[-.]/, '_') unless @action.nil?
    unless public_methods.include?(@action)||(@action&&public_methods.include?(@action.to_sym))
      if public_methods.include?('index')||public_methods.include?(:index) # For different RUBY_VERSION(s)
        @action, @action_arguments = 'index', @path_atoms
      else
        @action, @action_arguments = 'not_found', @path_atoms
      end
    end
    instance_eval(&self.class.before_block) unless self.class.before_block.nil?
    instance_eval(&self.class.helpers_block) unless self.class.helpers_block.nil?
    begin
      @res.write(self.__send__(@action,*@action_arguments))
    rescue ArgumentError => e
      failed_method = e.backtrace[0][/`.*'$/][1..-2]
      raise unless failed_method==@action
      @res.write(self.__send__('not_found', @path_atoms))
    end
    instance_eval(&self.class.after_block) unless self.class.after_block.nil?
  }
end

#helpers(&block) ⇒ Object



16
# File 'lib/minitrain.rb', line 16

def helpers(&block); @helpers_block = block; end