simplificator-has_setting 0.3.8 → 0.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 3
3
- :patch: 8
3
+ :patch: 9
4
4
  :major: 0
data/lib/has_setting.rb CHANGED
@@ -9,5 +9,8 @@ end
9
9
 
10
10
 
11
11
  HasSetting::Formatters.register_formatter(:string, HasSetting::Formatters::StringFormatter.new)
12
+ HasSetting::Formatters.register_formatter(:strings, HasSetting::Formatters::StringsFormatter.new)
12
13
  HasSetting::Formatters.register_formatter(:float, HasSetting::Formatters::FloatFormatter.new)
13
- HasSetting::Formatters.register_formatter(:int, HasSetting::Formatters::IntFormatter.new)
14
+ HasSetting::Formatters.register_formatter(:floats, HasSetting::Formatters::FloatFormatter.new)
15
+ HasSetting::Formatters.register_formatter(:int, HasSetting::Formatters::IntFormatter.new)
16
+ HasSetting::Formatters.register_formatter(:ints, HasSetting::Formatters::IntsFormatter.new)
@@ -36,9 +36,7 @@ module HasSetting
36
36
  # Callback to save settings
37
37
  def save_has_setting_association
38
38
  self.settings.each do |setting|
39
- if setting.changed?
40
- setting.save
41
- end
39
+ setting.save if setting.changed?
42
40
  end
43
41
  end
44
42
  end
@@ -63,14 +61,8 @@ module HasSetting
63
61
  setting = read_setting(name)
64
62
  options = args.first || self.class.has_setting_options[name]
65
63
  return options[:default] if setting.nil?
66
- formatter = HasSetting::Formatters.for_type(options[:type])
64
+ formatter = Formatters.for_type(options[:type])
67
65
  formatter.to_type(setting.value)
68
- #case options[:type]
69
- # when :string : setting.value
70
- # when :int : setting.value.nil? ? nil : setting.value.to_i
71
- # when :float : setting.value.nil? ? nil : setting.value.to_f
72
- # else raise ArgumentError.new("Unsupported type: #{options[:type]}")
73
- #end
74
66
  end
75
67
  end
76
68
  end
@@ -43,6 +43,38 @@ module HasSetting
43
43
  value.to_s
44
44
  end
45
45
  end
46
+
47
+ class IntsFormatter < NilSafeFormatter
48
+ def safe_to_type(value)
49
+ value.split(',').map() {|item| Formatters.for_type(:int).to_type(item)}
50
+ end
51
+ def safe_to_s(value)
52
+ Array(value).map() {|item| Formatters.for_type(:int).to_s(item)}.join(',')
53
+ end
54
+ end
55
+
56
+ class FloatsFormatter < NilSafeFormatter
57
+ def safe_to_type(value)
58
+ value.split(',').map() {|item| Formatters.for_type(:float).to_type(item)}
59
+ end
60
+ def safe_to_s(value)
61
+ Array(value).map() {|item| Formatters.for_type(:float).to_s(item)}.join(',')
62
+ end
63
+ end
64
+
65
+ class StringsFormatter < NilSafeFormatter
66
+ def safe_to_type(value)
67
+ # Ruby does not know "negative look before". Or i dont know how to do it in ruby. Thus
68
+ # i ended up using some reverse calls... ugly. Anyone out there eager to help me out?
69
+ value.reverse.split(/,(?!\\)/).map() {|item| item.reverse.gsub('\,', ',')}.reverse
70
+ end
71
+ def safe_to_s(value)
72
+ # Escape the separator character ',' with a backslash
73
+ Array(value).map() {|item| item.gsub(',', '\,')}.join(',')
74
+ end
75
+ end
76
+
77
+
46
78
  # Formatter for ints
47
79
  # Throws ArgumentError if value can not be converted
48
80
  class IntFormatter < NilSafeFormatter
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../test_helper'
2
2
  include HasSetting
3
3
  class FormattersTest < Test::Unit::TestCase
4
4
  def test_for_type
5
- [:string, :float, :int].each do |symbol|
5
+ [:string, :float, :floats, :int, :ints, :strings].each do |symbol|
6
6
  assert(Formatters.for_type(symbol), "No formatter for #{symbol}")
7
7
  end
8
8
  assert_raises(ArgumentError) do
@@ -10,6 +10,23 @@ class FormattersTest < Test::Unit::TestCase
10
10
  end
11
11
  end
12
12
 
13
+ def test_strings_formatter
14
+ f = Formatters::StringsFormatter.new
15
+ assert_equal(nil, f.to_s(nil))
16
+ assert_equal('bla', f.to_s('bla'))
17
+ assert_equal('bla', f.to_s(['bla']))
18
+ assert_equal('bla,bli', f.to_s(['bla', 'bli']))
19
+ assert_equal('\,schni\,schna\,,bli,\,bla', f.to_s([',schni,schna,', 'bli', ',bla']))
20
+ assert_equal('\,\,\,\,,\,\,\,,\,\,,\,', f.to_s([',,,,', ',,,', ',,', ',']))
21
+
22
+ assert_equal(nil, f.to_type(nil))
23
+ assert_equal([], f.to_type(''))
24
+ assert_equal(['bli'], f.to_type('bli'))
25
+ assert_equal(['bli','', 'bla'], f.to_type('bli,,bla'))
26
+ assert_equal([',schni,schna,', 'bli', ',bla'], f.to_type('\,schni\,schna\,,bli,\,bla'))
27
+ assert_equal([',,,,', ',,,', ',,', ','], f.to_type('\,\,\,\,,\,\,\,,\,\,,\,'))
28
+ end
29
+
13
30
  def test_string_formatter()
14
31
  f = Formatters::StringFormatter.new
15
32
  assert_equal('', f.to_s(''))
@@ -39,6 +56,7 @@ class FormattersTest < Test::Unit::TestCase
39
56
  assert_nil(f.to_type(nil))
40
57
  assert_equal(2, f.to_type('2'))
41
58
  assert_equal(2, f.to_type('2.6'))
59
+ assert_equal(2, f.to_type(' 2.6 '))
42
60
  end
43
61
 
44
62
  def test_float_formatter()
@@ -63,4 +81,31 @@ class FormattersTest < Test::Unit::TestCase
63
81
  assert_equal(2.0, f.to_type('2'))
64
82
  assert_equal(2.6, f.to_type('2.6'))
65
83
  end
84
+
85
+ def test_ints_formatter
86
+ f = Formatters::IntsFormatter.new
87
+ assert_equal(nil, f.to_s(nil))
88
+ assert_equal('1', f.to_s(1))
89
+ assert_equal('1', f.to_s([1]))
90
+ assert_equal('1,2', f.to_s([1,2]))
91
+
92
+ assert_equal(nil, f.to_type(nil))
93
+ assert_equal([], f.to_type(''))
94
+ assert_equal([1], f.to_type('1'))
95
+ assert_equal([1,2], f.to_type('1,2'))
96
+ end
97
+ def test_floats_formatter
98
+ f = Formatters::FloatsFormatter.new
99
+ assert_equal(nil, f.to_s(nil))
100
+ assert_equal('1.2', f.to_s(1.2))
101
+ assert_equal('1.2', f.to_s([1.2]))
102
+ assert_equal('1.2,1.3', f.to_s([1.2,1.3]))
103
+
104
+ assert_equal(nil, f.to_type(nil))
105
+ assert_equal([], f.to_type(''))
106
+ assert_equal([1.2], f.to_type('1.2'))
107
+ assert_equal([1.2,1.3], f.to_type('1.2,1.3'))
108
+ assert_equal([1.2,1.3], f.to_type('1.2, 1.3'))
109
+ end
110
+
66
111
  end
@@ -7,6 +7,7 @@ class HasSettingTest < Test::Unit::TestCase
7
7
  @baz = Baz.create!
8
8
  end
9
9
 
10
+
10
11
  def test_setting_has_accessors
11
12
  assert @foo.respond_to?(:setting_1)
12
13
  assert @foo.respond_to?(:setting_1=)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simplificator-has_setting
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.8
4
+ version: 0.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simplificator GmbH
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-06 00:00:00 -08:00
12
+ date: 2009-02-07 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15