Class: PluginStore
- Inherits:
-
Object
- Object
- PluginStore
- Defined in:
- app/models/plugin_store.rb
Overview
API to wrap up plugin store rows
Instance Attribute Summary collapse
-
#plugin_name ⇒ Object
readonly
Returns the value of attribute plugin_name.
Class Method Summary collapse
- .cast_value(type, value) ⇒ Object
- .determine_type(value) ⇒ Object
- .get(plugin_name, key) ⇒ Object
- .get_all(plugin_name, keys) ⇒ Object
- .map_json(item) ⇒ Object
- .remove(plugin_name, key) ⇒ Object
- .set(plugin_name, key, value) ⇒ Object
Instance Method Summary collapse
- #get(key) ⇒ Object
- #get_all(keys) ⇒ Object
-
#initialize(plugin_name) ⇒ PluginStore
constructor
A new instance of PluginStore.
- #remove(key) ⇒ Object
- #set(key, value) ⇒ Object
Constructor Details
#initialize(plugin_name) ⇒ PluginStore
Returns a new instance of PluginStore.
7 8 9 |
# File 'app/models/plugin_store.rb', line 7 def initialize(plugin_name) @plugin_name = plugin_name end |
Instance Attribute Details
#plugin_name ⇒ Object (readonly)
Returns the value of attribute plugin_name.
5 6 7 |
# File 'app/models/plugin_store.rb', line 5 def plugin_name @plugin_name end |
Class Method Details
.cast_value(type, value) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/models/plugin_store.rb', line 73 def self.cast_value(type, value) case type when "Integer", "Fixnum" value.to_i when "TrueClass", "FalseClass" value == "true" when "JSON" map_json(::JSON.parse(value)) else value end end |
.determine_type(value) ⇒ Object
59 60 61 |
# File 'app/models/plugin_store.rb', line 59 def self.determine_type(value) value.is_a?(Hash) || value.is_a?(Array) ? "JSON" : value.class.to_s end |
.get(plugin_name, key) ⇒ Object
27 28 29 30 31 |
# File 'app/models/plugin_store.rb', line 27 def self.get(plugin_name, key) if row = PluginStoreRow.find_by(plugin_name: plugin_name, key: key) cast_value(row.type_name, row.value) end end |
.get_all(plugin_name, keys) ⇒ Object
33 34 35 36 37 |
# File 'app/models/plugin_store.rb', line 33 def self.get_all(plugin_name, keys) rows = PluginStoreRow.where("plugin_name = ? AND key IN (?)", plugin_name, keys).to_a Hash[rows.map { |row| [row.key, cast_value(row.type_name, row.value)] }] end |
.map_json(item) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'app/models/plugin_store.rb', line 63 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 end |
.remove(plugin_name, key) ⇒ Object
55 56 57 |
# File 'app/models/plugin_store.rb', line 55 def self.remove(plugin_name, key) PluginStoreRow.where(plugin_name: plugin_name, key: key).destroy_all end |
.set(plugin_name, key, value) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/models/plugin_store.rb', line 39 def self.set(plugin_name, key, value) hash = { plugin_name: plugin_name, key: key } row = PluginStoreRow.find_by(hash) || PluginStoreRow.new(hash) 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 |
Instance Method Details
#get(key) ⇒ Object
11 12 13 |
# File 'app/models/plugin_store.rb', line 11 def get(key) self.class.get(plugin_name, key) end |
#get_all(keys) ⇒ Object
15 16 17 |
# File 'app/models/plugin_store.rb', line 15 def get_all(keys) self.class.get_all(plugin_name, keys) end |
#remove(key) ⇒ Object
23 24 25 |
# File 'app/models/plugin_store.rb', line 23 def remove(key) self.class.remove(plugin_name, key) end |
#set(key, value) ⇒ Object
19 20 21 |
# File 'app/models/plugin_store.rb', line 19 def set(key, value) self.class.set(plugin_name, key, value) end |