From d6a69615a7bcedab601ba579a7c33c3278d5bdc7 Mon Sep 17 00:00:00 2001 From: Jochen Topf Date: Mon, 14 Jan 2013 22:23:37 +0100 Subject: Code cleanup --- web/lib/utils.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'web') diff --git a/web/lib/utils.rb b/web/lib/utils.rb index 298ee64..875584c 100644 --- a/web/lib/utils.rb +++ b/web/lib/utils.rb @@ -1,10 +1,11 @@ # web/lib/utils.rb -# patches convenience methods into base classes +# ------------------------------------------------------------------------------ +# patch some convenience methods into base classes class Fixnum - # convert to string with this space as thousand separator + # convert to string with thin space as thousand separator def to_s_with_ts self.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1 ") end @@ -37,11 +38,15 @@ class Numeric end class Float + def round_to(n=0) (self * (10.0 ** n)).round * (10.0 ** (-n)) end + end +# ------------------------------------------------------------------------------ + def title @title = [] if @title.nil? @title = [@title] unless @title.is_a?(Array) @@ -54,6 +59,8 @@ def section(id) @section_title = (@section =~ /^(keys|tags)$/) ? t.osm[@section] : t.taginfo[@section] end +# ------------------------------------------------------------------------------ + # Escape tag key or value for XAPI according to # http://wiki.openstreetmap.org/wiki/XAPI#Escaping def xapi_escape(text) @@ -88,6 +95,8 @@ def wiki_link(title) external_link('wikilink_' + title.gsub(%r{[^A-Za-z0-9]}, '_'), title, prefix + title) end +# ------------------------------------------------------------------------------ + def tagcloud_size(tag) x = tag['scale1'].to_f / 20 + tag['pos'] / 4 (x * 40 + 12).to_i @@ -110,6 +119,8 @@ def get_total(type) return @db.stats(key) end +# ------------------------------------------------------------------------------ + # Like the 'get' method but will add a redirect for the same path with trailing / added def get!(path, &block) get path, &block @@ -124,6 +135,8 @@ def api(version, path, doc=nil, &block) get("/api/#{version}/#{path}", &block) end +# ------------------------------------------------------------------------------ + # Used in wiki api calls def get_wiki_result(res) return res.map{ |row| { -- cgit v1.2.3