class Netfira::WebConnect::Models::Setting

Public Instance Methods

typed_value() click to toggle source
# File lib/netfira/web_connect/models/support/setting.rb, line 11
def typed_value
  cast_by_content_type value, content_type
end
typed_value=(value) click to toggle source
# File lib/netfira/web_connect/models/support/setting.rb, line 15
def typed_value=(value)
  if String === value
    if value.encoding.name == 'ASCII-8BIT'
      self.value = value.b
      self.content_type = 'application/octet-stream'
    else
      self.value = value.force_encoding('UTF-8')
      self.content_type = 'text/plain;charset=UTF-8'
    end
  else
    self.value = JSON.generate(value, quirks_mode: true)
    self.content_type = 'application/json'
  end
end

Private Instance Methods

cast_by_content_type(value, content_type) click to toggle source
# File lib/netfira/web_connect/models/support/setting.rb, line 32
def cast_by_content_type(value, content_type)
  case content_type
    when 'application/json' then JSON.parse(value, quirks_mode: true)
    when 'application/octet-stream' then value.b
    when /^text\/plain;charset=(.+)$/ then value.force_encoding($1)
    else value
  end
end
dispatch_change_event() { || ... } click to toggle source
# File lib/netfira/web_connect/models/support/setting.rb, line 41
def dispatch_change_event
  old = persisted? ? cast_by_content_type(changed_attributes['value'], changed_attributes['content_type']) : nil
  yield
  new = OpenStruct.new(shop: shop, key: key, value: typed_value)
  dispatch_event [:on, :after], 'change_setting', new, old unless old == new.value
end
dispatch_destroy_event() click to toggle source
# File lib/netfira/web_connect/models/support/setting.rb, line 48
def dispatch_destroy_event
  return unless persisted?
  new = OpenStruct.new(shop: shop, key: key, value: nil)
  old = typed_value
  dispatch_event [:before, :on], 'change_setting', new, old unless old.nil?
  true
end