sinatra_fedora 1.1 → 1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -29,4 +29,7 @@
29
29
  * POSSIBILITY OF SUCH DAMAGE.
30
30
  =end
31
31
 
32
- require File.join(File.dirname(__FILE__), 'sinatra_fedora', 'fedora')
32
+ require 'sinatra/base'
33
+ require 'require_all'
34
+
35
+ require_rel './sinatra_fedora/*.rb'
@@ -29,116 +29,10 @@
29
29
  * POSSIBILITY OF SUCH DAMAGE.
30
30
  =end
31
31
 
32
- require 'sinatra/base'
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
-
63
- module Sinatra
64
- # simple way to escape HTML (method 'h')
65
- module HTMLEscapeHelper
66
- def h(text)
67
- Rack::Utils.escape_html(text)
68
- end
69
- end
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
-
96
- module Templates
97
- def render(engine, data, options={}, locals={}, &block)
98
- # merge app-level options
99
- options = settings.send(engine).merge(options) if settings.respond_to?(engine)
100
- options[:outvar] ||= '@_out_buf'
101
- options[:default_encoding] ||= settings.default_encoding
102
-
103
- # extract generic options
104
- locals = options.delete(:locals) || locals || {}
105
- views = options.delete(:views) || settings.views || "./views"
106
- @default_layout = :layout if @default_layout.nil?
107
- layout = options.delete(:layout)
108
- eat_errors = layout.nil?
109
- layout = @default_layout if layout.nil? or layout == true
110
- content_type = options.delete(:content_type) || options.delete(:default_content_type)
111
- layout_engine = options.delete(:layout_engine) || engine
112
- scope = options.delete(:scope) || self
113
-
114
- # Fedora views
115
- if data != :layout and options[:views_directory].nil?
116
- look_in = (scope.class.name.downcase || scope.class.views_from) if scope.class.views_from.nil?
117
- data = "#{look_in}/#{data.to_s}".to_sym
118
- end
119
-
120
- # compile and render template
121
- layout_was = @default_layout
122
- @default_layout = false
123
- template = compile_template(engine, data, options, views)
124
- output = template.render(scope, locals, &block)
125
- @default_layout = layout_was
126
-
127
- # render layout
128
- if layout
129
- options = options.merge(:views => views, :layout => false, :eat_errors => eat_errors, :scope => scope)
130
- catch(:layout_missing) { return render(layout_engine, layout, options, locals) { output } }
131
- end
132
-
133
- output.extend(ContentTyped).content_type = content_type if content_type
134
- output
135
- end
136
- end
137
- end
138
-
139
32
  class Fedora < Sinatra::Base
140
33
  helpers Sinatra::HTMLEscapeHelper
141
34
  helpers Sinatra::LinkHelper
35
+ helpers Sinatra::Partials
142
36
 
143
37
  def self.inherited(klass)
144
38
  super
@@ -0,0 +1,84 @@
1
+ module Sinatra
2
+ # Simple way to escape HTML (method 'h')
3
+ module HTMLEscapeHelper
4
+ def h(text)
5
+ Rack::Utils.escape_html(text)
6
+ end
7
+ end
8
+
9
+ # partials helper
10
+ module Partials
11
+ def partial(page, options = {})
12
+ options[:views_directory] ||= options[:via]
13
+ options[:layout] ||= false
14
+ render (options[:engine] ||= "haml"), page.to_sym, options
15
+ end
16
+ end
17
+
18
+ # link_to helper
19
+ module LinkHelper
20
+ extend Memoizable
21
+
22
+ def get_namespace(klass)
23
+ begin
24
+ Object.const_get(klass).namespace
25
+ rescue
26
+ klass
27
+ end
28
+ end
29
+
30
+ def link_to(link, disable=false)
31
+ if disable
32
+ return link
33
+ end
34
+
35
+ _class = link.gsub(/^\/([\w\d]+)/, '\1').split('/')
36
+ _name = get_namespace _class[0].capitalize
37
+ _name + _class.join('/').gsub(_class[0], '')
38
+ end
39
+ # Cache the namespace to save time
40
+ memoize :get_namespace
41
+ end
42
+
43
+ module Templates
44
+ def render(engine, data, options={}, locals={}, &block)
45
+ # Merge app-level options
46
+ options = settings.send(engine).merge(options) if settings.respond_to?(engine)
47
+ options[:outvar] ||= '@_out_buf'
48
+ options[:default_encoding] ||= settings.default_encoding
49
+
50
+ # Extract generic options
51
+ locals = options.delete(:locals) || locals || {}
52
+ views = options.delete(:views) || settings.views || "./views"
53
+ @default_layout = :layout if @default_layout.nil?
54
+ layout = options.delete(:layout)
55
+ eat_errors = layout.nil?
56
+ layout = @default_layout if layout.nil? or layout == true
57
+ content_type = options.delete(:content_type) || options.delete(:default_content_type)
58
+ layout_engine = options.delete(:layout_engine) || engine
59
+ scope = options.delete(:scope) || self
60
+
61
+ # Fedora views
62
+ if data != :layout and options[:views_directory].nil?
63
+ look_in = (scope.class.views_from || scope.class.name.downcase)
64
+ data = "#{look_in}/#{data.to_s}".to_sym
65
+ end
66
+
67
+ # Compile and render template
68
+ layout_was = @default_layout
69
+ @default_layout = false
70
+ template = compile_template(engine, data, options, views)
71
+ output = template.render(scope, locals, &block)
72
+ @default_layout = layout_was
73
+
74
+ # Render layout
75
+ if layout
76
+ options = options.merge(:views => views, :layout => false, :eat_errors => eat_errors, :scope => scope)
77
+ catch(:layout_missing) { return render(layout_engine, layout, options, locals) { output } }
78
+ end
79
+
80
+ output.extend(ContentTyped).content_type = content_type if content_type
81
+ output
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,28 @@
1
+ # Memoizable code for caching link_to objects
2
+ # see http://snippets.dzone.com/posts/show/5300
3
+ module Memoizable
4
+ # Store for cached values.
5
+ CACHE = Hash.new{|h,k| h[k] = Hash.new{|h,k| h[k] = {}}} # 3 level hash; CACHE[:foo][:bar][:yelp]
6
+
7
+ # Memoize the given method(s).
8
+ def memoize(*names)
9
+ names.each do |name|
10
+ unmemoized = "__unmemoized_#{name}"
11
+
12
+ class_eval %Q{
13
+ alias :#{unmemoized} :#{name}
14
+ private :#{unmemoized}
15
+ def #{name}(*args)
16
+ cache = CACHE[self][#{name.inspect}]
17
+ cache.has_key?(args) ? cache[args] : (cache[args] = send(:#{unmemoized}, *args))
18
+ end
19
+ }
20
+ end
21
+ end
22
+
23
+ # Flush cached return values.
24
+ def flush_memos
25
+ CACHE.clear
26
+ end
27
+ module_function :flush_memos
28
+ end
metadata CHANGED
@@ -1,46 +1,37 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: sinatra_fedora
3
- version: !ruby/object:Gem::Version
4
- hash: 13
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.2'
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 1
9
- version: "1.1"
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Daniel Durante
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-07-01 00:00:00 -04:00
12
+ date: 2011-08-27 00:00:00.000000000 -04:00
18
13
  default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
21
16
  name: sinatra
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ requirement: &2161353300 !ruby/object:Gem::Requirement
24
18
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 15
29
- segments:
30
- - 1
31
- - 0
32
- version: "1.0"
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '1.0'
33
23
  type: :runtime
34
- version_requirements: *id001
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.
24
+ prerelease: false
25
+ version_requirements: *2161353300
26
+ description: Converts class names intro controllers and automatically maps them. Same
27
+ thing with views and both, views and controllers, can be configured to your liking.
28
+ It combines the best of both Padrino and Sinatra.
36
29
  email: officedebo@gmail.com
37
- executables:
30
+ executables:
38
31
  - fedora
39
32
  extensions: []
40
-
41
33
  extra_rdoc_files: []
42
-
43
- files:
34
+ files:
44
35
  - bin/fedora
45
36
  - lib/hatrack/concert/app/controllers/home.rb
46
37
  - lib/hatrack/concert/app/views/index.haml
@@ -59,44 +50,35 @@ files:
59
50
  - lib/hatrack/official/app.rb
60
51
  - lib/hatrack/official/config.ru
61
52
  - lib/sinatra_fedora/fedora.rb
53
+ - lib/sinatra_fedora/helpers.rb
54
+ - lib/sinatra_fedora/memoize.rb
62
55
  - lib/sinatra_fedora.rb
63
56
  - COPYING
64
57
  - README.rdoc
65
58
  has_rdoc: true
66
59
  homepage: https://github.com/durango/sinatra_fedora
67
- licenses:
60
+ licenses:
68
61
  - MIT
69
62
  post_install_message:
70
63
  rdoc_options: []
71
-
72
- require_paths:
64
+ require_paths:
73
65
  - lib
74
- required_ruby_version: !ruby/object:Gem::Requirement
66
+ required_ruby_version: !ruby/object:Gem::Requirement
75
67
  none: false
76
- requirements:
77
- - - ">="
78
- - !ruby/object:Gem::Version
79
- hash: 3
80
- segments:
81
- - 0
82
- version: "0"
83
- required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
73
  none: false
85
- requirements:
86
- - - ">="
87
- - !ruby/object:Gem::Version
88
- hash: 23
89
- segments:
90
- - 1
91
- - 3
92
- - 6
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
93
77
  version: 1.3.6
94
78
  requirements: []
95
-
96
79
  rubyforge_project:
97
80
  rubygems_version: 1.6.2
98
81
  signing_key:
99
82
  specification_version: 3
100
83
  summary: An even classier way to use Sinatra.
101
84
  test_files: []
102
-