keep 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License
2
+
3
+ Copyright (c) Zach Holman, http://zachholman.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Keep
2
+
3
+ It makes keeping config information pretty easy.
4
+
5
+ ## Install
6
+
7
+ gem install keep
8
+
9
+ ## Usage
10
+
11
+ require 'keep'
12
+ keep = Keep.new('config/settings.yml')
13
+ keep.set(:password,'Ellen Page')
14
+ keep.get(:password)
15
+
16
+ Just tell Keep where to keep things, then give it a hash of what to keep and
17
+ what the value is.
18
+
19
+ Keep currently serializes to YAML (although different backends are likely to
20
+ come next- pull requests welcome).
21
+
22
+ ## More
23
+
24
+ keep = Keep.new('config/settings.yml')
25
+ keep.present?(:password)
26
+
27
+ ## Other important information
28
+
29
+ Nothing else is important.
data/Rakefile ADDED
@@ -0,0 +1,150 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'date'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def version
16
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
17
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
18
+ end
19
+
20
+ def date
21
+ Date.today.to_s
22
+ end
23
+
24
+ def rubyforge_project
25
+ name
26
+ end
27
+
28
+ def gemspec_file
29
+ "#{name}.gemspec"
30
+ end
31
+
32
+ def gem_file
33
+ "#{name}-#{version}.gem"
34
+ end
35
+
36
+ def replace_header(head, header_name)
37
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
38
+ end
39
+
40
+ #############################################################################
41
+ #
42
+ # Standard tasks
43
+ #
44
+ #############################################################################
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/testtask'
49
+ Rake::TestTask.new(:test) do |test|
50
+ test.libs << 'lib' << 'test'
51
+ test.pattern = 'test/**/test_*.rb'
52
+ test.verbose = true
53
+ end
54
+
55
+ desc "Generate RCov test coverage and open in your browser"
56
+ task :coverage do
57
+ require 'rcov'
58
+ sh "rm -fr coverage"
59
+ sh "rcov test/test_*.rb"
60
+ sh "open coverage/index.html"
61
+ end
62
+
63
+ require 'rake/rdoctask'
64
+ Rake::RDocTask.new do |rdoc|
65
+ rdoc.rdoc_dir = 'rdoc'
66
+ rdoc.title = "#{name} #{version}"
67
+ rdoc.rdoc_files.include('README*')
68
+ rdoc.rdoc_files.include('lib/**/*.rb')
69
+ end
70
+
71
+ desc "Open an irb session preloaded with this library"
72
+ task :console do
73
+ sh "irb -rubygems -r ./lib/#{name}.rb"
74
+ end
75
+
76
+ #############################################################################
77
+ #
78
+ # Custom tasks (add your own tasks here)
79
+ #
80
+ #############################################################################
81
+
82
+
83
+
84
+ #############################################################################
85
+ #
86
+ # Packaging tasks
87
+ #
88
+ #############################################################################
89
+
90
+ desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
91
+ task :release => :build do
92
+ unless `git branch` =~ /^\* master$/
93
+ puts "You must be on the master branch to release!"
94
+ exit!
95
+ end
96
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
97
+ sh "git tag v#{version}"
98
+ sh "git push origin master"
99
+ sh "git push origin v#{version}"
100
+ sh "gem push pkg/#{name}-#{version}.gem"
101
+ end
102
+
103
+ desc "Build #{gem_file} into the pkg directory"
104
+ task :build => :gemspec do
105
+ sh "mkdir -p pkg"
106
+ sh "gem build #{gemspec_file}"
107
+ sh "mv #{gem_file} pkg"
108
+ end
109
+
110
+ desc "Generate #{gemspec_file}"
111
+ task :gemspec => :validate do
112
+ # read spec file and split out manifest section
113
+ spec = File.read(gemspec_file)
114
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
115
+
116
+ # replace name version and date
117
+ replace_header(head, :name)
118
+ replace_header(head, :version)
119
+ replace_header(head, :date)
120
+ #comment this out if your rubyforge_project has a different name
121
+ replace_header(head, :rubyforge_project)
122
+
123
+ # determine file list from git ls-files
124
+ files = `git ls-files`.
125
+ split("\n").
126
+ sort.
127
+ reject { |file| file =~ /^\./ }.
128
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
129
+ map { |file| " #{file}" }.
130
+ join("\n")
131
+
132
+ # piece file back together and write
133
+ manifest = " s.files = %w[\n#{files}\n ]\n"
134
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
135
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
136
+ puts "Updated #{gemspec_file}"
137
+ end
138
+
139
+ desc "Validate #{gemspec_file}"
140
+ task :validate do
141
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
142
+ unless libfiles.empty?
143
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
144
+ exit!
145
+ end
146
+ unless Dir['VERSION*'].empty?
147
+ puts "A `VERSION` file at root level violates Gem best practices."
148
+ exit!
149
+ end
150
+ end
data/keep.gemspec ADDED
@@ -0,0 +1,71 @@
1
+ ## This is the rakegem gemspec template. Make sure you read and understand
2
+ ## all of the comments. Some sections require modification, and others can
3
+ ## be deleted if you don't need them. Once you understand the contents of
4
+ ## this file, feel free to delete any comments that begin with two hash marks.
5
+ ## You can find comprehensive Gem::Specification documentation, at
6
+ ## http://docs.rubygems.org/read/chapter/20
7
+ Gem::Specification.new do |s|
8
+ s.specification_version = 2 if s.respond_to? :specification_version=
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.rubygems_version = '1.3.5'
11
+
12
+ ## Leave these as is they will be modified for you by the rake gemspec task.
13
+ ## If your rubyforge_project name is different, then edit it and comment out
14
+ ## the sub! line in the Rakefile
15
+ s.name = 'keep'
16
+ s.version = '0.0.1'
17
+ s.date = '2011-02-17'
18
+ s.rubyforge_project = 'keep'
19
+
20
+ ## Make sure your summary is short. The description may be as long
21
+ ## as you like.
22
+ s.summary = "It makes keeping config information pretty easy."
23
+ s.description = "A standardized library to persist config information to disk."
24
+
25
+ ## List the primary authors. If there are a bunch of authors, it's probably
26
+ ## better to set the email to an email list or something. If you don't have
27
+ ## a custom homepage, consider using your GitHub URL or the like.
28
+ s.authors = ["Zach Holman"]
29
+ s.email = 'hello@zachholman.com'
30
+ s.homepage = 'https://github.com/holman/keep'
31
+
32
+ ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
33
+ ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
34
+ s.require_paths = %w[lib]
35
+
36
+ ## Specify any RDoc options here. You'll want to add your README and
37
+ ## LICENSE files to the extra_rdoc_files list.
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.extra_rdoc_files = %w[README.md LICENSE.md]
40
+
41
+ ## List your runtime dependencies here. Runtime dependencies are those
42
+ ## that are needed for an end user to actually USE your code.
43
+ #s.add_dependency('DEPNAME', [">= 1.1.0", "< 2.0.0"])
44
+
45
+ ## List your development dependencies here. Development dependencies are
46
+ ## those that are only needed during development
47
+ #s.add_development_dependency('DEVDEPNAME', [">= 1.1.0", "< 2.0.0"])
48
+
49
+ ## Leave this section as-is. It will be automatically generated from the
50
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
51
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
52
+ # = MANIFEST =
53
+ s.files = %w[
54
+ LICENSE.md
55
+ README.md
56
+ Rakefile
57
+ keep.gemspec
58
+ lib/keep.rb
59
+ lib/keep/disk.rb
60
+ lib/keep/yaml.rb
61
+ test/helper.rb
62
+ test/test_disk.rb
63
+ test/test_keep.rb
64
+ test/test_yaml.rb
65
+ ]
66
+ # = MANIFEST =
67
+
68
+ ## Test files will be grabbed from the file list. Make sure the path glob
69
+ ## matches what you actually use.
70
+ s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
71
+ end
data/lib/keep/disk.rb ADDED
@@ -0,0 +1,73 @@
1
+ # Keep::Disk is our hook into your system. We'll save and open files here.
2
+ #
3
+ # Pretty cool, amirite?
4
+ class Keep
5
+ class Disk
6
+
7
+ # The path to save the file.
8
+ #
9
+ # Returns a String path to the file.
10
+ attr_reader :path
11
+
12
+ # The path to save the file.
13
+ #
14
+ # path - String file path (relative or absolute) destination
15
+ #
16
+ # Returns a String path to the file.
17
+ attr_writer :path
18
+
19
+ # Creates a new Disk instance.
20
+ #
21
+ # Returns nothing.
22
+ def initialize(path)
23
+ @path = path
24
+ touch
25
+ end
26
+
27
+ # Ensures the destination file is created.
28
+ #
29
+ # Returns nothing.
30
+ def touch
31
+ FileUtils.touch(path)
32
+ end
33
+
34
+ # The in-memory data structure representing the configuration you're
35
+ # keeping. Implemented by child classes.
36
+ #
37
+ # Returns a Hash of configuration keys and values.
38
+ def data ; end
39
+
40
+ # Persists the in-memory hash to disk. Implemented by child classes.
41
+ #
42
+ # Returns whether the save was successful.
43
+ def save ; end
44
+
45
+ # Sets a value to a given key. Updates the in-memory hash, and then saves.
46
+ #
47
+ # key - a String value for the key of this Hash
48
+ # value - an Object that maps to the key, will be serialized
49
+ #
50
+ # Returns whether the save was successful.
51
+ def set(key,value)
52
+ data[key.to_s] = value
53
+ save
54
+ end
55
+
56
+ # Returns the value of the specified key.
57
+ #
58
+ # key - a String value for the key of this Hash
59
+ #
60
+ # Returns the value if found, otherwise nil.
61
+ def get(key)
62
+ data[key.to_s]
63
+ end
64
+
65
+ # Searches for the key in the in-memory Hash.
66
+ #
67
+ # Returns whether or not the key is present.
68
+ def present?(key)
69
+ !get(key).nil?
70
+ end
71
+
72
+ end
73
+ end
data/lib/keep/yaml.rb ADDED
@@ -0,0 +1,22 @@
1
+ class Keep
2
+ class Yaml < Disk
3
+
4
+ # The in-memory data structure representing the configuration you're
5
+ # keeping. Implemented by child classes.
6
+ #
7
+ # Returns a Hash of configuration keys and values.
8
+ def data
9
+ @data ||= YAML::load_file(path) || {}
10
+ end
11
+
12
+ # Persists the in-memory hash to disk. Implemented by child classes.
13
+ #
14
+ # Returns whether the save was successful.
15
+ def save
16
+ File.open(path, 'w') do |out|
17
+ YAML.dump(data, out)
18
+ end
19
+ end
20
+
21
+ end
22
+ end
data/lib/keep.rb ADDED
@@ -0,0 +1,80 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
2
+
3
+ require 'fileutils'
4
+ require 'yaml'
5
+
6
+ require 'keep/disk'
7
+ require 'keep/yaml'
8
+
9
+ class Keep
10
+ VERSION = '0.0.1'
11
+
12
+ # The path to the configuration file.
13
+ attr_reader :path
14
+
15
+ # The in-memory Disk object.
16
+ attr_reader :disk
17
+
18
+ # The configuration format outputted.
19
+ attr_reader :format
20
+
21
+ # The path to the configuration file.
22
+ #
23
+ # path - a String file path (absolute or relative)
24
+ #
25
+ # Returns a String.
26
+ attr_writer :path
27
+
28
+ # A reference to the in-memory Disk object.
29
+ attr_writer :disk
30
+
31
+ # The format of the configuration output.
32
+ attr_writer :format
33
+
34
+
35
+ # Initialize a new Keep instance.
36
+ #
37
+ # path - the String file path to save configuration to
38
+ #
39
+ # Returns nothing.
40
+ def initialize(path)
41
+ @path = path
42
+ @disk = Yaml.new(path)
43
+ end
44
+
45
+ # The format of configuration output.
46
+ #
47
+ # format - the lowercase String representation of a class inheriting Disk
48
+ #
49
+ # Returns nothing, defaults to 'yaml' right now.
50
+ def as(format)
51
+ @format = 'yaml'
52
+ end
53
+
54
+ # Sets a value to a given key.
55
+ #
56
+ # key - a String value for the key of this Hash
57
+ # value - an Object that maps to the key, will be serialized
58
+ #
59
+ # Returns whether the save was successful.
60
+ def set(key,value)
61
+ @disk.set(key,value)
62
+ end
63
+
64
+ # Returns the value of the specified key.
65
+ #
66
+ # key - a String value for the key of this Hash
67
+ #
68
+ # Returns the value if found, otherwise nil.
69
+ def get(key)
70
+ @disk.get(key)
71
+ end
72
+
73
+ # Searches for the key in the in-memory Hash.
74
+ #
75
+ # Returns whether or not the key is present.
76
+ def present?(key)
77
+ @disk.present?(key)
78
+ end
79
+
80
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,14 @@
1
+ # coding: utf-8
2
+
3
+ require 'test/unit'
4
+
5
+ begin
6
+ require 'rubygems'
7
+ require 'redgreen'
8
+ rescue LoadError
9
+ end
10
+
11
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
12
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
+
14
+ require 'keep'
data/test/test_disk.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'helper'
2
+
3
+ class TestDisk < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @path = '/tmp/keep-disk-test'
7
+ @disk = Keep::Disk.new(@path)
8
+ end
9
+
10
+ def teardown
11
+ `rm -rf #{@path}`
12
+ end
13
+
14
+ def test_path
15
+ assert_equal @path, @disk.path
16
+ end
17
+
18
+ def test_touch
19
+ assert File.exist?(@path)
20
+ end
21
+
22
+ end
data/test/test_keep.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'helper'
2
+
3
+ class TestKeeps < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @path = '/tmp/keep-test'
7
+ @keep = Keep.new(@path)
8
+ end
9
+
10
+ def teardown
11
+ `rm -rf #{@path}`
12
+ end
13
+
14
+ def test_path
15
+ assert_equal @path, @keep.path
16
+ end
17
+
18
+ def test_as
19
+ assert_equal 'yaml', @keep.as('yaml')
20
+ end
21
+
22
+ end
data/test/test_yaml.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'helper'
2
+
3
+ class TestKeep < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @path = '/tmp/keep-yaml-test'
7
+ @yaml = Keep::Yaml.new(@path)
8
+ end
9
+
10
+ def teardown
11
+ `rm -rf #{@path}`
12
+ end
13
+
14
+ def test_set
15
+ @yaml.set('cute', 'ellen page')
16
+ assert_equal "--- \ncute: ellen page", File.read(@path).chomp
17
+ end
18
+
19
+ def test_get_symbol
20
+ @yaml.set('cute', 'ellen page')
21
+ assert_equal 'ellen page', @yaml.get(:cute)
22
+ end
23
+
24
+ def test_get_string
25
+ @yaml.set('cute', 'ellen page')
26
+ assert_equal 'ellen page', @yaml.get('cute')
27
+ end
28
+
29
+ def test_present
30
+ assert !@yaml.present?('cute')
31
+ @yaml.set('cute', 'ellen page')
32
+ assert @yaml.present?('cute')
33
+ end
34
+
35
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: keep
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Zach Holman
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-17 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: A standardized library to persist config information to disk.
23
+ email: hello@zachholman.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.md
30
+ - LICENSE.md
31
+ files:
32
+ - LICENSE.md
33
+ - README.md
34
+ - Rakefile
35
+ - keep.gemspec
36
+ - lib/keep.rb
37
+ - lib/keep/disk.rb
38
+ - lib/keep/yaml.rb
39
+ - test/helper.rb
40
+ - test/test_disk.rb
41
+ - test/test_keep.rb
42
+ - test/test_yaml.rb
43
+ has_rdoc: true
44
+ homepage: https://github.com/holman/keep
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --charset=UTF-8
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ requirements: []
71
+
72
+ rubyforge_project: keep
73
+ rubygems_version: 1.3.7
74
+ signing_key:
75
+ specification_version: 2
76
+ summary: It makes keeping config information pretty easy.
77
+ test_files:
78
+ - test/test_disk.rb
79
+ - test/test_keep.rb
80
+ - test/test_yaml.rb