dugway 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e58f9b8d861b19265772a0bb454ae1a9cac1140c
4
- data.tar.gz: 2af6e3d778fff5dbf60948d2e940ad1bdc07eb47
3
+ metadata.gz: 1084a19f3f1373dbc3f1ca375b4003d236cd0385
4
+ data.tar.gz: 7212879ca2ec43c38f495ac9dcbc6dad2b06a615
5
5
  SHA512:
6
- metadata.gz: 38c26f7daab824b8ebfba0f644582d4b67d249c7e00ddc7145eaab096d4f064dfd1814389131f7c18f542cf90e67c036a8cc774acad05a56b73a0585d9ec5d7b
7
- data.tar.gz: aa02501381d7a971a5cf26b054d14d298fbe11b20d370b92a7bb4d99ee0a3278f06c1c0dac2b92f0c9cd423a2103c60e28701a7e530dba62523dfee95eb87746
6
+ metadata.gz: aed377b55bc5bea4ff270a32a832958274db29c4029eca6771529b71d1b6c6be22dbe0fcf4867e7986a69041b2e4b42a02b0914ccebe409e30045eec72cff7aa
7
+ data.tar.gz: cb900d1a0cc36f331ab61a06cedbb40187a0da9afe77ccf37fb5fc768c6add12d43a216d1fd6a80bf7d6cabdd6d0422e222f6bdc246618ab24d0d89e5dab0267
data/lib/dugway.rb CHANGED
@@ -22,7 +22,7 @@ module Dugway
22
22
  def application(options={})
23
23
  @options = options
24
24
 
25
- I18n.load_path += Dir[File.join(File.dirname(__FILE__), 'data', 'locales', '*.yml').to_s]
25
+ I18n.load_path += Dir[File.join(File.dirname(__FILE__), 'dugway', 'data', 'locales', '*.yml').to_s]
26
26
  I18n.default_locale = 'en-US'
27
27
  I18n.locale = Dugway.store.locale
28
28
 
@@ -36,20 +36,20 @@ module Dugway
36
36
  # Money Filters
37
37
 
38
38
  def money(amount)
39
- number_to_currency(amount, :locale => nil, :precision => I18n.translate('number.currency.format.precision'), :unit => '', :separator => I18n.translate('number.currency.format.separator'), :delimiter => I18n.translate('number.currency.format.delimiter'), :format => I18n.translate('number.currency.format.format')).strip
39
+ number_to_currency(amount, :unit => '').strip
40
40
  end
41
41
 
42
42
  def money_with_sign(amount)
43
43
  unit = I18n.translate('number.currency.format.unit')
44
- number_to_currency(amount).try(:gsub, unit, "<span class=\"currency_sign\">#{ HTMLEntities.new.encode(unit, :named) }</span>")
44
+ number_to_currency(amount).gsub(unit, %{<span class="currency_sign">#{ HTMLEntities.new.encode(unit, :named) }</span>})
45
45
  end
46
46
 
47
47
  def money_with_code(amount)
48
- %{#{ money(amount) } <span class="currency_code">#{ currency.code }</span>}
48
+ %{#{ money(amount) } <span class="currency_code">#{ currency['code'] }</span>}
49
49
  end
50
50
 
51
51
  def money_with_sign_and_code(amount)
52
- %{#{ money_with_sign(amount) } <span class="currency_code\">#{ currency.code }</span>}
52
+ %{#{ money_with_sign(amount) } <span class="currency_code">#{ currency['code'] }</span>}
53
53
  end
54
54
 
55
55
  # Shipping Filters
@@ -10,12 +10,12 @@ module Dugway
10
10
  end
11
11
 
12
12
  # Mimics
13
-
13
+
14
14
  DEFAULT_CURRENCY_VALUES = { :format => "%u%n", :negative_format => "-%u%n", :unit => "$", :separator => ".", :delimiter => ",", :precision => 2, :significant => false, :strip_insignificant_zeros => false }
15
-
15
+
16
16
  def number_to_currency(number, options={})
17
17
  return unless number
18
-
18
+
19
19
  options.symbolize_keys!
20
20
 
21
21
  defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {})
@@ -33,25 +33,74 @@ module Dugway
33
33
  format = options.delete(:negative_format)
34
34
  number = number.respond_to?("abs") ? number.abs : number.sub(/^-/, '')
35
35
  end
36
-
37
- value = number_with_precision(number, options[:precision])
36
+
37
+ value = number_with_precision(number, options)
38
38
  format.gsub(/%n/, value).gsub(/%u/, unit)
39
39
  end
40
40
 
41
- def number_with_delimiter(number, delimiter=",", separator=".")
41
+ def number_with_delimiter(number, options={})
42
+ options.symbolize_keys!
43
+
42
44
  begin
43
- parts = number.to_s.split('.')
44
- parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
45
- parts.join separator
46
- rescue
47
- number
45
+ Float(number)
46
+ rescue ArgumentError, TypeError
47
+ if options[:raise]
48
+ raise InvalidNumberError, number
49
+ else
50
+ return number
51
+ end
48
52
  end
53
+
54
+ defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {})
55
+ options = options.reverse_merge(defaults)
56
+
57
+ parts = number.to_s.to_str.split('.')
58
+ parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}")
59
+ parts.join(options[:separator]).html_safe
49
60
  end
50
61
 
51
- def number_with_precision(number, precision=3)
52
- "%01.#{precision}f" % number
53
- rescue
54
- number
62
+ def number_with_precision(number, options={})
63
+ options.symbolize_keys!
64
+
65
+ number = begin
66
+ Float(number)
67
+ rescue ArgumentError, TypeError
68
+ if options[:raise]
69
+ raise InvalidNumberError, number
70
+ else
71
+ return number
72
+ end
73
+ end
74
+
75
+ defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {})
76
+ precision_defaults = I18n.translate(:'number.precision.format', :locale => options[:locale], :default => {})
77
+ defaults = defaults.merge(precision_defaults)
78
+
79
+ options = options.reverse_merge(defaults) # Allow the user to unset default values: Eg.: :significant => false
80
+ precision = options.delete :precision
81
+ significant = options.delete :significant
82
+ strip_insignificant_zeros = options.delete :strip_insignificant_zeros
83
+
84
+ if significant and precision > 0
85
+ if number == 0
86
+ digits, rounded_number = 1, 0
87
+ else
88
+ digits = (Math.log10(number.abs) + 1).floor
89
+ rounded_number = (BigDecimal.new(number.to_s) / BigDecimal.new((10 ** (digits - precision)).to_f.to_s)).round.to_f * 10 ** (digits - precision)
90
+ digits = (Math.log10(rounded_number.abs) + 1).floor # After rounding, the number of digits may have changed
91
+ end
92
+ precision -= digits
93
+ precision = precision > 0 ? precision : 0 #don't let it be negative
94
+ else
95
+ rounded_number = BigDecimal.new(number.to_s).round(precision).to_f
96
+ end
97
+ formatted_number = number_with_delimiter("%01.#{precision}f" % rounded_number, options)
98
+ if strip_insignificant_zeros
99
+ escaped_separator = Regexp.escape(options[:separator])
100
+ formatted_number.sub(/(#{escaped_separator})(\d*[1-9])?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '').html_safe
101
+ else
102
+ formatted_number
103
+ end
55
104
  end
56
105
 
57
106
  def tag(name, options = nil, open = false, escape = true)
@@ -1,3 +1,3 @@
1
1
  module Dugway
2
- VERSION = "0.6.1"
2
+ VERSION = "0.6.2"
3
3
  end
@@ -24,9 +24,33 @@ describe Dugway::Filters::CoreFilters do
24
24
  end
25
25
  end
26
26
 
27
+ describe "#money" do
28
+ it "should convert a number to currency format" do
29
+ rendered_template("{{ 1234.56 | money }}").should == '1,234.56'
30
+ end
31
+ end
32
+
33
+ describe "#money_with_sign" do
34
+ it "should convert a number to currency format with a sign" do
35
+ rendered_template("{{ 1234.56 | money_with_sign }}").should == '<span class="currency_sign">$</span>1,234.56'
36
+ end
37
+ end
38
+
39
+ describe "#money_with_code" do
40
+ it "should convert a number to currency format with a code" do
41
+ rendered_template("{{ 1234.56 | money_with_code }}").should == '1,234.56 <span class="currency_code">USD</span>'
42
+ end
43
+ end
44
+
45
+ describe "#money_with_sign_and_code" do
46
+ it "should convert a number to currency format with a sign and code" do
47
+ rendered_template("{{ 1234.56 | money_with_sign_and_code }}").should == '<span class="currency_sign">$</span>1,234.56 <span class="currency_code">USD</span>'
48
+ end
49
+ end
50
+
27
51
  private
28
52
 
29
53
  def rendered_template(template, assigns={})
30
- Liquid::Template.parse(template).render(assigns)
54
+ Liquid::Template.parse(template).render(assigns, :registers => { :currency => Dugway.store.currency })
31
55
  end
32
56
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dugway
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Big Cartel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-16 00:00:00.000000000 Z
11
+ date: 2013-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler