2013-08-25 22:52:36 -04:00
|
|
|
# API to wrap up plugin store rows
|
|
|
|
class PluginStore
|
|
|
|
def self.get(plugin_name, key)
|
2014-05-06 09:41:59 -04:00
|
|
|
if row = PluginStoreRow.find_by(plugin_name: plugin_name, key: key)
|
2013-08-25 22:52:36 -04:00
|
|
|
cast_value(row.type_name, row.value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.set(plugin_name, key, value)
|
|
|
|
hash = {plugin_name: plugin_name, key: key}
|
2014-08-19 08:40:23 -04:00
|
|
|
row = PluginStoreRow.find_by(hash) || PluginStoreRow.new(hash)
|
2013-08-25 22:52:36 -04:00
|
|
|
|
|
|
|
row.type_name = determine_type(value)
|
|
|
|
# nil are stored as nil
|
|
|
|
row.value =
|
|
|
|
if row.type_name == "JSON"
|
|
|
|
value.to_json
|
|
|
|
elsif value
|
|
|
|
value.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
row.save
|
|
|
|
end
|
|
|
|
|
2014-02-24 19:11:31 -05:00
|
|
|
def self.remove(plugin_name, key)
|
2014-02-25 11:26:35 -05:00
|
|
|
PluginStoreRow.where(plugin_name: plugin_name, key: key).destroy_all
|
2014-02-24 19:11:31 -05:00
|
|
|
end
|
|
|
|
|
2013-08-25 22:52:36 -04:00
|
|
|
def self.determine_type(value)
|
2014-03-10 05:22:59 -04:00
|
|
|
value.is_a?(Hash) || value.is_a?(Array) ? "JSON" : value.class.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.map_json(item)
|
|
|
|
if item.is_a? Hash
|
|
|
|
ActiveSupport::HashWithIndifferentAccess.new item
|
|
|
|
elsif item.is_a? Array
|
|
|
|
item.map { |subitem| map_json subitem}
|
|
|
|
else
|
|
|
|
item
|
|
|
|
end
|
2013-08-25 22:52:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.cast_value(type, value)
|
|
|
|
case type
|
|
|
|
when "Fixnum" then value.to_i
|
|
|
|
when "TrueClass", "FalseClass" then value == "true"
|
2014-03-10 05:22:59 -04:00
|
|
|
when "JSON" then map_json(::JSON.parse(value))
|
2013-08-25 22:52:36 -04:00
|
|
|
else value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|