Module: Sinatra::Helpers
- Defined in:
- lib/sinatra/helpers.rb,
lib/sinatra/helpers/country.rb,
lib/sinatra/helpers/haml_error_presenter.rb
Defined Under Namespace
Modules: Country Classes: HamlErrorPresenter
Constant Summary collapse
- VERSION =
"0.2.0"
Class Method Summary collapse
Instance Method Summary collapse
-
#country_choices ⇒ Array
Returns an array of pairs i.e.
-
#currency(number, opts = {}) ⇒ String?
Formats a number into a currency display.
-
#day_choices ⇒ Array
Returns an array of pairs i.e.
-
#errors_on(object, options = { :class => 'errors' }) {|Sinatra::Helpers::HamlErrorPresenter| ... } ⇒ Object
Presents errors on your form.
-
#h(str) ⇒ Object
Returns an HTML sanitized string.
-
#month_choices(month_names = settings.default_month_names) ⇒ Array
Returns an array of pairs i.e.
-
#percentage(number, precision = 2) ⇒ String?
Show the percentage representation of a numeric value.
-
#select_options(pairs, current = nil, prompt = nil) ⇒ Object
Accepts a list of pairs and produces option tags.
-
#year_choices(loffset = settings.default_year_loffset, uoffset = settings.default_year_uoffset) ⇒ Array
Returns an array of pairs i.e.
Class Method Details
.registered(app) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/sinatra/helpers.rb', line 14 def self.registered(app) app.set :default_year_loffset, -60 app.set :default_year_uoffset, 0 app.set :default_month_names, Date::MONTHNAMES app.set :default_currency_unit, '$' app.set :default_currency_precision, 2 app.set :default_currency_separator, ',' end |
Instance Method Details
#country_choices ⇒ Array
Returns an array of pairs i.e.
- “Afghanistan”, “AF”
-
…
- “United States”, “US”
-
…
- “Zimbabwe”, “ZW”
38 39 40 |
# File 'lib/sinatra/helpers.rb', line 38 def country_choices Country.to_select end |
#currency(number, opts = {}) ⇒ String?
Formats a number into a currency display. Uses the following settings:
-
settings.default_currency_unit (defaults to ‘$’)
-
settings.default_currency_precision (defaults to 2)
-
settings.default_currenty_separator (defaults to ‘,’)
211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/sinatra/helpers.rb', line 211 def currency(number, opts = {}) return if number.to_s.empty? unit = opts[:unit] || settings.default_currency_unit precision = opts[:precision] || settings.default_currency_precision separator = opts[:separator] || settings.default_currency_separator ret = "%s %.#{ Integer(precision) }f" % [unit, number] parts = ret.split('.') parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{separator}") parts.join('.') end |
#day_choices ⇒ Array
Returns an array of pairs i.e.
- 1, 1
- 2, 2
-
…
- 31, 31
50 51 52 53 |
# File 'lib/sinatra/helpers.rb', line 50 def day_choices days = (1..31).to_a days.zip(days) end |
#errors_on(object, options = { :class => 'errors' }) {|Sinatra::Helpers::HamlErrorPresenter| ... } ⇒ Object
Presents errors on your form. Takes the explicit approach and assumes that for every form you have, the copy for the errors are important, instead of producing canned responses.
Allows you to do the following in your haml view:
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/sinatra/helpers.rb', line 161 def errors_on(object, = { :class => 'errors' }, &block) return if object.errors.empty? lines = if object.errors.respond_to?(:full_messages) object.errors. else HamlErrorPresenter.new(object.errors).present(self, &block) end haml_tag(:div, ) do haml_tag(:ul) do lines.each do |error| haml_tag(:li, error) end end end end |
#h(str) ⇒ Object
Returns an HTML sanitized string.
25 26 27 |
# File 'lib/sinatra/helpers.rb', line 25 def h(str) Rack::Utils.escape_html(str) end |
#month_choices(month_names = settings.default_month_names) ⇒ Array
Returns an array of pairs i.e.
- ‘January’, 1
- ‘February’, 2
-
…
- ‘December’, 12
You may pass in Date::ABBR_MONTHNAMES if you want the shortened month names.
66 67 68 69 70 |
# File 'lib/sinatra/helpers.rb', line 66 def month_choices(month_names = settings.default_month_names) month_names.map. with_index { |month, idx| [month, idx] }. tap { |arr| arr.shift } end |
#percentage(number, precision = 2) ⇒ String?
Show the percentage representation of a numeric value.
234 235 236 237 238 239 |
# File 'lib/sinatra/helpers.rb', line 234 def percentage(number, precision = 2) return if number.to_s.empty? ret = "%02.#{ precision }f%" % number ret.gsub(/\.0*%$/, '%') end |
#select_options(pairs, current = nil, prompt = nil) ⇒ Object
Accepts a list of pairs and produces option tags.
122 123 124 125 126 127 128 |
# File 'lib/sinatra/helpers.rb', line 122 def (pairs, current = nil, prompt = nil) pairs.unshift([prompt, '']) if prompt pairs.map { |label, value| tag(:option, label, :value => value, :selected => (current == value)) }.join("\n") end |
#year_choices(loffset = settings.default_year_loffset, uoffset = settings.default_year_uoffset) ⇒ Array
Returns an array of pairs i.e.
- 2005, 2005
- 2006, 2006
-
…
- 2010, 2010
100 101 102 103 |
# File 'lib/sinatra/helpers.rb', line 100 def year_choices(loffset = settings.default_year_loffset, uoffset = settings.default_year_uoffset) years = ((Date.today.year + loffset)..(Date.today.year + uoffset)).to_a years.zip(years) end |