nifty-key-value-store 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ require 'nifty/key_value_store/version'
2
+ require 'nifty/key_value_store/railtie' if defined?(Rails)
@@ -0,0 +1,21 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: nifty_key_value_pairs
4
+ #
5
+ # id :integer not null, primary key
6
+ # parent_id :integer
7
+ # parent_type :string(255)
8
+ # group :string(255)
9
+ # name :string(255)
10
+ # value :string(255)
11
+ #
12
+ module Nifty
13
+ module KeyValueStore
14
+
15
+ class KeyValuePair < ::ActiveRecord::Base
16
+ self.table_name = 'nifty_key_value_store'
17
+ belongs_to :parent, :polymorphic => true
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,14 @@
1
+ class CreateNiftyKeyValueStoreTable < ActiveRecord::Migration
2
+
3
+ def up
4
+ create_table :nifty_key_value_store do |t|
5
+ t.integer :parent_id
6
+ t.string :parent_type, :group, :name, :value
7
+ end
8
+ end
9
+
10
+ def down
11
+ drop_table :nifty_key_value_store
12
+ end
13
+
14
+ end
@@ -0,0 +1,21 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module Nifty
5
+ module KeyValueStore
6
+ class MigrationGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+
9
+ source_root File.expand_path("../", __FILE__)
10
+
11
+ def self.next_migration_number(path)
12
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
13
+ end
14
+
15
+ def create_model_file
16
+ migration_template 'migration.rb', "db/migrate/create_nifty_key_value_store_table"
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,87 @@
1
+ module Nifty
2
+ module KeyValueStore
3
+ module ModelExtension
4
+
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def key_value_stores
11
+ @key_value_stores ||= []
12
+ end
13
+
14
+ def key_value_store(*names)
15
+
16
+ unless self.reflect_on_all_associations(:has_many).map(&:name).include?(:nifty_key_value_pairs)
17
+ has_many :nifty_key_value_pairs, :as => :parent, :dependent => :destroy, :class_name => 'Nifty::KeyValueStore::KeyValuePair'
18
+ after_save :save_custom_values_to_db
19
+ end
20
+
21
+ names.each do |name|
22
+ key_value_stores << name
23
+
24
+ # Getter
25
+ define_method name do
26
+ if value = instance_variable_get("@#{name}")
27
+ value
28
+ else
29
+ instance_variable_set "@#{name}", get_custom_values_for(name)
30
+ end
31
+ end
32
+
33
+ # JSON Getter
34
+ define_method "#{name}_json" do
35
+ send(name).to_json
36
+ end
37
+
38
+ # Setter
39
+ define_method "#{name}=" do |value|
40
+ instance_variable_set("@#{name}", value)
41
+ end
42
+
43
+ # JSON Setter
44
+ define_method "#{name}_json=" do |value|
45
+ send("#{name}=", JSON.parse(value))
46
+ end
47
+
48
+ end
49
+ end
50
+ end
51
+
52
+ # Get the values for a given group as a hash from the database without caching
53
+ def get_custom_values_for(group)
54
+ nifty_key_value_pairs.where(:group => group).inject({}) do |hash, value|
55
+ hash[value.name] = value.value
56
+ hash
57
+ end
58
+ end
59
+
60
+ # Save all custom values in the appropriate methods to the
61
+ def save_custom_values_to_db
62
+ # get the existing joins ready
63
+ existing_joins = self.nifty_key_value_pairs
64
+ # go through all groups
65
+ self.class.key_value_stores.each do |group|
66
+ db_values = get_custom_values_for(group)
67
+ local_values = self.send(group).delete_if { |k,v| v.blank? }
68
+
69
+ # remove any keys which no longer exist
70
+ keys_to_remove = db_values.keys - local_values.keys
71
+ self.nifty_key_value_pairs.where(:group => group, :name => keys_to_remove).destroy_all unless keys_to_remove.empty?
72
+
73
+ # add/update all remaining keys
74
+ local_values.each do |key, value|
75
+ if existing_join = existing_joins.select { |j| j.group == group.to_s && j.name == key}.first
76
+ existing_join.value = value
77
+ existing_join.save
78
+ else
79
+ self.nifty_key_value_pairs.create(:group => group, :name => key, :value => value)
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,21 @@
1
+ module Nifty
2
+ module KeyValueStore
3
+ class Railtie < Rails::Railtie #:nodoc:
4
+
5
+ initializer 'nifty.key_value_store.initialize' do
6
+
7
+ ActiveSupport.on_load(:active_record) do
8
+ require 'nifty/key_value_store/key_value_pair'
9
+ require 'nifty/key_value_store/model_extension'
10
+ ::ActiveRecord::Base.send :include, Nifty::KeyValueStore::ModelExtension
11
+ end
12
+
13
+ end
14
+
15
+ generators do
16
+ require 'nifty/key_value_store/migration_generator'
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ module Nifty
2
+ module KeyValueStore
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nifty-key-value-store
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adam Cooke
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A key value store controller for Rails models
15
+ email:
16
+ - adam@niftyware.io
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/nifty/key_value_store/key_value_pair.rb
22
+ - lib/nifty/key_value_store/migration.rb
23
+ - lib/nifty/key_value_store/migration_generator.rb
24
+ - lib/nifty/key_value_store/model_extension.rb
25
+ - lib/nifty/key_value_store/railtie.rb
26
+ - lib/nifty/key_value_store/version.rb
27
+ - lib/nifty/key_value_store.rb
28
+ homepage: https://github.com/niftyware/key-value-store
29
+ licenses: []
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 1.8.23
49
+ signing_key:
50
+ specification_version: 3
51
+ summary: A key value store controller for Rails models
52
+ test_files: []