kvpair 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bitswitch.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Kelly Becker
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # KVPair
2
+
3
+ KVPair is a Key Value Pair engine for Rails 3
4
+
5
+ ## Installing KVPair
6
+
7
+ You can install KVPair with [RubyGems](https://rubygems.org/gems/kvpair).
8
+
9
+ `gem install kvpair` or `gem 'kvpair'`
10
+
11
+ ## Documentation for KVPair
12
+
13
+ Not available yet
14
+
15
+ ## Contributing to KVPair
16
+
17
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
18
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
19
+ * Fork the project.
20
+ * Start a feature/bugfix branch.
21
+ * Commit and push until you are happy with your contribution.
22
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
23
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
24
+
25
+ ## Copyright
26
+
27
+ Copyright (c) 2012 Kelly Becker. See LICENSE.txt for
28
+ further details.
29
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/kvpair.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kvpair/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "kvpair"
8
+ gem.version = KellyLSB::KVPair::VERSION
9
+ gem.authors = ["Kelly Becker"]
10
+ gem.email = ["kellylsbkr@gmail.com"]
11
+ gem.description = "Rails easy Key Value Pairs."
12
+ gem.summary = "Stores namespaced Key Value Pairs."
13
+ gem.homepage = "http://kellybecker.me"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
data/lib/kvpair.rb ADDED
@@ -0,0 +1,84 @@
1
+ unless defined? ActiveRecord::Base
2
+ raise StandardError, "ActiveRecord is not available."
3
+ end
4
+
5
+ module KellyLSB
6
+ module KVPair
7
+ extend ActiveSupport::Concern
8
+
9
+ module ClassMethods
10
+ def kvpair(ns)
11
+ nse = ns.to_s + '='
12
+ send(:include, Module.new {
13
+ send(:define_method, ns.to_sym) do |*args|
14
+
15
+ # Get the pairs
16
+ pairs = Kvpair.where(
17
+ :owner => "#{self.class.name}:#{self.id}",
18
+ :namespace => "#{ns}"
19
+ )
20
+
21
+ # Store data
22
+ data = Hash.new
23
+
24
+ # Get the pairs
25
+ pairs.each do |pair|
26
+ data[pair.key] = pair.value
27
+ data[pair.key.to_sym] = pair.value
28
+ end
29
+
30
+ # Return data
31
+ return data
32
+ end
33
+
34
+ send(:define_method, nse.to_sym) do |input|
35
+
36
+ # Remove unknown keys
37
+ input.delete_if{|k,v| v.nil?}
38
+
39
+ # Store keys
40
+ keys = Array.new
41
+
42
+ # Upsert the record
43
+ input.each do |key, val|
44
+ keys << key
45
+
46
+ begin
47
+ Kvpair.where(
48
+ :owner => "#{self.class.name}:#{self.id}",
49
+ :namespace => "#{ns}",
50
+ :key => "#{key}"
51
+ ).first_or_create(:value => "#{val}")
52
+ rescue
53
+ pair = Kvpair.where(
54
+ :owner => "#{self.class.name}:#{self.id}",
55
+ :namespace => "#{ns}",
56
+ :key => "#{key}"
57
+ ).first
58
+
59
+ pair.update_attribute(:value, val) unless pair.nil?
60
+
61
+ if pair.nil?
62
+ Kvpair.create(
63
+ :owner => "#{self.class.name}:#{self.id}",
64
+ :namespace => "#{ns}",
65
+ :key => "#{key}",
66
+ :value => "#{val}"
67
+ )
68
+ end
69
+ end
70
+ end
71
+
72
+ # Remove unwanted keys
73
+ Kvpair.delete_all(["`owner` = ? && `namespace` = ? && (`key` NOT IN (?) || `value` = '' || `value` IS NULL)", "#{self.class.name}:#{self.id}", ns, keys])
74
+
75
+ # Return the switch
76
+ return self.send(ns)
77
+ end
78
+ })
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ ActiveRecord::Base.send(:include, KellyLSB::KVPair)
@@ -0,0 +1,5 @@
1
+ module KellyLSB
2
+ module KVPair
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kvpair
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kelly Becker
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-29 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Rails easy Key Value Pairs.
15
+ email:
16
+ - kellylsbkr@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - kvpair.gemspec
27
+ - lib/kvpair.rb
28
+ - lib/kvpair/version.rb
29
+ homepage: http://kellybecker.me
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 1.8.23
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Stores namespaced Key Value Pairs.
53
+ test_files: []