Class: HasSetting::Formatters::StringsFormatter

Inherits:
NilSafeFormatter show all
Defined in:
lib/has_setting/formatters.rb

Instance Method Summary collapse

Methods inherited from NilSafeFormatter

#to_s, #to_type

Instance Method Details

#safe_to_s(value) ⇒ Object



107
108
109
110
# File 'lib/has_setting/formatters.rb', line 107

def safe_to_s(value)
  # Escape the separator character ',' with a backslash
  Array(value).map() {|item| item.gsub(',', '\,')}.join(',')
end

#safe_to_type(value) ⇒ Object



102
103
104
105
106
# File 'lib/has_setting/formatters.rb', line 102

def safe_to_type(value)
  # Ruby does not know "negative look before". Or i dont know how to do it in ruby. Thus
  # i ended up using some reverse calls... ugly. Anyone out there eager to help me out?
  value.reverse.split(/,(?!\\)/).map() {|item| item.reverse.gsub('\,', ',')}.reverse
end