harshed 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rubocop.yml +19 -0
- data/Gemfile +14 -0
- data/LICENSE +21 -0
- data/README.md +56 -0
- data/Rakefile +26 -0
- data/data/.gitignore +2 -0
- data/harshed.gemspec +20 -0
- data/lib/harshed.rb +102 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 460df5ea911680f7ee2a14085d74db9495668bec
|
4
|
+
data.tar.gz: fce9d965d4c8f17e70803a98f4363b4617854123
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a56f3abc51a067ca7a054c499218654976c2686b52d8f3651fc2580e953b12ca14d489d9b43dc344ab5033a560c11b60b0bf7b3e1fd2d3c0e62397eb5afe606c
|
7
|
+
data.tar.gz: 55756d0abc54b57510619c59c8918e35107186a703bdd23f6573706aed33bfb9dde10ab1ee96f7958d58d813afcfe31cd377be792c135fff47acc0016ae00217
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# https://github.com/bbatsov/rubocop/blob/master/config/enabled.yml
|
2
|
+
|
3
|
+
Metrics/AbcSize:
|
4
|
+
Enabled: true
|
5
|
+
Max: 22
|
6
|
+
|
7
|
+
|
8
|
+
Metrics/LineLength:
|
9
|
+
Description: 'Limit lines to 80 characters.'
|
10
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#80-character-limits'
|
11
|
+
Max: 120
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Style/Documentation:
|
15
|
+
Description: 'Document classes and non-namespace modules.'
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Style/StructInheritance:
|
19
|
+
Enabled: false
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Chris Baker
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Harshed
|
2
|
+
|
3
|
+
Ruby Serializable Hashed Array Utility
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'harshed'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$> bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$> gem install harshed
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
class Character
|
24
|
+
attr_reader :character_name, :strength, :dexterity, :constitution, :wisdom, :intelligence, :charisma
|
25
|
+
|
26
|
+
def initialize(character_name)
|
27
|
+
@character_name = character_name
|
28
|
+
@strength = 10
|
29
|
+
@dexterity = 10
|
30
|
+
@constitution = 10
|
31
|
+
@wisdom = 10
|
32
|
+
@intelligence = 10
|
33
|
+
@charisma = 10
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
@heroes = Harshed.new(:character_name, storage_folder: 'characters')
|
38
|
+
|
39
|
+
sir_fumblealot = Character.new('SirFumbleAlot')
|
40
|
+
stinky = Character.new('StinkyTheBeggar')
|
41
|
+
|
42
|
+
@heroes.store([sir_fumblealot, stinky])
|
43
|
+
|
44
|
+
@heroes.to_disk
|
45
|
+
|
46
|
+
@heroes_reborn = Harshed.new(:character_name, storage_folder: 'characters').from_disk
|
47
|
+
|
48
|
+
## Development
|
49
|
+
|
50
|
+
To run tests, Reek, and RubyCop:
|
51
|
+
|
52
|
+
$> rake boom
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/folkengine/harshed.
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'reek/rake/task'
|
4
|
+
require 'rubocop/rake_task'
|
5
|
+
|
6
|
+
task default: :test
|
7
|
+
|
8
|
+
Reek::Rake::Task.new do |t|
|
9
|
+
t.fail_on_error = false
|
10
|
+
end
|
11
|
+
|
12
|
+
RuboCop::RakeTask.new
|
13
|
+
|
14
|
+
Rake::TestTask.new do |t|
|
15
|
+
t.libs << 'test'
|
16
|
+
t.test_files = FileList['test/**/test*.rb']
|
17
|
+
t.verbose = true
|
18
|
+
end
|
19
|
+
|
20
|
+
task :boom do
|
21
|
+
Rake::Task['test'].execute
|
22
|
+
puts 'Running Reek...'
|
23
|
+
Rake::Task['reek'].execute
|
24
|
+
puts
|
25
|
+
Rake::Task['rubocop'].execute
|
26
|
+
end
|
data/data/.gitignore
ADDED
data/harshed.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'harshed'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'harshed'
|
8
|
+
spec.version = Harshed::VERSION
|
9
|
+
spec.authors = ['Folkengine']
|
10
|
+
spec.email = ['gaoler@electronicpanopticon.com']
|
11
|
+
|
12
|
+
spec.summary = 'Ruby Serializable Hashed Array Utility'
|
13
|
+
spec.description = ''
|
14
|
+
spec.homepage = 'https://github.com/folkengine/harshed'
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.require_paths = ['lib']
|
17
|
+
|
18
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
19
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
20
|
+
end
|
data/lib/harshed.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'yamlable'
|
3
|
+
|
4
|
+
# Ruby Serializable Hashed Array Utility
|
5
|
+
class Harshed
|
6
|
+
include Yamlable
|
7
|
+
|
8
|
+
VERSION = '0.1.0'.freeze
|
9
|
+
|
10
|
+
attr_reader :hash, :key_to_map
|
11
|
+
|
12
|
+
def initialize(key_to_map, items = [],
|
13
|
+
storage_base: Harshed.default_base_folder,
|
14
|
+
storage_folder: '')
|
15
|
+
@key_to_map = key_to_map
|
16
|
+
@hash = {}
|
17
|
+
@storage_folder = storage_folder
|
18
|
+
@storage_base = storage_base
|
19
|
+
store(items)
|
20
|
+
end
|
21
|
+
|
22
|
+
def store(items)
|
23
|
+
# Hack to get around how Array() mangles Hashes and Structs
|
24
|
+
if items.is_a?(Array)
|
25
|
+
items.each { |item| store_item(item) }
|
26
|
+
else
|
27
|
+
store_item(items)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def store_item(item)
|
32
|
+
validate_type(item)
|
33
|
+
@hash[item.send(@key_to_map)] = item
|
34
|
+
end
|
35
|
+
|
36
|
+
def valid_type?(item)
|
37
|
+
@hash.values.last.class == item.class
|
38
|
+
end
|
39
|
+
|
40
|
+
def validate_type(item)
|
41
|
+
return if @hash.empty?
|
42
|
+
unless valid_type?(item)
|
43
|
+
raise TypeError "Harshed Object class types (#{last_class}/#{item_class}) must be the same."
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_a
|
48
|
+
@hash.values
|
49
|
+
end
|
50
|
+
|
51
|
+
def key?(key)
|
52
|
+
@hash.key?(key)
|
53
|
+
end
|
54
|
+
|
55
|
+
def value(my_key)
|
56
|
+
@hash[my_key]
|
57
|
+
end
|
58
|
+
|
59
|
+
def sample
|
60
|
+
@hash.values.sample
|
61
|
+
end
|
62
|
+
|
63
|
+
def from_disk
|
64
|
+
Dir["#{folder_path}/*.yml"].each do |filename|
|
65
|
+
yml = File.read(filename)
|
66
|
+
store(Psych.load(yml, filename))
|
67
|
+
end
|
68
|
+
self
|
69
|
+
end
|
70
|
+
|
71
|
+
# This method smells of :reek:NestedIterators
|
72
|
+
def to_disk
|
73
|
+
mkdir
|
74
|
+
@hash.each do |key, value|
|
75
|
+
File.open(filename(key), 'w') { |file| file.write(value.to_yaml) }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def mkdir
|
80
|
+
Dir.mkdir(folder_path) unless Dir.exist?(folder_path)
|
81
|
+
end
|
82
|
+
|
83
|
+
def rm_r
|
84
|
+
FileUtils.remove_dir folder_path
|
85
|
+
end
|
86
|
+
|
87
|
+
def folder_path
|
88
|
+
if @storage_folder.empty?
|
89
|
+
File.join(@storage_base, @hash.values.first.class.to_s)
|
90
|
+
else
|
91
|
+
File.join(@storage_base, @storage_folder)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def filename(key)
|
96
|
+
File.join(folder_path, "#{key}.yml")
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.default_base_folder
|
100
|
+
File.join(File.dirname(__FILE__), '..', 'data')
|
101
|
+
end
|
102
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: harshed
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Folkengine
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: ''
|
42
|
+
email:
|
43
|
+
- gaoler@electronicpanopticon.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rubocop.yml"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- data/.gitignore
|
55
|
+
- harshed.gemspec
|
56
|
+
- lib/harshed.rb
|
57
|
+
homepage: https://github.com/folkengine/harshed
|
58
|
+
licenses: []
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.5.1
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: Ruby Serializable Hashed Array Utility
|
80
|
+
test_files: []
|