tarpaulin 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.3.3
@@ -103,7 +103,8 @@ end
103
103
 
104
104
  require File.here "tarpaulin/logger"
105
105
  require File.here "tarpaulin/endless"
106
+ require File.here "tarpaulin/tilt/clearsilver_template"
106
107
  require File.here "tarpaulin/camping"
107
108
  require File.here "tarpaulin/camping/flash"
108
109
  require File.here "tarpaulin/camping/filter"
109
- require File.here "tarpaulin/tilt/clearsilver_template"
110
+ require File.here "tarpaulin/camping/accept_language"
@@ -6,14 +6,17 @@
6
6
  #
7
7
  # don't need to require anything as these are modules that are
8
8
  # included elsewhere and those elsewhere require stuff :)
9
+ #
10
+ # A. use #included and then call TheApp for stuff we need (like #link_controller)
11
+ # B. what is the difference between #R, #R, #D, #URL ???
12
+ # C. use decent error pages, 403, 404, and Ruby Inspect (html version) in dev mode
13
+ # D. do link_ methods need to use #R or some variant?
9
14
  module Tarpaulin
10
15
 
11
- @@link = nil
12
- def self.link_controller=(link)
13
- @@link = link
14
- end
15
- def self.link_controller
16
- @@link
16
+ def self.included(mod)
17
+ $DEBUG and $stderr.puts "mod is #{@mod}"
18
+ @mod = mod
19
+ $DEBUG and $stderr.puts "now it is #{@mod}"
17
20
  end
18
21
 
19
22
  # check for Context, only show a.inspect in dev mode
@@ -24,9 +27,10 @@ module Tarpaulin
24
27
  if f.nil?
25
28
  f = block.call
26
29
  end
30
+ extra = (ENV['MODE'] == 'development') ? "<br><hr>#{a.inspect}" : ''
27
31
  html_msg = "<!DOCTYPE html><html lang='en'><head>"+
28
32
  "<title>&#171; Uh oh &#187;</title><body><span class='error'>"+
29
- "Site error (BEEP!) 403: Invalid Path: #{f}<br>#{a.inspect}</span></body>"
33
+ "Site error (BEEP!) 403: Invalid Path: #{f}#{extra}</span></body>"
30
34
  r(403, html_msg, h)
31
35
  end
32
36
 
@@ -37,9 +41,10 @@ module Tarpaulin
37
41
  if f.nil?
38
42
  f = block.call
39
43
  end
44
+ extra = (ENV['MODE'] == 'development') ? "<br><hr>#{a.inspect}" : ''
40
45
  html_msg = "<!DOCTYPE html><html lang='en'><head>"+
41
46
  "<title>&#171; Uh oh &#187;</title><body><span class='error'>"+
42
- "Site error (BEEP!) 404: File not found: #{f}<br>#{a.inspect}</span></body>"
47
+ "Site error (BEEP!) 404: File not found: #{f}#{extra}</span></body>"
43
48
  r(404, html_msg, h)
44
49
  end
45
50
 
@@ -47,6 +52,12 @@ module Tarpaulin
47
52
 
48
53
  module Helpers
49
54
 
55
+ def self.included(mod)
56
+ $DEBUG and $stderr.puts "h:mod is #{@mod}"
57
+ @mod = mod
58
+ $DEBUG and $stderr.puts "now it is #{@mod}"
59
+ end
60
+
50
61
  # should be configified
51
62
  def document_root # or whatever ...
52
63
  '/var/www/localhost/htdocs/'
@@ -76,31 +87,25 @@ module Tarpaulin
76
87
  File.join(document_root, JS)
77
88
  end
78
89
 
90
+ def prepare_root
91
+ if @root.length > 0 and @root[0] == '/'.ord
92
+ root = @root.reverse.chop!.reverse
93
+ else
94
+ root = @root
95
+ end
96
+ root
97
+ end
98
+
79
99
  def stylesheet_link_tag(stylish)
80
-
81
- href = if stylish.start_with?("http")
100
+ type = "text/css"
101
+ href = if stylish.start_with?("http") or stylish.start_with?("//")
82
102
  "#{stylish}"
83
103
  else
84
- type = "text/css"
85
- # $stderr.puts @root.inspect
86
- if @root.length > 0 and @root[0] == '/'
87
- root = @root.reverse.chop!.reverse
88
- else
89
- root = @root
90
- end
91
- # $stderr.puts root
92
- if root and root.length > 0
93
- r = R(Tarpaulin.link_controller, root, Tarpaulin::Helpers::CSS, "#{stylish}.css")
94
- r2 = R(Tarpaulin.link_controller, Tarpaulin::Helpers::CSS, "#{stylish}.css")
95
- else
96
- r = R(Tarpaulin.link_controller, Tarpaulin::Helpers::CSS, "#{stylish}.css")
97
- r2 = nil
98
- end
99
- # $stderr.puts r.inspect
100
- # $stderr.puts r2.inspect
101
- r
104
+ root = prepare_root
105
+ (root and root.length > 0) ?
106
+ R(link_controller, root, Tarpaulin::Helpers::CSS, "#{stylish}.css") :
107
+ R(link_controller, Tarpaulin::Helpers::CSS, "#{stylish}.css")
102
108
  end
103
- # $stderr.puts href
104
109
 
105
110
  Markaby::Builder.new.capture {
106
111
  link :rel => "stylesheet",
@@ -111,35 +116,29 @@ module Tarpaulin
111
116
  end
112
117
 
113
118
  def javascript_link_tag(scriptish)
114
-
115
- src = if scriptish.start_with?("http")
119
+ type = "text/javascript"
120
+ src = if scriptish.start_with?("http") or scriptish.start_with?("//") # 3rd party css? really?
116
121
  "#{scriptish}"
117
122
  else
118
- type = "text/javascript"
119
- # $stderr.puts @root.inspect
120
- if @root.length > 0 and @root[0] == '/'
121
- root = @root.reverse.chop!.reverse
122
- else
123
- root = @root
124
- end
125
- # $stderr.puts root
126
- if root and root.length > 0
127
- r = R(Tarpaulin.link_controller, root, Tarpaulin::Helpers::JS, "#{scriptish}.js")
128
- r2 = R(Tarpaulin.link_controller, Tarpaulin::Helpers::JS, "#{scriptish}.js")
129
- else
130
- r = R(Tarpaulin.link_controller, Tarpaulin::Helpers::JS, "#{scriptish}.js")
131
- r2 = nil
132
- end
133
- # $stderr.puts r.inspect
134
- # $stderr.puts r2.inspect
135
- r
123
+ root = prepare_root
124
+ (root and root.length > 0) ?
125
+ R(link_controller, root, Tarpaulin::Helpers::JS, "#{scriptish}.js") :
126
+ R(link_controller, Tarpaulin::Helpers::JS, "#{scriptish}.js")
136
127
  end
137
- # $stderr.puts src
138
128
 
139
129
  Markaby::Builder.new.capture {
140
- script(:type => type, :src => src) {}
130
+ script(:type => type, :src => src) do
131
+ end
141
132
  }
142
133
  end
134
+
135
+ @@empty_hdf = Neo::Hdf.new
136
+
137
+ def hdf_render *a
138
+ block_given? ?
139
+ render(*a, &Proc.new) :
140
+ render(*a) { @@empty_hdf }
141
+ end
143
142
 
144
143
  end # module Helpers
145
144
 
@@ -0,0 +1,61 @@
1
+ #
2
+ # unashamedly robbed from https://github.com/iain/http_accept_language/blob/master/lib/http_accept_language.rb
3
+ #
4
+ module HttpAcceptLanguage
5
+
6
+ # Returns a sorted array based on user preference in HTTP_ACCEPT_LANGUAGE.
7
+ # Browsers send this HTTP header, so don't think this is holy.
8
+ #
9
+ # Example:
10
+ #
11
+ # request.user_preferred_languages
12
+ # # => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]
13
+ #
14
+ def user_preferred_languages
15
+ @user_preferred_languages ||= env['HTTP_ACCEPT_LANGUAGE'].split(/\s*,\s*/).collect do |l|
16
+ l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
17
+ l.split(';q=')
18
+ end.sort do |x,y|
19
+ raise "Not correctly formatted" unless x.first =~ /^[a-z\-]+$/i
20
+ y.last.to_f <=> x.last.to_f
21
+ end.collect do |l|
22
+ l.first.downcase.gsub(/-[a-z]+$/i) { |x| x.upcase }
23
+ end
24
+ rescue # Just rescue anything if the browser messed up badly.
25
+ []
26
+ end
27
+
28
+ # Sets the user languages preference, overiding the browser
29
+ #
30
+ def user_preferred_languages=(languages)
31
+ @user_preferred_languages = languages
32
+ end
33
+
34
+ # Finds the locale specifically requested by the browser.
35
+ #
36
+ # Example:
37
+ #
38
+ # request.preferred_language_from I18n.available_locales
39
+ # # => 'nl'
40
+ #
41
+ def preferred_language_from(array)
42
+ (user_preferred_languages & array.collect { |i| i.to_s }).first
43
+ end
44
+
45
+ # Returns the first of the user_preferred_languages that is compatible
46
+ # with the available locales. Ignores region.
47
+ #
48
+ # Example:
49
+ #
50
+ # request.compatible_language_from I18n.available_locales
51
+ #
52
+ def compatible_language_from(available_languages)
53
+ user_preferred_languages.map do |x| #en-US
54
+ available_languages.find do |y| # en
55
+ y = y.to_s
56
+ x == y || x.split('-', 2).first == y.split('-', 2).first
57
+ end
58
+ end.compact.first
59
+ end
60
+
61
+ end
@@ -1,35 +1,50 @@
1
1
  #
2
- # include the CampingFilters module in TheApp
2
+ # include the CampingHooks module in TheApp
3
3
  #
4
- module CampingFilters
4
+ module CampingHooks
5
5
  module ClassMethods
6
- def filters
7
- @filters ||= {:before => [], :after => []}
6
+
7
+ def set_hook_key(k)
8
+ @hooks ||= {}
9
+ @hooks[k] ||= []
8
10
  end
9
11
 
10
- def before(actions, &blk)
11
- actions = [actions] unless actions.is_a?(Array)
12
- actions.each do |action|
13
- filters[:before] << [action, blk]
14
- end
12
+ def set_hook_value(k, v)
13
+ set_hook_key(k)
14
+ @hooks[k] << v
15
15
  end
16
16
 
17
- def after(actions, &blk)
18
- actions = [actions] unless actions.is_a?(Array)
19
- actions.each do |action|
20
- filters[:after] << [action, blk]
21
- end
17
+ def get_hooks
18
+ @hooks ||= {}
22
19
  end
20
+
21
+ def hook(hook_pair, &block)
22
+ hook_sym = hook_pair.keys.first
23
+ hook_actions = hook_pair.values.first
24
+ instance_eval <<-EOT
25
+ def #{hook_sym}(actions, &block)
26
+ actions = [actions] unless actions.respond_to?(:each)
27
+ #binding.pry
28
+ actions.each do |action|
29
+ set_hook_value(:#{hook_sym}, [action, block])
30
+ end
31
+ end
32
+ EOT
33
+ send(hook_sym, hook_actions, &block)
34
+ end
35
+
23
36
  end # ClassMethods
24
37
 
25
38
  def self.included(mod)
26
- mod.extend(ClassMethods)
39
+ mod.extend(ClassMethods) # both are class methods, but what Object is @hooks an instance variable of?
27
40
  end
28
41
 
29
- def run_filters(type)
42
+ def run_filter(sym)
30
43
  o = self.class.to_s.split("::")
31
- filters = Object.const_get(o.first).filters
32
- filters[type].each do |filter|
44
+ app = Object.const_get(o.first)
45
+ filters = app.get_hooks
46
+ app.set_hook_key(sym)
47
+ filters[sym].each do |filter|
33
48
  if (filter[0].is_a?(Symbol) && (filter[0] == o.last.to_sym || filter[0] == :all)) ||
34
49
  (filter[0].is_a?(String) && /^#{filter[0]}\/?$/ =~ @env.REQUEST_URI)
35
50
  self.instance_eval(&filter[1])
@@ -39,9 +54,9 @@ module CampingFilters
39
54
 
40
55
  def service(*a) # params to controllers, ids n stuff
41
56
  override_self = catch(:halt) do
42
- run_filters(:before)
57
+ run_filter(:before_service)
43
58
  override_self = super(*a)
44
- run_filters(:after)
59
+ run_filter(:after_service)
45
60
  override_self
46
61
  end
47
62
  override_self
@@ -45,6 +45,6 @@ class Endless
45
45
 
46
46
  end
47
47
 
48
- def endless_path(head, tail)
48
+ def endless_path(head, tail=1) # needs to start at 1 cuz head*1 = head (should be identity product)
49
49
  Endless.new { [[tail, head * tail], endless_path(head, tail+1)] }
50
50
  end
@@ -36,8 +36,8 @@ module Tilt
36
36
  def additive_parsing? ; additive_parsing end
37
37
 
38
38
  def debug # implicit
39
- if $L
40
- #$L.debug(&Proc.new) # pass it along
39
+ if $DEBUG and $L
40
+ $L.debug(&Proc.new) # pass it along
41
41
  end
42
42
  end
43
43
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "tarpaulin"
8
- s.version = "0.3.2"
8
+ s.version = "0.3.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Anthony Durity"]
12
- s.date = "2011-10-30"
12
+ s.date = "2011-11-02"
13
13
  s.description = "So I don't have to copy and paste a million times."
14
14
  s.email = "github@jollyrotten.org"
15
15
  s.extra_rdoc_files = [
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "lib/tarpaulin.rb",
27
27
  "lib/tarpaulin/camping.rb",
28
+ "lib/tarpaulin/camping/accept_language.rb",
28
29
  "lib/tarpaulin/camping/filter.rb",
29
30
  "lib/tarpaulin/camping/flash.rb",
30
31
  "lib/tarpaulin/endless.rb",
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tarpaulin
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 2
10
- version: 0.3.2
9
+ - 3
10
+ version: 0.3.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Anthony Durity
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-30 00:00:00 Z
18
+ date: 2011-11-02 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  version_requirements: &id001 !ruby/object:Gem::Requirement
@@ -161,6 +161,7 @@ files:
161
161
  - VERSION
162
162
  - lib/tarpaulin.rb
163
163
  - lib/tarpaulin/camping.rb
164
+ - lib/tarpaulin/camping/accept_language.rb
164
165
  - lib/tarpaulin/camping/filter.rb
165
166
  - lib/tarpaulin/camping/flash.rb
166
167
  - lib/tarpaulin/endless.rb