BrianTheCoder-ratpack 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "ratpack"
8
+ gem.summary = %Q{A collection of helpers I wanted for sinatra, thought I'd share}
9
+ gem.email = "wbsmith83@gmail.com"
10
+ gem.homepage = "http://github.com/BrianTheCoder/ratpack"
11
+ gem.authors = ["brianthecoder"]
12
+
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = false
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+
40
+ task :default => :test
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "Rat Pack #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 2
3
- :patch: 0
3
+ :patch: 2
4
4
  :major: 0
data/lib/ratpack.rb CHANGED
@@ -2,7 +2,7 @@ require 'sinatra/base'
2
2
  require 'extlib'
3
3
 
4
4
  module RatPack
5
- VERSION = '0.0.1'
5
+ VERSION = '0.2.2'
6
6
  LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
7
7
  PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
8
8
 
@@ -22,9 +22,9 @@ module RatPack
22
22
  dir ||= ::File.basename(fname, '.*')
23
23
  search_me = ::File.expand_path(
24
24
  ::File.join(::File.dirname(fname), dir, '**', '*.rb'))
25
-
26
25
  Dir.glob(search_me).sort.each {|rb| require rb}
27
26
  end
28
- end # module VelvetRope
27
+ end # module RatPack
29
28
 
30
- RatPack.require_all_libs_relative_to(__FILE__)
29
+ RatPack.require_all_libs_relative_to(__FILE__)
30
+ require File.join(RatPack::LIBPATH,'sinatra','ratpack')
@@ -0,0 +1,16 @@
1
+ class Hash
2
+ # Returns the value of self for each argument and deletes those entries.
3
+ #
4
+ # ==== Parameters
5
+ # *args:: the keys whose values should be extracted and deleted.
6
+ #
7
+ # ==== Returns
8
+ # Array[Object]:: The values of the provided arguments in corresponding order.
9
+ #
10
+ # :api: public
11
+ def extract!(*args)
12
+ args.map do |arg|
13
+ self.delete(arg)
14
+ end
15
+ end
16
+ end
data/lib/ratpack/forms.rb CHANGED
@@ -1,126 +1,83 @@
1
1
  module RatPack
2
2
  module Forms
3
- # def error_messages_for(obj = nil, opts = {})
4
- # current_form_context.error_messages_for(obj, opts[:error_class] || "error",
5
- # opts[:build_li] || "<li>%s</li>",
6
- # opts[:header] || "<h2>Form submission failed because of %s problem%s</h2>",
7
- # opts.key?(:before) ? opts[:before] : true)
8
- # end
3
+ def error_messages_for(obj = nil, opts = {})
4
+ return unless obj.respond_to?(:errors)
5
+ html = tag(:h2, "Form submission failed be cause of #{obj.errors.size} #{pluralize("error",obj.errors.size)}")
6
+ obj.errors.each do |field,msg|
7
+ html << tag(:li,msg)
8
+ end
9
+ end
9
10
 
10
11
  %w(text password hidden file).each do |kind|
11
12
  self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
12
13
  def #{kind}_field(attrs)
13
- update_unbound_controls(attrs, "#{kind}")
14
- label = "#{kind}" == "hidden" ? "" : unbound_label(attrs)
15
- label + self_closing_tag(:input, {:type => "#{kind}"}.merge(attrs))
14
+ attrs[:class] = "text" if %w(text password).include?("#{kind}")
15
+ closed_form_field(:input, {:type => "#{kind}"}.merge(attrs))
16
16
  end
17
17
  RUBY
18
18
  end
19
-
20
- def check_box(attrs)
21
- update_unbound_controls(attrs, "checkbox")
22
- if attrs.delete(:boolean)
23
- on, off = attrs.delete(:on), attrs.delete(:off)
24
- unbound_hidden_field(:name => attrs[:name], :value => off) <<
25
- self_closing_tag(:input, {:type => "checkbox", :value => on}.merge(attrs)) + unbound_label(attrs)
26
- else
27
- self_closing_tag(:input, {:type => "checkbox"}.merge(attrs)) + unbound_label(attrs)
28
- end
19
+
20
+ def text_area(attrs = {})
21
+ form_field(:textarea, attrs.delete(:value) || "", attrs)
29
22
  end
30
-
23
+
24
+ def button(contents, attrs = {})
25
+ form_field(:button, contents, attrs)
26
+ end
27
+
31
28
  def radio_button(attrs)
32
- update_unbound_controls(attrs, "radio")
33
- self_closing_tag(:input, {:type => "radio"}.merge(attrs)) + unbound_label(attrs)
29
+ closed_form_field(:input, {:type => "radio"}.merge(attrs))
34
30
  end
35
31
 
36
32
  def radio_group(arr, attrs = {})
37
33
  arr.map do |ind_attrs|
38
34
  ind_attrs = {:value => ind_attrs} unless ind_attrs.is_a?(Hash)
39
35
  joined = attrs.merge(ind_attrs)
40
- joined.merge!(:label => joined[:label] || joined[:value])
41
36
  radio_button(joined)
42
37
  end.join
43
38
  end
44
39
 
45
- def text_area(contents, attrs)
46
- update_unbound_controls(attrs, "text_area")
47
- unbound_label(attrs) + tag(:textarea, contents, attrs)
48
- end
49
-
50
- def label(contents, attrs = {})
51
- if contents
52
- if contents.is_a?(Hash)
53
- label_attrs = contents
54
- contents = label_attrs.delete(:title)
55
- else
56
- label_attrs = attrs
57
- end
58
- tag(:label, contents, label_attrs)
40
+ def check_box(attrs)
41
+ if attrs.delete(:boolean)
42
+ on, off = attrs.delete(:on), attrs.delete(:off)
43
+ hidden_field(:name => attrs[:name], :value => off) <<
44
+ closed_form_field(:input, {:type => "checkbox", :value => on}.merge(attrs))
59
45
  else
60
- ""
46
+ closed_form_field(:input, {:type => "checkbox"}.merge(attrs))
61
47
  end
62
48
  end
63
49
 
64
50
  def select(attrs = {})
65
- update_unbound_controls(attrs, "select")
66
51
  attrs[:name] << "[]" if attrs[:multiple] && !(attrs[:name] =~ /\[\]$/)
67
- tag(:select, options_for(attrs), attrs)
52
+ form_field(:select, options_for(attrs), attrs)
68
53
  end
69
54
 
70
- def button(contents, attrs)
71
- update_unbound_controls(attrs, "button")
72
- tag(:button, contents, attrs)
73
- end
74
-
75
- def submit(value, attrs)
55
+ def submit(value, attrs = {})
76
56
  attrs[:type] ||= "submit"
77
57
  attrs[:name] ||= "submit"
78
58
  attrs[:value] ||= value
79
- update_unbound_controls(attrs, "submit")
80
- self_closing_tag(:input, attrs)
59
+ closed_form_field(:input, attrs)
81
60
  end
82
61
 
83
- # def form(attrs = {}, &blk)
84
- # captured = @origin.capture(&blk)
85
- # fake_method_tag = process_form_attrs(attrs)
86
- # tag(:form, fake_method_tag + captured, attrs)
87
- # end
88
- #
89
- # def fieldset(attrs, &blk)
90
- # captured = @origin.capture(&blk)
91
- # legend = (l_attr = attrs.delete(:legend)) ? tag(:legend, l_attr) : ""
92
- # tag(:fieldset, legend + captured, attrs)
93
- # end
94
-
95
62
  private
96
-
97
- def process_form_attrs(attrs)
98
- method = attrs[:method] || :post
99
- attrs[:enctype] = "multipart/form-data" if attrs.delete(:multipart) || @multipart
100
- method == :post || method == :get ? "" : fake_out_method(attrs, method)
63
+
64
+ def form_field(type, content, attrs)
65
+ attrs[:id] = attrs[:name] unless attrs.has_key?(:id)
66
+ build_field(attrs) + tag(type, content, attrs)
101
67
  end
102
-
103
- # This can be overridden to use another method to fake out methods
104
- def fake_out_method(attrs, method)
105
- self_closing_tag(:input, :type => "hidden", :name => "_method", :value => method)
68
+
69
+ def closed_form_field(type, attrs)
70
+ attrs[:id] = attrs[:name] unless attrs.has_key?(:id)
71
+ build_field(attrs) + self_closing_tag(type, attrs)
106
72
  end
107
73
 
108
- def update_unbound_controls(attrs, type)
109
- case type
110
- when "checkbox"
111
- update_unbound_check_box(attrs)
112
- when "radio"
113
- update_unbound_radio_button(attrs)
114
- when "file"
115
- @multipart = true
116
- when "text","password"
117
- attrs[:class] = "text"
118
- end
119
-
120
- attrs[:disabled] ? attrs[:disabled] = "disabled" : attrs.delete(:disabled)
74
+ def build_field(attrs)
75
+ label = attrs.has_key?(:label) ? build_label(attrs) : ""
76
+ hint = attrs.has_key?(:hint) ? tag(:div,attrs.delete(:hint), :class => "hint") : ""
77
+ label + hint
121
78
  end
122
79
 
123
- def unbound_label(attrs = {})
80
+ def build_label(attrs)
124
81
  if attrs[:id]
125
82
  label_attrs = {:for => attrs[:id]}
126
83
  elsif attrs[:name]
@@ -128,64 +85,32 @@ module RatPack
128
85
  else
129
86
  label_attrs = {}
130
87
  end
131
-
132
- label_option = attrs.delete(:label)
133
- if label_option.is_a? Hash
134
- label(label_attrs.merge(label_option))
135
- else
136
- label(label_option, label_attrs)
137
- end
138
- end
139
-
140
- def update_unbound_check_box(attrs)
141
- boolean = attrs[:boolean] || (attrs[:on] && attrs[:off]) ? true : false
142
-
143
- case
144
- when attrs.key?(:on) ^ attrs.key?(:off)
145
- raise ArgumentError, ":on and :off must be specified together"
146
- when (attrs[:boolean] == false) && (attrs.key?(:on))
147
- raise ArgumentError, ":boolean => false cannot be used with :on and :off"
148
- when boolean && attrs.key?(:value)
149
- raise ArgumentError, ":value can't be used with a boolean checkbox"
150
- end
151
-
152
- if attrs[:boolean] = boolean
153
- attrs[:on] ||= "1"; attrs[:off] ||= "0"
154
- end
155
-
156
- attrs[:checked] = "checked" if attrs.delete(:checked)
157
- end
158
-
159
- def update_unbound_radio_button(attrs)
160
- attrs[:checked] = "checked" if attrs.delete(:checked)
88
+ tag(:label, attrs.delete(:label), label_attrs)
161
89
  end
162
90
 
163
91
  def options_for(attrs)
164
92
  blank, prompt = attrs.delete(:include_blank), attrs.delete(:prompt)
165
93
  b = blank || prompt ? tag(:option, prompt || "", :value => "") : ""
166
-
167
94
  # yank out the options attrs
168
- collection, selected, text_method, value_method =
169
- attrs.extract!(:collection, :selected, :text_method, :value_method)
170
-
95
+ collection, selected, text_method, value_method = attrs.extract!(:collection, :selected, :text_method, :value_method)
171
96
  # if the collection is a Hash, optgroups are a-coming
172
97
  if collection.is_a?(Hash)
173
98
  ([b] + collection.map do |g,col|
174
- tag(:optgroup, options(col, text_method, value_method, selected), :label => g)
99
+ tag(:optgroup, select_options(col, text_method, value_method, selected), :label => g)
175
100
  end).join
176
101
  else
177
- options(collection || [], text_method, value_method, selected, b)
102
+ select_options(collection || [], text_method, value_method, selected, b)
178
103
  end
179
104
  end
180
-
181
- def options(col, text_meth, value_meth, sel, b = nil)
105
+
106
+ def select_options(col, text_meth, value_meth, sel, b = nil)
182
107
  ([b] + col.map do |item|
183
108
  text_meth = text_meth && item.respond_to?(text_meth) ? text_meth : :last
184
109
  value_meth = value_meth && item.respond_to?(value_meth) ? value_meth : :first
185
-
110
+
186
111
  text = item.is_a?(String) ? item : item.send(text_meth)
187
112
  value = item.is_a?(String) ? item : item.send(value_meth)
188
-
113
+
189
114
  option_attrs = {:value => value}
190
115
  if sel.is_a?(Array)
191
116
  option_attrs.merge!(:selected => "selected") if value.in? sel
@@ -1,5 +1,9 @@
1
1
  module RatPack
2
- module HtmlHelpers
2
+ module HtmlHelpers
3
+ def pluralize(str,num)
4
+ num.to_i > 1 ? str.plural : str.singular
5
+ end
6
+
3
7
  def link_to(name, url, options = {})
4
8
  defaults = {:href => url}
5
9
  tag(:a,name,defaults.merge(options))
@@ -7,6 +7,6 @@ module RatPack
7
7
  else
8
8
  []
9
9
  end
10
- end
10
+ end
11
11
  end
12
12
  end
data/lib/ratpack/tag.rb CHANGED
@@ -2,7 +2,7 @@ module RatPack
2
2
  module Tag
3
3
  def tag(name, contents = nil, attrs = {}, &block)
4
4
  attrs, contents = contents, nil if contents.is_a?(Hash)
5
- contents = capture(&block) if block_given?
5
+ # contents = capture(&block) if block_given?
6
6
  open_tag(name, attrs) + contents.to_s + close_tag(name)
7
7
  end
8
8
 
@@ -1,17 +1,12 @@
1
1
  module Sinatra
2
- module Ratpack
3
- module Helpers
4
- include RatPack::Tag
5
- include RatPack::HtmlHelpers
6
- include RatPack::Forms
7
- include RatPack::Routes
8
- end
9
-
2
+ module RatPack
10
3
  def self.registered(app)
11
- puts 'loading ratpack'
12
- app.helpers Helpers
4
+ app.helpers ::RatPack::Tag
5
+ app.helpers ::RatPack::HtmlHelpers
6
+ app.helpers ::RatPack::Routes
7
+ app.helpers ::RatPack::Forms
13
8
  end
14
9
  end
15
10
 
16
- register Ratpack
11
+ register RatPack
17
12
  end # Sinatra
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: BrianTheCoder-ratpack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - brianthecoder
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-07 00:00:00 -07:00
12
+ date: 2009-04-12 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -20,27 +20,26 @@ executables: []
20
20
  extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
- - README.rdoc
24
23
  - LICENSE
24
+ - README.rdoc
25
25
  files:
26
+ - LICENSE
26
27
  - README.rdoc
28
+ - Rakefile
27
29
  - VERSION.yml
28
- - lib/ratpack
30
+ - lib/ratpack.rb
31
+ - lib/ratpack/core_ext/hash.rb
29
32
  - lib/ratpack/forms.rb
30
33
  - lib/ratpack/html_helpers.rb
31
34
  - lib/ratpack/routes.rb
32
35
  - lib/ratpack/tag.rb
33
- - lib/ratpack.rb
34
- - lib/sinatra
35
36
  - lib/sinatra/ratpack.rb
36
37
  - test/ratpack_test.rb
37
38
  - test/test_helper.rb
38
- - LICENSE
39
39
  has_rdoc: true
40
40
  homepage: http://github.com/BrianTheCoder/ratpack
41
41
  post_install_message:
42
42
  rdoc_options:
43
- - --inline-source
44
43
  - --charset=UTF-8
45
44
  require_paths:
46
45
  - lib
@@ -63,5 +62,6 @@ rubygems_version: 1.2.0
63
62
  signing_key:
64
63
  specification_version: 2
65
64
  summary: A collection of helpers I wanted for sinatra, thought I'd share
66
- test_files: []
67
-
65
+ test_files:
66
+ - test/ratpack_test.rb
67
+ - test/test_helper.rb