sinatra_fedora 1.0.1 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
data/COPYING ADDED
@@ -0,0 +1,10 @@
1
+ Copyright (c) 2011, Daniel Durante
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+ * Neither the name of Sinatra Fedora nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9
+
10
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.rdoc ADDED
@@ -0,0 +1,90 @@
1
+ = Sinatra::Fedora
2
+
3
+ A Sinatra (http://github.com/sinatra/sinatra) class that provides the ability to automatically map controllers into the URL. Acts like a very light "wrapper" around Sinatra::Base. Essentially, a watered-down (in a good way) version of Padrino.
4
+
5
+ tl;dr A classier way for Sinatra
6
+
7
+ == Installation
8
+ gem install sinatra_fedora
9
+
10
+ == The Fedora Binary
11
+ The Fedora binary offers basic functionality as for now.
12
+
13
+ === Creating a new project
14
+ Creating a project within the directory
15
+ fedora sing [hat]
16
+
17
+ Creating a project within a new directory
18
+ fedora new <project_name> [hat]
19
+
20
+ === What are hats?
21
+ Fedora offers a way to create templates for your projects. Think of each hat as a blueprint for a project. Sometimes you just need Sinatra and other times you need Sinatra/DataMapper/Warden/etc. You can manage hats by using the "hatrack" command in fedora.
22
+
23
+ To add a hat...
24
+ fedora hatrack add ~/path/to/directory <hat name>
25
+
26
+ To remove a hat (you can replace +rm+ with +rem+, +remove+, +del+, or +delete+)...
27
+ fedora hatrack rm <hat name>
28
+
29
+ === Official Hats
30
+ [official]
31
+ The default hat, sets up a simple Sinatra application
32
+
33
+ [datamapper]
34
+ Same as default hat with DataMapper (you must apply which adapter you need) added
35
+
36
+ [concert]
37
+ Sets up DataMapper (same rules apply for [datamapper]) and {sinatra_warden}[https://github.com/jsmestad/sinatra_warden]. It does not setup CSRF, etc.
38
+
39
+ == Usage
40
+ === Helpers
41
+ To escape HTML simply type "h" for example:
42
+ h "<This is escaped!>"
43
+
44
+ Fedora offers an incredibly way to link/route towards pages. Let's say your controller is called "Auth" and every link goes to '/auth', '/auth/list', '/auth/dashboard', etc. You would simply type (for example):
45
+ link_to('/auth/list')
46
+
47
+ Your boss goes up to you and says, "I want all of the membership links to point towards user not auth." The only thing you have to change is the controller's url/namespace option.
48
+ class Auth < Fedora
49
+ url '/user'
50
+ ...code here...
51
+ end
52
+
53
+ And Fedora will translate all of your '/auth' links to '/user' automatically. The namespace translation only happens once and then caches for the rest of the page so having multiple '/auth/*' links wont cause much overhead.
54
+
55
+ === Main app.rb file
56
+ require 'sinatra_fedora'
57
+
58
+ class Fedora
59
+ enable :sessions
60
+
61
+ set :views, File.dirname(__FILE__) + '/app/views'
62
+ set :public, File.dirname(__FILE__) + '/public'
63
+
64
+ Dir.glob('app/models/*.rb').each { |r| require File.expand_path(File.join(File.dirname(__FILE__), r)) }
65
+ Dir.glob('app/controllers/*.rb').each { |r| require File.expand_path(File.join(File.dirname(__FILE__), r)) }
66
+ end
67
+
68
+ === Typical Controller Class
69
+ class Home < Fedora
70
+ url '/' # Tells Fedora to set these actions to the root of the URL
71
+ views_from '/' # Grab views from :views/
72
+
73
+ get '/' do
74
+ haml :index
75
+ end
76
+ end
77
+
78
+ If your controller doesn't contain "views_from" Fedora will simply look in :views/<controller name>/ ("url" acts in the same exact way). Be sure to check out the app.rb and config.ru source. You can replace "url" method with "namespace" as well.
79
+
80
+ == Goals
81
+ * Make development faster with larger projects using Sinatra
82
+ * Keep it as slim as possible. For a fully featured Sinatra framework use Padrino.
83
+ * Keep it classy.
84
+
85
+ == Feedback
86
+ I'm always looking for ways to improve Fedora as well as my other projects. Shoot me a message on here fork the project over!
87
+
88
+ == Contributors
89
+
90
+ * Jonathan Stott (https://github.com/namelessjon)
data/bin/fedora ADDED
@@ -0,0 +1,104 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'fileutils'
5
+
6
+ options = {}
7
+
8
+ opt_parser = OptionParser.new do |opt|
9
+ opt.banner = "Usage: fedora COMMAND [OPTIONS]"
10
+ opt.separator ""
11
+ opt.separator "Commands"
12
+ opt.separator " new <project name> [hat]: Creates a new Fedora project"
13
+ opt.separator " e.g. fedora new project_name"
14
+ opt.separator " hats are blueprints for your projects type 'fedora hatrack' for more info."
15
+ opt.separator ""
16
+ opt.separator " sing [hat]: Creates a new Fedora project in the directory that you're in."
17
+ opt.separator ""
18
+ end
19
+
20
+ opt_parser.parse!
21
+
22
+ case ARGV[0]
23
+ when "new", "sing"
24
+ if ARGV[1].nil? and ARGV[0] != "sing"
25
+ puts opt_parser
26
+ else
27
+ ARGV[2] ||= "official"
28
+
29
+ if File.directory? ARGV[2]
30
+ hatrack_dir = ARGV[2]
31
+ else
32
+ ARGV[2].gsub(/[^\w\d]/, '')
33
+ hatrack_dir = File.expand_path(File.join(File.dirname(__FILE__), '../lib/hatrack/' + ARGV[2]))
34
+ end
35
+
36
+ if !File.directory? hatrack_dir
37
+ puts "Hatrack '" + ARGV[2] + "' doesn't exist"
38
+ exit(1)
39
+ end
40
+
41
+ if ARGV[0] == "sing"
42
+ FileUtils.cp_r hatrack_dir + '/.', Dir.pwd
43
+ ARGV[1] = ''
44
+ else
45
+ ARGV[1].gsub(/[^\w\d]/, '').downcase
46
+ current_dir = File.expand_path(File.join(File.dirname(__FILE__)))
47
+
48
+ if File.exists? current_dir + "/#{ARGV[1]}"
49
+ puts "Project #{ARGV[1]} already exist in this directory, try choosing a different name."
50
+ exit(1)
51
+ end
52
+
53
+ FileUtils.mkdir ARGV[1]
54
+ FileUtils.cp_r hatrack_dir + '/.', ARGV[1]
55
+ end
56
+
57
+ puts "Sinatra found his fedora in " + Dir.pwd + "/#{ARGV[1]}"
58
+ puts "Head on over there to start developing"
59
+ end
60
+ when "hatrack"
61
+ case ARGV[1]
62
+ when "rm", "rem", "remove", "del", "delete"
63
+ ARGV[2].gsub(/[^\w\d]/, '')
64
+ hatrack_dir = File.expand_path(File.join(File.dirname(__FILE__), '../lib/hatrack/' + ARGV[2]))
65
+ if ARGV[2] == "official"
66
+ puts "You can't delete the official Fedora hat."
67
+ elsif File.directory? hatrack_dir
68
+ FileUtils.rm_rf hatrack_dir
69
+ puts ARGV[2] + " hat has been removed."
70
+ else
71
+ puts ARGV[2] + " is not a valid hat."
72
+ end
73
+ when "add"
74
+ if ARGV[3].nil?
75
+ puts "You must name this hat something."
76
+ exit(1)
77
+ end
78
+
79
+ ARGV[3].gsub(/[^\w\d]/, '')
80
+ if File.directory? ARGV[2]
81
+ FileUtils.mkdir File.expand_path(File.join(File.dirname(__FILE__), '../lib/hatrack/' + ARGV[3]))
82
+ FileUtils.cp_r ARGV[2] + '/.', File.expand_path(File.join(File.dirname(__FILE__), '../lib/hatrack/' + ARGV[3]))
83
+ puts ARGV[3] + " hat has been made added to the hatrack."
84
+ else
85
+ puts "We couldn't find the hat: " + ARGV[2]
86
+ exit(1)
87
+ end
88
+ else
89
+ opt_parser2 = OptionParser.new do |opt|
90
+ opt.banner = "Usage: fedora hatrack COMMAND HAT"
91
+ opt.separator ""
92
+ opt.separator "Commands"
93
+ opt.separator " rm, rem, remove, del, delete: Removes a hat from the hatrack"
94
+ opt.separator " e.g. fedora hatrack rm official"
95
+ opt.separator ""
96
+ opt.separator " add [directory to hat] [hat name]: Adds a hat to the hatrack."
97
+ opt.separator " e.g. fedora hatrack add ~/custom_hats/example example"
98
+ opt.separator ""
99
+ end
100
+ puts opt_parser2
101
+ end
102
+ else
103
+ puts opt_parser
104
+ end
@@ -0,0 +1,16 @@
1
+ class Home < Fedora
2
+ # Set as the root URL otherwise
3
+ # the url will default to /home
4
+ # (class name lowercased)
5
+ url '/'
6
+
7
+ # views_from tells Fedora which
8
+ # folder in :views you want to use
9
+ # for all actions in this class
10
+ # (:views_directory overrides this)
11
+ views_from '/'
12
+
13
+ get '/' do
14
+ haml :index
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ Visit our
2
+ %a(href = 'https://github.com/durango/sinatra_fedora' target = '_blank') GitHub
3
+ page to learn more.
@@ -0,0 +1,2 @@
1
+ %h1 Welcome to Sinatra::Fedora!
2
+ = yield
@@ -0,0 +1,55 @@
1
+ require 'sinatra_fedora'
2
+ require 'sass'
3
+ require 'haml'
4
+ require 'sinatra_warden'
5
+ require 'datamapper'
6
+ require 'rack-flash'
7
+
8
+ class Fedora
9
+ enable :sessions
10
+ register Sinatra::Warden
11
+ use Rack::Flash
12
+
13
+ #DataMapper.setup(:default, 'adapter://user:pass@host/db')
14
+
15
+ set :views, File.dirname(__FILE__) + '/app/views'
16
+ set :public, File.dirname(__FILE__) + '/public'
17
+
18
+ # sinatra_warden helpers
19
+ # for more info https://github.com/jsmestad/sinatra_warden
20
+ set :auth_error_message, 'Invalid e-mail/password combination'
21
+ set :auth_failure_path, '/login'
22
+ set :auth_already_logged_in, '/account'
23
+
24
+ # Don't forget to change this!
25
+ use Rack::Session::Cookie, :secret => "my secret key!"
26
+
27
+ configure(:development) do
28
+ #DataMapper::Logger.new($stdout, :debug)
29
+ end
30
+
31
+ # Render .css files
32
+ get '/:file.css' do |file|
33
+ content_type :css
34
+ response['Expires'] = (Time.now + 60*60*24*356*3).httpdate
35
+ if File.file?(File.join(settings.views, "#{file}.scss"))
36
+ scss file.to_sym
37
+ else
38
+ not_found
39
+ end
40
+ end
41
+
42
+ Dir.glob('app/models/*.rb').each { |r| require File.expand_path(File.join(File.dirname(__FILE__), r)) }
43
+ DataMapper.finalize.auto_upgrade!
44
+
45
+ Dir.glob('app/controllers/*.rb').each { |r| require File.expand_path(File.join(File.dirname(__FILE__), r)) }
46
+
47
+ # Warden helper file
48
+ require File.dirname(__FILE__) + '/warden.rb'
49
+
50
+ # Add warden strategies here
51
+ use Warden::Manager do |manager|
52
+ manager.default_strategies :password
53
+ manager.failure_app = Fedora
54
+ end
55
+ end
@@ -0,0 +1,2 @@
1
+ require './app.rb'
2
+ run Fedora.map
@@ -0,0 +1,22 @@
1
+ # Link warden with the correct user id and function to grab user info
2
+ Warden::Manager.serialize_into_session{|user| user.id }
3
+ Warden::Manager.serialize_from_session{|id| User.get(id) }
4
+
5
+ Warden::Manager.before_failure do |env,opts|
6
+ env['REQUEST_METHOD'] = "POST"
7
+ end
8
+
9
+ Warden::Strategies.add(:password) do
10
+ def valid?
11
+ params["email"] || params["password"]
12
+ end
13
+
14
+ def authenticate!
15
+ u = User.authenticate(params["email"], params["password"])
16
+ if u.nil?
17
+ fail!("Could not log in")
18
+ else
19
+ success!(u)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ class Home < Fedora
2
+ # Set as the root URL otherwise
3
+ # the url will default to /home
4
+ # (class name lowercased)
5
+ url '/'
6
+
7
+ # views_from tells Fedora which
8
+ # folder in :views you want to use
9
+ # for all actions in this class
10
+ # (:views_directory overrides this)
11
+ views_from '/'
12
+
13
+ get '/' do
14
+ haml :index
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ Visit our
2
+ %a(href = 'https://github.com/durango/sinatra_fedora' target = '_blank') GitHub
3
+ page to learn more.
@@ -0,0 +1,2 @@
1
+ %h1 Welcome to Sinatra::Fedora!
2
+ = yield
@@ -0,0 +1,20 @@
1
+ require 'sinatra_fedora'
2
+ require 'datamapper'
3
+
4
+ class Fedora
5
+ enable :sessions
6
+
7
+ #DataMapper.setup(:default, 'adapter://user:password@host/db')
8
+
9
+ set :views, File.dirname(__FILE__) + '/app/views'
10
+ set :public, File.dirname(__FILE__) + '/public'
11
+
12
+ configure(:development) do
13
+ DataMapper::Logger.new($stdout, :debug)
14
+ end
15
+
16
+ Dir.glob('app/models/*.rb').each { |r| require File.expand_path(File.join(File.dirname(__FILE__), r)) }
17
+ #DataMapper.finalize.auto_upgrade!
18
+
19
+ Dir.glob('app/controllers/*.rb').each { |r| require File.expand_path(File.join(File.dirname(__FILE__), r)) }
20
+ end
@@ -0,0 +1,2 @@
1
+ require './app.rb'
2
+ run Fedora.map
@@ -0,0 +1,16 @@
1
+ class Home < Fedora
2
+ # Set as the root URL otherwise
3
+ # the url will default to /home
4
+ # (class name lowercased)
5
+ url '/'
6
+
7
+ # views_from tells Fedora which
8
+ # folder in :views you want to use
9
+ # for all actions in this class
10
+ # (:views_directory overrides this)
11
+ views_from '/'
12
+
13
+ get '/' do
14
+ haml :index
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ Visit our
2
+ %a(href = 'https://github.com/durango/sinatra_fedora' target = '_blank') GitHub
3
+ page to learn more.
@@ -0,0 +1,2 @@
1
+ %h1 Welcome to Sinatra::Fedora!
2
+ = yield
@@ -0,0 +1,11 @@
1
+ require 'sinatra_fedora'
2
+
3
+ class Fedora
4
+ enable :sessions
5
+
6
+ set :views, File.dirname(__FILE__) + '/app/views'
7
+ set :public, File.dirname(__FILE__) + '/public'
8
+
9
+ Dir.glob('app/models/*.rb').each { |r| require File.expand_path(File.join(File.dirname(__FILE__), r)) }
10
+ Dir.glob('app/controllers/*.rb').each { |r| require File.expand_path(File.join(File.dirname(__FILE__), r)) }
11
+ end
@@ -0,0 +1,2 @@
1
+ require './app.rb'
2
+ run Fedora.map
@@ -31,13 +31,68 @@
31
31
 
32
32
  require 'sinatra/base'
33
33
 
34
+ # Memoizable code for caching link_to objects
35
+ # see http://snippets.dzone.com/posts/show/5300
36
+ module Memoizable
37
+ # Store for cached values.
38
+ CACHE = Hash.new{|h,k| h[k] = Hash.new{|h,k| h[k] = {}}} # 3 level hash; CACHE[:foo][:bar][:yelp]
39
+
40
+ # Memoize the given method(s).
41
+ def memoize(*names)
42
+ names.each do |name|
43
+ unmemoized = "__unmemoized_#{name}"
44
+
45
+ class_eval %Q{
46
+ alias :#{unmemoized} :#{name}
47
+ private :#{unmemoized}
48
+ def #{name}(*args)
49
+ cache = CACHE[self][#{name.inspect}]
50
+ cache.has_key?(args) ? cache[args] : (cache[args] = send(:#{unmemoized}, *args))
51
+ end
52
+ }
53
+ end
54
+ end
55
+
56
+ # Flush cached return values.
57
+ def flush_memos
58
+ CACHE.clear
59
+ end
60
+ module_function :flush_memos
61
+ end
62
+
34
63
  module Sinatra
64
+ # simple way to escape HTML (method 'h')
35
65
  module HTMLEscapeHelper
36
66
  def h(text)
37
67
  Rack::Utils.escape_html(text)
38
68
  end
39
69
  end
40
70
 
71
+ # link_to helper
72
+ module LinkHelper
73
+ extend Memoizable
74
+
75
+ def get_namespace(klass)
76
+ begin
77
+ Object.const_get(klass).namespace
78
+ rescue
79
+ klass
80
+ end
81
+ end
82
+
83
+ def link_to(link, disable=false)
84
+ if disable
85
+ return link
86
+ end
87
+
88
+ _class = link.gsub(/^\/([\w\d]+)/, '\1').split('/')
89
+ _name = get_namespace _class[0].capitalize
90
+ _name + _class.join('/').gsub(_class[0], '')
91
+ end
92
+ # cache the namespace to save time
93
+ memoize :get_namespace
94
+ end
95
+
41
96
  module Templates
42
97
  def render(engine, data, options={}, locals={}, &block)
43
98
  # merge app-level options
@@ -79,12 +134,11 @@ module Sinatra
79
134
  output
80
135
  end
81
136
  end
82
-
83
- helpers HTMLEscapeHelper
84
137
  end
85
138
 
86
139
  class Fedora < Sinatra::Base
87
140
  helpers Sinatra::HTMLEscapeHelper
141
+ helpers Sinatra::LinkHelper
88
142
 
89
143
  def self.inherited(klass)
90
144
  super
@@ -92,7 +146,7 @@ class Fedora < Sinatra::Base
92
146
  end
93
147
 
94
148
  def self.descendents
95
- @descendents ||= []
149
+ @descendents ||= []
96
150
  end
97
151
 
98
152
  def self.map
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra_fedora
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 0
9
8
  - 1
10
- version: 1.0.1
9
+ version: "1.1"
11
10
  platform: ruby
12
11
  authors:
13
12
  - Daniel Durante
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2011-06-28 00:00:00 -04:00
17
+ date: 2011-07-01 00:00:00 -04:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -31,19 +30,38 @@ dependencies:
31
30
  - 1
32
31
  - 0
33
32
  version: "1.0"
34
- type: :development
33
+ type: :runtime
35
34
  version_requirements: *id001
36
35
  description: Converts class names intro controllers and automatically maps them. Same thing with views and both, views and controllers, can be configured to your liking. It combines the best of both Padrino and Sinatra.
37
36
  email: officedebo@gmail.com
38
- executables: []
39
-
37
+ executables:
38
+ - fedora
40
39
  extensions: []
41
40
 
42
41
  extra_rdoc_files: []
43
42
 
44
43
  files:
45
- - lib/sinatra_fedora.rb
44
+ - bin/fedora
45
+ - lib/hatrack/concert/app/controllers/home.rb
46
+ - lib/hatrack/concert/app/views/index.haml
47
+ - lib/hatrack/concert/app/views/layout.haml
48
+ - lib/hatrack/concert/app.rb
49
+ - lib/hatrack/concert/config.ru
50
+ - lib/hatrack/concert/warden.rb
51
+ - lib/hatrack/datamapper/app/controllers/home.rb
52
+ - lib/hatrack/datamapper/app/views/index.haml
53
+ - lib/hatrack/datamapper/app/views/layout.haml
54
+ - lib/hatrack/datamapper/app.rb
55
+ - lib/hatrack/datamapper/config.ru
56
+ - lib/hatrack/official/app/controllers/home.rb
57
+ - lib/hatrack/official/app/views/index.haml
58
+ - lib/hatrack/official/app/views/layout.haml
59
+ - lib/hatrack/official/app.rb
60
+ - lib/hatrack/official/config.ru
46
61
  - lib/sinatra_fedora/fedora.rb
62
+ - lib/sinatra_fedora.rb
63
+ - COPYING
64
+ - README.rdoc
47
65
  has_rdoc: true
48
66
  homepage: https://github.com/durango/sinatra_fedora
49
67
  licenses:
@@ -67,10 +85,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
85
  requirements:
68
86
  - - ">="
69
87
  - !ruby/object:Gem::Version
70
- hash: 3
88
+ hash: 23
71
89
  segments:
72
- - 0
73
- version: "0"
90
+ - 1
91
+ - 3
92
+ - 6
93
+ version: 1.3.6
74
94
  requirements: []
75
95
 
76
96
  rubyforge_project: