bini 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.travis.yml +1 -0
- data/CHANGELOG.md +10 -0
- data/Guardfile +7 -5
- data/README.md +57 -0
- data/lib/bini.rb +12 -64
- data/lib/bini/config.rb +1 -1
- data/lib/bini/core.rb +75 -0
- data/lib/bini/extensions/savable.rb +96 -0
- data/lib/bini/sash.rb +12 -80
- data/lib/bini/version.rb +1 -1
- data/spec/lib/bini/extensions/savable_spec.rb +57 -0
- data/spec/lib/bini/optparser_spec.rb +1 -1
- data/spec/lib/bini/sash_spec.rb +19 -64
- data/spec/lib/bini_spec.rb +8 -16
- data/spec/spec_helper.rb +2 -4
- metadata +13 -13
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c94b975fedf242c57e6baa97d09deba3425c6049
|
4
|
+
data.tar.gz: 2dd81e4bd023c3ff7cb871730e5b64bb1f1a689e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 50b3ca5d6a48c9515e9464363a232339cead4df11eb6e95b54da8cb8324e1e43ee3e39aa389e68699f18eccbe4217ae329b9ba3645d3092444c0c7a091f1db25
|
7
|
+
data.tar.gz: b08c579b54f233ce009d2953134746ea3f1fc260bfa45f02a82b96e34caa1d8f33c81c9592cefa9c1e481e4d7e3b1f3701399feb057a6ea2ec0a14aa042e8ee2
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
# 0.7.0
|
2
|
+
* Added Bini::Extensions::Savable. This can extend any hash into a savable one.
|
3
|
+
* Updated Sash to be built off Extensions::Savable.
|
4
|
+
* Added Bini.clear to reset defaults. Refactored how defaults and attributes are generated.
|
5
|
+
* Added Bini.data_dir (default: ~/.local/share/#{long_name})
|
6
|
+
* Added some documentation for Sash.
|
7
|
+
* Added overrides:{} and options:{} to Sash. This splits out the options from any k/v's you want to set initially.
|
8
|
+
* Added autoloads, from now on you just have to require 'bini' and use the modules as you see fit.
|
9
|
+
* Fixed bini.long_name by making them all procs. (github #6)
|
10
|
+
|
1
11
|
# 0.6.0
|
2
12
|
* renamed Bini.config to Bini::Config
|
3
13
|
* 100% test coverage.
|
data/Guardfile
CHANGED
@@ -10,10 +10,12 @@ end
|
|
10
10
|
guard :rspec do
|
11
11
|
watch(%r{^spec/.+_spec\.rb$})
|
12
12
|
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
13
|
+
watch('lib/bini/core.rb') { "spec"}
|
14
|
+
watch('lib/bini.rb') { "spec" }
|
13
15
|
watch('spec/spec_helper.rb') { "spec" }
|
14
16
|
end
|
15
|
-
group :docs do
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
17
|
+
# group :docs do
|
18
|
+
# guard :yard do
|
19
|
+
# watch(%r{^lib/(.+)\.rb$})
|
20
|
+
# end
|
21
|
+
# end
|
data/README.md
CHANGED
@@ -43,6 +43,63 @@ require 'bini/config'
|
|
43
43
|
require 'bini/optparser'
|
44
44
|
require 'bini/log'
|
45
45
|
```
|
46
|
+
## Extensions
|
47
|
+
### Savable
|
48
|
+
|
49
|
+
Make any hash savable. It saves the key/value pairs from the hash into a
|
50
|
+
YAML file that can be loaded later. It manages permissions (optionally), can
|
51
|
+
create a backup/save file, automatically save on changes.
|
52
|
+
|
53
|
+
To use it just ```include Bini::Extensions::Savable``` in the top of your class
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class NewClass < Hash
|
57
|
+
include Bini::Exetensions::Savable
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
Now when you create the new object, it will have access to a few addtional
|
62
|
+
methods.
|
63
|
+
|
64
|
+
% obj.save
|
65
|
+
% obj.load
|
66
|
+
|
67
|
+
Are the main two, the other methods can be used but are typically called through
|
68
|
+
those two. These secondary methods are:
|
69
|
+
|
70
|
+
% obj.backup
|
71
|
+
% obj.set_mode
|
72
|
+
|
73
|
+
Backup will create a backup file on the spot. This won't save the current hash,
|
74
|
+
just copy the save file.
|
75
|
+
|
76
|
+
Set mode will set the mode stored in ```obj.options[:mode]``` on the file.
|
77
|
+
|
78
|
+
You can check if the obj needs to be saved with ```is_dirty?```. This will
|
79
|
+
return ```true``` if you need to save and false if you do not.
|
80
|
+
|
81
|
+
To configure savable you create k/v pairs in it's options.
|
82
|
+
|
83
|
+
% obj.options[:backup] = true
|
84
|
+
|
85
|
+
Options is just anothr hash to contain the various settings cleanly.
|
86
|
+
|
87
|
+
* file: Filename to save the hash to.
|
88
|
+
* backup: Boolean, whether to produce a backup or not.
|
89
|
+
* mode: FixNum: mode to store it as: 0600, 0755, 0644 and so on.
|
90
|
+
* autosave: Boolean: Automatically save on changes.
|
91
|
+
|
92
|
+
## Helpers
|
93
|
+
### Sash
|
94
|
+
|
95
|
+
Sash is a class extended from hash with savable and a few extra features. You can add
|
96
|
+
overrides (load values into the hash on creation), options at creation and autoload.
|
97
|
+
|
98
|
+
Overrides are just a way to set values on creation.
|
99
|
+
|
100
|
+
### Additional Options
|
101
|
+
|
102
|
+
* autoload: Boolean: set to true to load the value of the config right on creation
|
46
103
|
|
47
104
|
### API
|
48
105
|
|
data/lib/bini.rb
CHANGED
@@ -1,69 +1,17 @@
|
|
1
|
-
require
|
1
|
+
require "bini/core"
|
2
2
|
|
3
|
-
require "bini/sash"
|
4
|
-
require "bini/version"
|
5
|
-
require "bini/filemagic"
|
6
|
-
|
7
|
-
# A collection of very small helpers that assist me in writing a CLI without
|
8
|
-
# getting in the way.
|
9
|
-
#
|
10
|
-
# Provides some dynamic attributes, they all behave the same and just hook into
|
11
|
-
# the defaults to provide non-nil results when needed.
|
12
|
-
#
|
13
|
-
# @!attribute long_name [rw]
|
14
|
-
# @return [String] An application name, useful if your app is named differently then your binary.
|
15
|
-
# @!attribute cache_dir [rw]
|
16
|
-
# @return [String] The directory to store any cache related files.
|
17
|
-
# @!attribute config_dir [rw]
|
18
|
-
# @return [String] The directory to store any config related files.
|
19
|
-
# @!attribute version [rw]
|
20
|
-
# @return [String] The version of the application, not of Bini.
|
21
3
|
module Bini
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
@defaults[:config_dir] = "#{Dir.home}/.config/bini/#{@long_name}/"
|
34
|
-
|
35
|
-
# Dynamic attribute's based off the keys.
|
36
|
-
def keys
|
37
|
-
keys ||= [:long_name, :cache_dir, :config_dir, :version]
|
38
|
-
end
|
39
|
-
|
40
|
-
keys.each do |key|
|
41
|
-
define_method(key) do
|
42
|
-
v = instance_variable_get "@#{key}"
|
43
|
-
return !v ? @defaults[key] : v
|
44
|
-
end
|
45
|
-
define_method("#{key}=".to_sym) do |dir|
|
46
|
-
instance_variable_set "@#{key}", dir
|
47
|
-
end
|
4
|
+
autoload :Sash, 'bini/sash'
|
5
|
+
autoload :Options, 'bini/optparser'
|
6
|
+
autoload :Config, 'bini/config'
|
7
|
+
autoload :Backups, 'bini/backups'
|
8
|
+
autoload :OptionParser, 'bini/optparser'
|
9
|
+
autoload :FileMagic, 'bini/filemagic'
|
10
|
+
autoload :Prompts, 'bini/prompts'
|
11
|
+
autoload :VERSION, 'bini/version'
|
12
|
+
|
13
|
+
module Extensions
|
14
|
+
autoload :Savable, 'bini/extensions/savable'
|
48
15
|
end
|
49
16
|
|
50
|
-
# Adds a rails style configure method.
|
51
|
-
def configure
|
52
|
-
yield self
|
53
|
-
parameters
|
54
|
-
end
|
55
|
-
|
56
|
-
# List available parameters and values in those params
|
57
|
-
def parameters
|
58
|
-
@values = {}
|
59
|
-
keys.each { |k| @values.merge! k => Bini.send(k) }
|
60
|
-
@values
|
61
|
-
end
|
62
|
-
alias_method :params, :parameters
|
63
|
-
|
64
|
-
# Returns true or false if all parameters are set to something other than defaults.
|
65
|
-
def parameters?
|
66
|
-
@defaults.map {|k,v| @defaults[k] != parameters[k]}.all?
|
67
|
-
end
|
68
17
|
end
|
69
|
-
|
data/lib/bini/config.rb
CHANGED
data/lib/bini/core.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# A collection of very small helpers that assist me in writing a CLI without
|
2
|
+
# getting in the way.
|
3
|
+
#
|
4
|
+
# Provides some dynamic attributes, they all behave the same and just hook into
|
5
|
+
# the defaults to provide non-nil results when needed.
|
6
|
+
#
|
7
|
+
# @!attribute long_name [rw]
|
8
|
+
# @return [String] An application name, useful if your app is named differently then your binary.
|
9
|
+
# @!attribute cache_dir [rw]
|
10
|
+
# @return [String] The directory to store any cache related files.
|
11
|
+
# @!attribute config_dir [rw]
|
12
|
+
# @return [String] The directory to store any config related files.
|
13
|
+
# @!attribute version [rw]
|
14
|
+
# @return [String] The version of the application, not of Bini.
|
15
|
+
module Bini
|
16
|
+
extend self
|
17
|
+
|
18
|
+
# A collection of sane defaults to be provided if the same attr is still nil.
|
19
|
+
attr_accessor :defaults
|
20
|
+
attr_accessor :long_name
|
21
|
+
attr_accessor :cache_dir
|
22
|
+
attr_accessor :config_dir
|
23
|
+
attr_accessor :data_dir
|
24
|
+
attr_accessor :version
|
25
|
+
|
26
|
+
@defaults = {}
|
27
|
+
@defaults[:long_name] = Proc.new { $0.split("/").last }
|
28
|
+
@defaults[:cache_dir] = Proc.new { "#{Dir.home}/.cache/#{@long_name}" }
|
29
|
+
@defaults[:config_dir] = Proc.new { "#{Dir.home}/.config/#{@long_name}" }
|
30
|
+
@defaults[:data_dir] = Proc.new { "#{Dir.home}/.local/share/#{@long_name}" }
|
31
|
+
@defaults[:version] = Proc.new { "v0.0.0" }
|
32
|
+
|
33
|
+
# Dynamic attribute's based off the defaults.
|
34
|
+
def keys
|
35
|
+
Bini.instance_methods.select do |m|
|
36
|
+
m =~ /=$/ && m != :defaults=
|
37
|
+
end.map do |m|
|
38
|
+
m[0..-2].to_sym
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Reset the defaults back to nothing.
|
43
|
+
def clear
|
44
|
+
@defaults.each do |key,value|
|
45
|
+
instance_variable_set "@#{key}", nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
keys.each do |key|
|
49
|
+
define_method(key) do
|
50
|
+
v = instance_variable_get "@#{key}"
|
51
|
+
return v if v
|
52
|
+
|
53
|
+
@defaults[key] ? default = @defaults[key].call : default = nil
|
54
|
+
return default
|
55
|
+
end
|
56
|
+
define_method("#{key}=".to_sym) do |dir|
|
57
|
+
instance_variable_set "@#{key}", dir
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Adds a rails style configure method.
|
62
|
+
def configure
|
63
|
+
yield self
|
64
|
+
parameters
|
65
|
+
end
|
66
|
+
|
67
|
+
# List available parameters and values in those params
|
68
|
+
def parameters
|
69
|
+
@values = {}
|
70
|
+
keys.each { |k| @values.merge! k => Bini.send(k) }
|
71
|
+
@values
|
72
|
+
end
|
73
|
+
alias_method :params, :parameters
|
74
|
+
end
|
75
|
+
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module Bini
|
2
|
+
module Extensions
|
3
|
+
module Savable
|
4
|
+
|
5
|
+
# Lets setup some options.
|
6
|
+
def initialize
|
7
|
+
options[:dirty] = false
|
8
|
+
options[:file] = "#{Bini.config_dir}/#{Bini.long_name}/savable.yaml"
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
12
|
+
def []=(key,value)
|
13
|
+
super key, value
|
14
|
+
if options[:autosave] == true
|
15
|
+
save
|
16
|
+
else
|
17
|
+
options[:dirty] = true
|
18
|
+
end
|
19
|
+
return value
|
20
|
+
end
|
21
|
+
|
22
|
+
# save self into a file
|
23
|
+
def save
|
24
|
+
return false if options[:dirty] && options[:dirty] != true
|
25
|
+
|
26
|
+
FileUtils.mkdir_p basedir if !Dir.exist? basedir
|
27
|
+
|
28
|
+
# I do this the long way because I want an immediate sync.
|
29
|
+
f = open(options[:file], 'w')
|
30
|
+
|
31
|
+
# make a plan hash, save that instead of the class.
|
32
|
+
f.write(YAML::dump({}.merge(self)))
|
33
|
+
f.sync
|
34
|
+
f.close
|
35
|
+
|
36
|
+
backup if options[:backup]
|
37
|
+
set_mode if options[:mode]
|
38
|
+
options[:dirty] = false
|
39
|
+
return true
|
40
|
+
end
|
41
|
+
|
42
|
+
def load
|
43
|
+
self.clear
|
44
|
+
if options[:file] && File.exist?(options[:file]) && File.stat(options[:file]).size > 0
|
45
|
+
h = YAML::load open(options[:file], 'r').read
|
46
|
+
h.each { |k,v| self[k] = v}
|
47
|
+
end
|
48
|
+
return self
|
49
|
+
end
|
50
|
+
|
51
|
+
# Generate a backup file real quick.
|
52
|
+
def backup
|
53
|
+
FileUtils.cp options[:file], backup_file if File.file? options[:file]
|
54
|
+
end
|
55
|
+
|
56
|
+
# Set the mode of both the save file and backup file.
|
57
|
+
def set_mode
|
58
|
+
FileUtils.chmod options[:mode], options[:file] if options[:mode] && File.exist?(options[:file])
|
59
|
+
|
60
|
+
return true if !backup_file
|
61
|
+
FileUtils.chmod options[:mode], backup_file if File.exist? backup_file
|
62
|
+
return true
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
# has something updated that means we need to save
|
67
|
+
def is_dirty?
|
68
|
+
return true if options[:dirty] == true
|
69
|
+
return false
|
70
|
+
end
|
71
|
+
|
72
|
+
# clean the system of residual files (obviously destructive).
|
73
|
+
def clean_files
|
74
|
+
#FileUtils.rm options[:file], v
|
75
|
+
return true if !options[:file]
|
76
|
+
FileUtils.rm options[:file] if File.file? options[:file]
|
77
|
+
FileUtils.rm backup_file if File.file? backup_file
|
78
|
+
return true
|
79
|
+
end
|
80
|
+
|
81
|
+
def options
|
82
|
+
@options ||= Hash.new
|
83
|
+
end
|
84
|
+
|
85
|
+
# The base directory of the save file.
|
86
|
+
def basedir
|
87
|
+
File.dirname File.absolute_path options[:file]
|
88
|
+
end
|
89
|
+
|
90
|
+
# The save file plus an extension.
|
91
|
+
def backup_file
|
92
|
+
"#{options[:file]}.bak"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/lib/bini/sash.rb
CHANGED
@@ -5,92 +5,24 @@ module Bini
|
|
5
5
|
# This is a savable hash, it can be configured and used to store whatever the# contents of the hash are for loading later. Will serialize in yaml to keep all
|
6
6
|
# the dependencies in ruby stdlib.
|
7
7
|
class Sash < Hash
|
8
|
+
include Bini::Extensions::Savable
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
attr_accessor :mode
|
12
|
-
attr_accessor :auto_load
|
13
|
-
attr_accessor :auto_save
|
14
|
-
|
15
|
-
# initialization sets the values of the class Sash, not the contents of the Hash.
|
10
|
+
# overrides:{Hash.new}
|
11
|
+
# options: Sash options.
|
16
12
|
def initialize(params = {})
|
17
|
-
params
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
# The base directory of the save file.
|
22
|
-
def basedir
|
23
|
-
return nil if !file
|
24
|
-
File.dirname File.absolute_path @file
|
25
|
-
end
|
26
|
-
|
27
|
-
# The save file plus an extension.
|
28
|
-
def backup_file
|
29
|
-
"#{@file}.bak"
|
30
|
-
end
|
31
|
-
|
32
|
-
# Save the hash to the file, check for backup and set_mode.
|
33
|
-
def save
|
34
|
-
if any?
|
35
|
-
FileUtils.mkdir_p basedir if !Dir.exist? basedir
|
36
|
-
backup if @backup
|
37
|
-
|
38
|
-
# I do this the long way because I want an immediate sync.
|
39
|
-
f = open(@file, 'w')
|
40
|
-
f.write YAML::dump self
|
41
|
-
f.sync
|
42
|
-
f.close
|
43
|
-
|
44
|
-
set_mode if @mode
|
45
|
-
end
|
46
|
-
true
|
47
|
-
end
|
48
|
-
# Store a value in the Hash. Can autosave.
|
49
|
-
def []=(key,value)
|
50
|
-
store key, value
|
51
|
-
save! if @auto_save == true
|
52
|
-
end
|
53
|
-
# Save the hash to a file, overwriting if necessary.
|
54
|
-
def save!
|
55
|
-
delete_file
|
56
|
-
save
|
57
|
-
end
|
13
|
+
# if we get any params not listed above, throw an exception.
|
14
|
+
p = params.select { |k,v| k != :options && k != :overrides}
|
15
|
+
raise ArgumentError, "Extra values passed in: #{p}" if p.count > 0
|
58
16
|
|
59
|
-
|
60
|
-
|
61
|
-
self.clear
|
62
|
-
if @file && File.exist?(@file) && File.stat(@file).size > 0
|
63
|
-
h = YAML::load open(@file, 'r').read
|
64
|
-
h.each { |k,v| self[k] = v}
|
65
|
-
return true
|
66
|
-
end
|
67
|
-
false
|
68
|
-
end
|
17
|
+
# set our options to our attributes.
|
18
|
+
options.merge! params[:options] if params[:options]
|
69
19
|
|
70
|
-
|
71
|
-
def backup
|
72
|
-
FileUtils.cp @file, backup_file if File.file? @file
|
73
|
-
end
|
20
|
+
load if self.options[:autoload]
|
74
21
|
|
75
|
-
|
76
|
-
def set_mode
|
77
|
-
# Why are we trying to set_mode when we don't even have a file?
|
78
|
-
return false if !@file
|
79
|
-
File.chmod @mode, @file if File.exist? @file
|
22
|
+
self.merge! params[:overrides] if params[:overrides]
|
80
23
|
|
81
|
-
|
82
|
-
return true if !backup_file
|
83
|
-
File.chmod @mode, backup_file if File.exist? backup_file
|
84
|
-
true
|
85
|
-
end
|
86
|
-
|
87
|
-
private
|
88
|
-
|
89
|
-
# Delete the save file.
|
90
|
-
def delete_file
|
91
|
-
return false if !@file
|
92
|
-
FileUtils.rm @file if File.file? @file
|
93
|
-
return true
|
24
|
+
return self
|
94
25
|
end
|
95
26
|
end
|
96
27
|
end
|
28
|
+
|
data/lib/bini/version.rb
CHANGED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class TestClass < Hash
|
4
|
+
include Bini::Extensions::Savable
|
5
|
+
end
|
6
|
+
|
7
|
+
describe Bini::Extensions::Savable do
|
8
|
+
before (:each) do
|
9
|
+
Bini.config_dir = "tmp/"
|
10
|
+
Bini.long_name = "savable_test"
|
11
|
+
@obj = TestClass.new
|
12
|
+
@obj.clean_files
|
13
|
+
end
|
14
|
+
|
15
|
+
it "will (auto)save" do
|
16
|
+
@obj[:foo] = 'bar'
|
17
|
+
@obj.save
|
18
|
+
@obj.clear
|
19
|
+
@obj.should eq({})
|
20
|
+
@obj.load
|
21
|
+
@obj[:foo].should eq 'bar'
|
22
|
+
@obj.options[:autosave] = true
|
23
|
+
@obj[:fooboo] = 'bar2'
|
24
|
+
@obj.clear
|
25
|
+
@obj.load
|
26
|
+
@obj[:fooboo].should eq 'bar2'
|
27
|
+
end
|
28
|
+
|
29
|
+
it "will know when it needs to save" do
|
30
|
+
@obj[:dirttty] = 'twue'
|
31
|
+
@obj.is_dirty?.should be_true
|
32
|
+
@obj.save
|
33
|
+
@obj.is_dirty?.should be_false
|
34
|
+
end
|
35
|
+
|
36
|
+
it "will generate a backup on request" do
|
37
|
+
@obj.options[:backup] = true
|
38
|
+
@obj[:back] = 'bbbb'
|
39
|
+
@obj.save
|
40
|
+
File.exists?(@obj.backup_file).should be_true
|
41
|
+
end
|
42
|
+
it "will set mode on request" do
|
43
|
+
@obj.options[:mode] = 0600
|
44
|
+
@obj[:m] == 'mmm'
|
45
|
+
@obj.save
|
46
|
+
# According to the docs, this is likely platform dependent. Need a mac
|
47
|
+
# to test.
|
48
|
+
File.stat(@obj.options[:file]).mode.should eq 0100600
|
49
|
+
end
|
50
|
+
it "will clean up after itself if requested" do
|
51
|
+
@obj[:too] = 'foo'
|
52
|
+
@obj.save
|
53
|
+
@obj.clean_files
|
54
|
+
File.exist?(@obj.options[:file]).should be_false
|
55
|
+
File.exist?(@obj.backup_file).should be_false
|
56
|
+
end
|
57
|
+
end
|
@@ -43,7 +43,7 @@ describe "optparser" do
|
|
43
43
|
describe "parse" do
|
44
44
|
it "will attempt to mash in the Config[]#hash if available." do
|
45
45
|
Bini::Config[@key] == @value
|
46
|
-
Options.parse
|
46
|
+
Options.parse
|
47
47
|
Options[@key] = @value
|
48
48
|
end
|
49
49
|
it "Will echo the version set by Bini.version" do
|
data/spec/lib/bini/sash_spec.rb
CHANGED
@@ -1,79 +1,34 @@
|
|
1
|
-
require '
|
2
|
-
|
3
|
-
include Bini
|
4
|
-
|
5
|
-
describe "Sash" do
|
6
|
-
def random_file
|
7
|
-
"tmp/sash-#{(0...4).map{rand(16).to_s(16)}.join}.yaml"
|
8
|
-
end
|
9
|
-
|
10
|
-
before (:all) do
|
11
|
-
FileUtils.mkdir_p 'tmp'
|
12
|
-
end
|
1
|
+
require 'spec_helper'
|
13
2
|
|
3
|
+
describe Bini::Sash do
|
14
4
|
before (:each) do
|
15
|
-
@filename =
|
16
|
-
@s = Sash.new file:@filename
|
5
|
+
@filename = "tmp/sash_savefile.yaml"
|
6
|
+
@s = Bini::Sash.new options:{file:@filename}
|
17
7
|
@s[:before_each] = true
|
18
8
|
end
|
19
9
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
it "can select the filename" do
|
25
|
-
@s.file.should be @filename
|
26
|
-
end
|
27
|
-
|
28
|
-
it "can save" do
|
29
|
-
@s['foo'] = :bar
|
30
|
-
@s.save
|
31
|
-
@s2 = Sash.new file:@filename
|
32
|
-
@s2.load
|
33
|
-
@s2['foo'].should eq :bar
|
10
|
+
it "Can pass overrides via overrides:{}" do
|
11
|
+
@s2 = Bini::Sash.new(overrides:{foo: :bar})
|
12
|
+
@s2.should include(:foo)
|
34
13
|
end
|
35
14
|
|
36
|
-
it
|
37
|
-
@s.mode = 0600
|
38
|
-
@s.set_mode
|
15
|
+
it 'will fail gracefully if nothing to load.' do
|
39
16
|
@s.save
|
40
|
-
|
41
|
-
|
42
|
-
|
17
|
+
FileUtils.rm @filename
|
18
|
+
@s.load.should eq({})
|
19
|
+
@s.options[:file] = nil
|
20
|
+
@s.load.should eq({})
|
21
|
+
FileUtils.touch @filename
|
22
|
+
@s.load.should eq({})
|
43
23
|
end
|
44
|
-
|
45
|
-
|
46
|
-
@s = Sash.new file:@filename, auto_save:true
|
47
|
-
@s[:auto_save] = true
|
48
|
-
@s2 = Sash.new file:@filename
|
49
|
-
@s2.load
|
50
|
-
@s2[:auto_save].should be true
|
24
|
+
it "will raise an exception if you pass in unknown arguments to new." do
|
25
|
+
@s2 = expect { Sash.new(foo:'bar') }.to raise_error
|
51
26
|
end
|
52
27
|
|
53
28
|
it "can auto load" do
|
54
|
-
@s[:
|
55
|
-
@s.save
|
56
|
-
@s2 = Sash.new file:@filename, auto_load:true
|
57
|
-
@s2[:auto_load].should be true
|
58
|
-
end
|
59
|
-
|
60
|
-
# We save twice because in order to produce a backup file, we need an original.
|
61
|
-
it "can make a backup file" do
|
62
|
-
@s.backup = true
|
63
|
-
@s[:backup] = "something"
|
64
|
-
@s.save
|
29
|
+
@s[:autoload] = true
|
65
30
|
@s.save
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
it "will behave like a normal Hash" do
|
70
|
-
@s.kind_of?(Hash).should be_true
|
71
|
-
end
|
72
|
-
|
73
|
-
it "will clear before load, destroying previous contents" do
|
74
|
-
@s[:clear] = 'clear'
|
75
|
-
@s.load
|
76
|
-
@s[:clear].should be_nil
|
31
|
+
@s2 = Bini::Sash.new options:{file:@filename, autoload:true}
|
32
|
+
@s2[:autoload].should be_true
|
77
33
|
end
|
78
34
|
end
|
79
|
-
|
data/spec/lib/bini_spec.rb
CHANGED
@@ -3,31 +3,24 @@ require 'spec_helper'
|
|
3
3
|
# I'm not 100% sure convinvced any of these do what I think they do.
|
4
4
|
describe "Bini" do
|
5
5
|
before (:each) do
|
6
|
-
Bini.
|
7
|
-
@long_name = Bini.long_name
|
8
|
-
Bini.cache_dir = nil
|
9
|
-
@cache_dir = Bini.cache_dir
|
10
|
-
Bini.config_dir = nil
|
11
|
-
@config_dir = Bini.config_dir
|
12
|
-
Bini.version = nil
|
6
|
+
Bini.clear
|
13
7
|
end
|
14
8
|
|
15
|
-
describe "
|
16
|
-
it
|
17
|
-
Bini.long_name = "
|
18
|
-
Bini.
|
19
|
-
Bini.cache_dir
|
20
|
-
Bini.config_dir = "tmp/there"
|
21
|
-
Bini.parameters?.should be_true
|
9
|
+
describe "long_name" do
|
10
|
+
it "Will update the directories automatically." do
|
11
|
+
Bini.long_name = "rspecify"
|
12
|
+
Bini.long_name.should eq "rspecify"
|
13
|
+
Bini.cache_dir.should eq "#{Dir.home}/.cache/rspecify"
|
22
14
|
end
|
23
15
|
end
|
24
16
|
|
17
|
+
describe
|
25
18
|
# Dynamically generate our key tests, more for fun then any real need.
|
26
19
|
Bini.keys.each do |key|
|
27
20
|
describe "#{key}" do
|
28
21
|
it "can be reset to a default" do
|
29
22
|
Bini.send "#{key}=", nil
|
30
|
-
Bini.send(key).should eq Bini.defaults[key]
|
23
|
+
Bini.send(key).should eq Bini.defaults[key].call
|
31
24
|
end
|
32
25
|
it "can be overriden" do
|
33
26
|
r = random_hex 16
|
@@ -45,6 +38,5 @@ describe "Bini" do
|
|
45
38
|
end
|
46
39
|
end
|
47
40
|
|
48
|
-
Bini.version
|
49
41
|
end
|
50
42
|
|
data/spec/spec_helper.rb
CHANGED
@@ -1,12 +1,10 @@
|
|
1
1
|
require 'simplecov'
|
2
|
+
require 'bini'
|
3
|
+
|
2
4
|
SimpleCov.start do
|
3
5
|
add_filter "/spec/"
|
4
6
|
end
|
5
7
|
|
6
|
-
require 'bini'
|
7
|
-
require 'bini/optparser'
|
8
|
-
require 'bini/config'
|
9
|
-
|
10
8
|
include Bini
|
11
9
|
|
12
10
|
# This file was generated by the `rspec --init` command. Conventionally, all
|
metadata
CHANGED
@@ -1,30 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bini
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.7.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Ernie Brodeur
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2013-05-27 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: sys-proctable
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
description: Bini needs no description.
|
@@ -47,6 +44,8 @@ files:
|
|
47
44
|
- bini.gemspec
|
48
45
|
- lib/bini.rb
|
49
46
|
- lib/bini/config.rb
|
47
|
+
- lib/bini/core.rb
|
48
|
+
- lib/bini/extensions/savable.rb
|
50
49
|
- lib/bini/filemagic.rb
|
51
50
|
- lib/bini/mixins.rb
|
52
51
|
- lib/bini/optparser.rb
|
@@ -54,6 +53,7 @@ files:
|
|
54
53
|
- lib/bini/sash.rb
|
55
54
|
- lib/bini/version.rb
|
56
55
|
- spec/lib/bini/config_spec.rb
|
56
|
+
- spec/lib/bini/extensions/savable_spec.rb
|
57
57
|
- spec/lib/bini/filemagic_spec.rb
|
58
58
|
- spec/lib/bini/mixins_spec.rb
|
59
59
|
- spec/lib/bini/optparser_spec.rb
|
@@ -62,32 +62,32 @@ files:
|
|
62
62
|
- spec/spec_helper.rb
|
63
63
|
homepage: https://github.com/erniebrodeur/bini
|
64
64
|
licenses: []
|
65
|
+
metadata: {}
|
65
66
|
post_install_message:
|
66
67
|
rdoc_options: []
|
67
68
|
require_paths:
|
68
69
|
- lib
|
69
70
|
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
-
none: false
|
71
71
|
requirements:
|
72
|
-
- -
|
72
|
+
- - '>='
|
73
73
|
- !ruby/object:Gem::Version
|
74
74
|
version: '0'
|
75
75
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
-
none: false
|
77
76
|
requirements:
|
78
|
-
- -
|
77
|
+
- - '>='
|
79
78
|
- !ruby/object:Gem::Version
|
80
79
|
version: '0'
|
81
80
|
requirements: []
|
82
81
|
rubyforge_project:
|
83
|
-
rubygems_version:
|
82
|
+
rubygems_version: 2.0.0
|
84
83
|
signing_key:
|
85
|
-
specification_version:
|
84
|
+
specification_version: 4
|
86
85
|
summary: Bini is a gem that helps me build CLI tools. It's not thor, trollop, or
|
87
86
|
any of the other major frameworks. It makes lots and lots of assumptions. It's
|
88
87
|
probably not for you.
|
89
88
|
test_files:
|
90
89
|
- spec/lib/bini/config_spec.rb
|
90
|
+
- spec/lib/bini/extensions/savable_spec.rb
|
91
91
|
- spec/lib/bini/filemagic_spec.rb
|
92
92
|
- spec/lib/bini/mixins_spec.rb
|
93
93
|
- spec/lib/bini/optparser_spec.rb
|