harshed 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 460df5ea911680f7ee2a14085d74db9495668bec
4
- data.tar.gz: fce9d965d4c8f17e70803a98f4363b4617854123
3
+ metadata.gz: 28bb5bf44b780690fbbe646c505cb02f0bc7ef38
4
+ data.tar.gz: a9aa91b9e408e57a8bedd5da138a4c60fbcffe77
5
5
  SHA512:
6
- metadata.gz: a56f3abc51a067ca7a054c499218654976c2686b52d8f3651fc2580e953b12ca14d489d9b43dc344ab5033a560c11b60b0bf7b3e1fd2d3c0e62397eb5afe606c
7
- data.tar.gz: 55756d0abc54b57510619c59c8918e35107186a703bdd23f6573706aed33bfb9dde10ab1ee96f7958d58d813afcfe31cd377be792c135fff47acc0016ae00217
6
+ metadata.gz: 8d415cbe289df4e384241b06d3cda246482d49f0fe4f485af1d5b74ebc86c1b81fdf037e2a85a17901dd70e1b93480cd5b82d77feae89a9feffbf12e9c615a2c
7
+ data.tar.gz: 76f166a66a44a80c111db5a2ca3fcc20daa40f2bc663338d3f0cb7c4903c946d616c232e5df738e7babfee38fc4b38ad0c7bebec3cca1f071f9befbac1f97ccb
data/.gitignore CHANGED
@@ -7,4 +7,5 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
- /.idea/
10
+ /.idea/
11
+ *.gem
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.1.5"
4
+ - "2.2.0"
data/Gemfile CHANGED
@@ -3,12 +3,8 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in harshed.gemspec
4
4
  gemspec
5
5
 
6
- gem 'faker'
7
- gem 'yamlable'
8
-
9
6
  group :development do
10
7
  gem 'minitest'
11
8
  gem 'minitest-reporters'
12
- gem 'reek'
13
- gem 'rubocop'
9
+ gem 'faker'
14
10
  end
data/README.md CHANGED
@@ -1,7 +1,25 @@
1
1
  # Harshed
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/harshed.svg)](https://badge.fury.io/rb/harshed)
4
+ [![Build Status](https://travis-ci.org/folkengine/harshed.svg?branch=master)](https://travis-ci.org/folkengine/harshed)
5
+
3
6
  Ruby Serializable Hashed Array Utility
4
7
 
8
+ A Harsh is a hashed array that can be quickly written to and read from disk in a format that can be easily reviewed.
9
+ Take a collection of same type objects. Pick a pivot point, aka, a field unique to each object that can be used as the key
10
+ for the hash.
11
+
12
+ I wrote this module to support development/testing techniques that I've grown use to. As a developer I am squarely in the
13
+ [classical, anti-mockist camp](https://agilewarrior.wordpress.com/2015/04/18/classical-vs-mockist-testing/). Harshed helps with that.
14
+
15
+ Eventually, I'd like to update this module to support the following features:
16
+
17
+ 1. Support for JSON
18
+ 2. Support for XML
19
+ 3. Ability to pivot on a field not in the object being serialized.
20
+ 4. Harshed DB support
21
+ 5. Deal with the sure to be their defect of spaces on the pivot field
22
+
5
23
  ## Installation
6
24
 
7
25
  Add this line to your application's Gemfile:
@@ -43,13 +61,14 @@ Or install it yourself as:
43
61
 
44
62
  @heroes.to_disk
45
63
 
46
- @heroes_reborn = Harshed.new(:character_name, storage_folder: 'characters').from_disk
64
+ @heroes_reborn = Harshed.new(:character_name, storage_folder: 'characters')
65
+ .from_disk
47
66
 
48
67
  ## Development
49
68
 
50
- To run tests, Reek, and RubyCop:
69
+ To run tests:
51
70
 
52
- $> rake boom
71
+ $> rake
53
72
 
54
73
  ## Contributing
55
74
 
data/Rakefile CHANGED
@@ -1,26 +1,10 @@
1
1
  require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
- require 'reek/rake/task'
4
- require 'rubocop/rake_task'
5
3
 
6
4
  task default: :test
7
5
 
8
- Reek::Rake::Task.new do |t|
9
- t.fail_on_error = false
10
- end
11
-
12
- RuboCop::RakeTask.new
13
-
14
6
  Rake::TestTask.new do |t|
15
7
  t.libs << 'test'
16
8
  t.test_files = FileList['test/**/test*.rb']
17
9
  t.verbose = true
18
10
  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
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "harshed"
5
+
6
+ require "irb"
7
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -1,2 +1,2 @@
1
- .yml
2
- .yaml
1
+ *.yml
2
+ *.yaml
@@ -1,20 +1,22 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'harshed'
4
+ require 'harshed/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'harshed'
8
8
  spec.version = Harshed::VERSION
9
9
  spec.authors = ['Folkengine']
10
10
  spec.email = ['gaoler@electronicpanopticon.com']
11
+ spec.license = 'MIT'
11
12
 
12
13
  spec.summary = 'Ruby Serializable Hashed Array Utility'
13
- spec.description = ''
14
+ spec.description = 'Utility to quickly save and retrieve objects from disk'
14
15
  spec.homepage = 'https://github.com/folkengine/harshed'
15
16
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
17
  spec.require_paths = ['lib']
17
18
 
18
- spec.add_development_dependency 'bundler', '~> 1.11'
19
- spec.add_development_dependency 'rake', '~> 10.0'
19
+ spec.add_runtime_dependency 'yamlable', '>= 0.0.2'
20
+ spec.add_development_dependency 'bundler', '>= 1.7.6'
21
+ spec.add_development_dependency 'rake', "~> 10.0"
20
22
  end
@@ -1,101 +1,10 @@
1
1
  require 'fileutils'
2
2
  require 'yamlable'
3
+ require 'harshed/harsh'
4
+ require 'harshed/version'
3
5
 
4
6
  # 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
-
7
+ module Harshed
99
8
  def self.default_base_folder
100
9
  File.join(File.dirname(__FILE__), '..', 'data')
101
10
  end
@@ -0,0 +1,95 @@
1
+ # Ruby Serializable Hashed Array Utility
2
+ module Harshed
3
+ class Harsh
4
+ include Yamlable
5
+
6
+ attr_reader :hash, :key_to_map
7
+
8
+ def initialize(key_to_map, items = [],
9
+ storage_base: Harshed.default_base_folder,
10
+ storage_folder: '')
11
+ @key_to_map = key_to_map
12
+ @hash = {}
13
+ @storage_folder = storage_folder
14
+ @storage_base = storage_base
15
+ store(items)
16
+ end
17
+
18
+ def store(items)
19
+ # Hack to get around how Array() mangles Hashes and Structs
20
+ if items.is_a?(Array)
21
+ items.each { |item| store_item(item) }
22
+ else
23
+ store_item(items)
24
+ end
25
+ end
26
+
27
+ def store_item(item)
28
+ validate_type(item)
29
+ @hash[item.send(@key_to_map)] = item
30
+ end
31
+
32
+ def valid_type?(item)
33
+ @hash.values.last.class == item.class
34
+ end
35
+
36
+ def validate_type(item)
37
+ return if @hash.empty?
38
+ unless valid_type?(item)
39
+ raise TypeError "Harshed Object class types (#{last_class}/#{item_class}) must be the same."
40
+ end
41
+ end
42
+
43
+ def to_a
44
+ @hash.values
45
+ end
46
+
47
+ def key?(key)
48
+ @hash.key?(key)
49
+ end
50
+
51
+ def value(my_key)
52
+ @hash[my_key]
53
+ end
54
+
55
+ def sample
56
+ @hash.values.sample
57
+ end
58
+
59
+ def from_disk
60
+ Dir["#{folder_path}/*.yml"].each do |filename|
61
+ yml = File.read(filename)
62
+ store(Psych.load(yml, filename))
63
+ end
64
+ self
65
+ end
66
+
67
+ # This method smells of :reek:NestedIterators
68
+ def to_disk
69
+ mkdir
70
+ @hash.each do |key, value|
71
+ File.open(filename(key), 'w') { |file| file.write(value.to_yaml) }
72
+ end
73
+ end
74
+
75
+ def mkdir
76
+ Dir.mkdir(folder_path) unless Dir.exist?(folder_path)
77
+ end
78
+
79
+ def rm_r
80
+ FileUtils.remove_dir folder_path
81
+ end
82
+
83
+ def folder_path
84
+ if @storage_folder.empty?
85
+ File.join(@storage_base, @hash.values.first.class.to_s)
86
+ else
87
+ File.join(@storage_base, @storage_folder)
88
+ end
89
+ end
90
+
91
+ def filename(key)
92
+ File.join(folder_path, "#{key}.yml")
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,3 @@
1
+ module Harshed
2
+ VERSION = '0.1.2'.freeze
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: harshed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Folkengine
@@ -10,20 +10,34 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2016-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yamlable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.2
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
- - - "~>"
31
+ - - ">="
18
32
  - !ruby/object:Gem::Version
19
- version: '1.11'
33
+ version: 1.7.6
20
34
  type: :development
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
- - - "~>"
38
+ - - ">="
25
39
  - !ruby/object:Gem::Version
26
- version: '1.11'
40
+ version: 1.7.6
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -38,7 +52,7 @@ dependencies:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
54
  version: '10.0'
41
- description: ''
55
+ description: Utility to quickly save and retrieve objects from disk
42
56
  email:
43
57
  - gaoler@electronicpanopticon.com
44
58
  executables: []
@@ -47,15 +61,21 @@ extra_rdoc_files: []
47
61
  files:
48
62
  - ".gitignore"
49
63
  - ".rubocop.yml"
64
+ - ".travis.yml"
50
65
  - Gemfile
51
66
  - LICENSE
52
67
  - README.md
53
68
  - Rakefile
69
+ - bin/console
70
+ - bin/setup
54
71
  - data/.gitignore
55
72
  - harshed.gemspec
56
73
  - lib/harshed.rb
74
+ - lib/harshed/harsh.rb
75
+ - lib/harshed/version.rb
57
76
  homepage: https://github.com/folkengine/harshed
58
- licenses: []
77
+ licenses:
78
+ - MIT
59
79
  metadata: {}
60
80
  post_install_message:
61
81
  rdoc_options: []