persistent_hash 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8015f93dc2bd0290f4a1fbe5b3b08528993e571a
4
+ data.tar.gz: 180e92e79cc5c5f98647f40858ce609a1f7ba8ed
5
+ SHA512:
6
+ metadata.gz: e8fcf4a150150491958841a3f3ab0deafaf3e9b4828d6aa3dc59cee56248ca9507e105ee6310e767d62b9fa8ae8c04e3e38a301766f66aa706d77b64ac34eff2
7
+ data.tar.gz: 8833ab36db95c372b7530fa1da83b36b55a7ecb94fa23b184ec49746269a48f09aee07e8854319b7375fd2337a6b3a70fefb5f57cd000010a2f99c30c9eb7471
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Alex Dean
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/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
8
+ load 'rails/tasks/engine.rake'
9
+
10
+ Bundler::GemHelper.install_tasks
11
+
12
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
13
+
14
+ require 'rspec/core'
15
+ require 'rspec/core/rake_task'
16
+
17
+ desc "Run all specs in spec directory (excluding plugin specs)"
18
+ RSpec::Core::RakeTask.new(:spec)
19
+
20
+ task :default => :spec
@@ -0,0 +1,48 @@
1
+ module PersistentHash
2
+
3
+ # Formatters are used to create the readable_value attribute for values
4
+ # stored in the Hash.
5
+ class Formatter
6
+
7
+ @formatters = {}
8
+
9
+ # specify how the human-readable version of the item should be generated
10
+ #
11
+ # @example
12
+ # PersistentHash::Formatter.add(
13
+ # Time,
14
+ # ->(val) { val.iso8601 }
15
+ # )
16
+ #
17
+ # @param [Class] clazz The formatter will receive instances of this class.
18
+ # @param [Proc] proc A proc which receives an instance of clazz and
19
+ # returns a string representation of that instance.
20
+ def self.add(clazz, proc)
21
+ @formatters[clazz] = proc
22
+ true
23
+ end
24
+
25
+ # create a human-readable version of value for storage in the db
26
+ #
27
+ # If a specific formattation proc has been supplied for the value's
28
+ # class via `add`, that proc will be invoked. Otherwise, the result of
29
+ # the value's `to_s` method will be returned.
30
+ #
31
+ # @return [String] A string representation of value.
32
+ def self.format(value)
33
+ formatter = @formatters[value.class]
34
+ if formatter
35
+ formatted = formatter.call(value)
36
+ else
37
+ formatted = value.to_s
38
+ end
39
+ end
40
+
41
+ # forget all current formatters
42
+ def self.reset!
43
+ @formatters = {}
44
+ true
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,35 @@
1
+ module PersistentHash
2
+ class Hash < ActiveRecord::Base
3
+
4
+ self.table_name = 'persistent_hash'
5
+
6
+ # save a value in the hash
7
+ def self.[]=(key_name, value)
8
+ where(key_name: key_name).delete_all
9
+
10
+ if ! value.nil?
11
+ formatted = PersistentHash::Formatter.format(value)
12
+
13
+ create!(
14
+ key_name: key_name,
15
+ readable_value: formatted,
16
+ marshalled: Base64.encode64(Marshal.dump(value))
17
+ )
18
+ end
19
+ end
20
+
21
+ # fetch a value from the hash.
22
+ #
23
+ # @return The requested value, or nil if the requested key does not exist.
24
+ def self.[](key_name)
25
+ value = nil
26
+ item = select(:marshalled).where(key_name: key_name).first
27
+ if item
28
+ value = Marshal.load(Base64.decode64(item.marshalled))
29
+ end
30
+
31
+ value
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,12 @@
1
+ class CreatePersistentHash < ActiveRecord::Migration
2
+ def change
3
+ create_table :persistent_hash do |t|
4
+ t.string :key_name, null: false
5
+ t.string :readable_value, null: false
6
+ t.text :marshalled, null: false
7
+ t.datetime :updated_at, null: false
8
+ end
9
+
10
+ add_index :persistent_hash, :key_name, unique: true
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module PersistentHash
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace PersistentHash
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module PersistentHash
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ require "persistent_hash/engine"
2
+
3
+ module PersistentHash
4
+ def self.[]=(key_name, value)
5
+ PersistentHash::Hash[key_name] = value
6
+ end
7
+ def self.[](key_name)
8
+ PersistentHash::Hash[key_name]
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :persistent_hash do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: persistent_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Dean
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: timecop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Handy place to stick small bits of persistent data. Like redis if you
98
+ don't want to install redis.
99
+ email:
100
+ - alex@crackpot.org
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - MIT-LICENSE
106
+ - Rakefile
107
+ - app/models/persistent_hash/formatter.rb
108
+ - app/models/persistent_hash/hash.rb
109
+ - db/migrate/20150518213536_create_persistent_hash.rb
110
+ - lib/persistent_hash.rb
111
+ - lib/persistent_hash/engine.rb
112
+ - lib/persistent_hash/version.rb
113
+ - lib/tasks/persistent_hash_tasks.rake
114
+ homepage: https://github.com/alexdean/persistent_hash
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.4.3
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: ActiveRecord-based key/value store for Rails applications.
138
+ test_files: []