Class: Module

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

Instance Method Summary collapse

Instance Method Details

#picnic!Object

Adds Picnic functionality to a Camping-enabled module.

Example:

Camping.goes :Blog
Blog.picnic!

Your Blog Camping app now has Picnic functionality.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/picnic.rb', line 23

def picnic!
  include Picnic
  
  puts "Adding Picnic functionality to #{self} from #{File.dirname(File.expand_path(__FILE__))}..."
  self.module_eval do
    # Initialize your application's logger. 
    # This is automatically done for you when you call #picnic!
    # The logger is initialized based on your <tt>:log</tt> configuration.
    # See <tt>config.example.yml</tt> for info on configuring the logger.
    def init_logger
      puts "Initializing #{self} logger..."
      $LOG = Picnic::Utils::Logger.new(self::Conf.log[:file])
      $LOG.level = Picnic::Utils::Logger.const_get(self::Conf.log[:level])
    end
    module_function :init_logger
    
    # Initialize your application's database logger. 
    # If enabled, all SQL queries going through ActiveRecord will be logged here.
    #
    # THIS SEEMS TO BE BROKEN RIGHT NOW and I can't really understand why.
    def init_db_logger
      begin
        if self::Conf.db_log
          log_file = self::Conf.db_log[:file] || "#{self.to_s.downcase}_db.log"
          self::Models::Base.logger = Picnic::Utils::Logger.new(log_file)
          self::Models::Base.logger.level = Picnic::Utils::Logger.const_get(self::Conf.db_log[:level] || 'DEBUG')
          $LOG.debug "Logging database queries to #{log_file.inspect}"
        end
      rescue Errno::EACCES => e
        $LOG.warn "Can't write to database log file at '#{log_file}': #{e}"
      end
    end
    module_function :init_db_logger
    
    # Enable authentication for your app.
    #
    # For example:
    #
    #   Camping.goes :Blog
    #   Blog.picnic!
    #
    #   $CONF[:authentication] ||= {:username => 'admin', :password => 'picnic'}
    #   Blog.authenticate_using :basic
    #
    #   module Blog
    #     def self.authenticate(credentials)
    #       credentials[:username] == Taskr::Conf[:authentication][:username] &&
    #         credentials[:password] == Taskr::Conf[:authentication][:password]
    #     end
    #   end
    #
    # Note that in the above example we use the authentication configuration from
    # your app's conf file.
    #
    def authenticate_using(mod)
      load "picnic/authentication.rb"
      mod = self::Authentication.const_get(mod.to_s.camelize) unless mod.kind_of? Module
      
      $LOG.info("Enabling authentication for all requests using #{mod.inspect}.")
      
      module_eval do
        include mod
      end
    end
    module_function :authenticate_using
  
    # Launches the web server to run your Picnic app.
    # This method will continue to run as long as your server is running.
    def start_picnic
        require "#{File.dirname(File.expand_path(__FILE__))}/picnic/postambles.rb"
        self.extend self::Postambles
        
        if $PID_FILE && !(self::Conf.server.to_s == 'mongrel' || self::Conf.server.to_s == 'webrick')
          $LOG.warn("Unable to create a pid file. You must use mongrel or webrick for this feature.")
        end
        
        puts "\nStarting with configuration: #{$CONF.to_yaml}"
        puts
      
      #  begin
          raise NoMethodError if self::Conf.server.nil?
          send(self::Conf.server)
      #  rescue NoMethodError => e
      #    # FIXME: this rescue can sometime report the incorrect error messages due to other underlying problems
      #    #         raising a NoMethodError
      #    if Fluxr::Conf.server
      #      raise e, "The server setting '#{Fluxr::Conf.server}' in your config.yml file is invalid."
      #    else
      #      raise e, "You must have a 'server' setting in your config.yml file. Please see the Fluxr documentation."
      #    end
      #  end
    end
    module_function :start_picnic
    
    c = File.dirname(File.expand_path(__FILE__))+'/picnic/controllers.rb'
    p = IO.read(c).gsub("Picnic", self.to_s)
    eval p, TOPLEVEL_BINDING
    
  end
  
  self::Conf.load(self)
  init_logger
end