flat_hash 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Mark Ryall
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/README.rdoc ADDED
@@ -0,0 +1,25 @@
1
+ = Flat Hash
2
+
3
+ Serialisation for hash instances.
4
+
5
+ == Rationale
6
+
7
+ It's a bit of a struggle to justify creating this gem. It just encapsulates some functionality that i'd built twice in other gems (shh and cardigan). In both cases, I wanted to be able to manage a collection of serialised hash instances on all platforms in a version control system. I'd used yaml initially but found that serialising the same hash on windows vs linux would produced slight differences.
8
+
9
+ At this stage, only the serialisation/deserialisation has been completed.
10
+
11
+ It will also need to accomodate rewriting hashes that had been serialised as yaml.
12
+
13
+ == Usage
14
+
15
+ === FlatHash::Serialiser
16
+
17
+ This class is just used to read and write hashes from disk
18
+
19
+ Reading:
20
+
21
+ hash = FlatHash::Serialiser.new(File.open('hash)).read
22
+
23
+ Writing:
24
+
25
+ FlatHash::Serialiser.new(File.open('hash', 'w')).write(hash)
@@ -0,0 +1,3 @@
1
+ module FlatHash
2
+ Changeset = Struct.new :id, :time, :author, :modifications, :additions, :deletions, :description
3
+ end
@@ -0,0 +1,4 @@
1
+ changeset = "{node}\n{date|date}\n{author}{files}\nadditions:{file_adds}\ndeletions:{file_dels}\ndescription:\n{desc|escape}\n"
2
+ file = "\n{file|escape}"
3
+ file_add = "\n{file_add|escape}"
4
+ file_del = "\n{file_del|escape}"
@@ -0,0 +1,57 @@
1
+ require 'fileutils'
2
+
3
+ module FlatHash; end
4
+
5
+ class Object
6
+ def metaclass
7
+ (class << self; self; end)
8
+ end
9
+
10
+ def meta_eval &block
11
+ metaclass.instance_eval &block
12
+ end
13
+ end
14
+
15
+ class FlatHash::Directory
16
+ include Enumerable
17
+
18
+ def initialize serialiser, path
19
+ @serialiser, @path = serialiser, path
20
+ end
21
+
22
+ def each
23
+ return unless File.exist?(@path)
24
+ Dir.foreach(@path) do |path|
25
+ basename = File.basename(path)
26
+ yield basename unless ['.','..'].include?(basename)
27
+ end
28
+ end
29
+
30
+ def destroy key
31
+ FileUtils.rm(File.join(@path,key))
32
+ end
33
+
34
+ def << entry
35
+ write entry.name, entry.content
36
+ end
37
+
38
+ def read key
39
+ File.open(File.join(@path,key)) {|io| add_key(@serialiser.read(io), key) }
40
+ end
41
+
42
+ alias :[] :read
43
+
44
+ def write key, hash
45
+ FileUtils.mkdir_p @path
46
+ File.open(File.join(@path,key),'w') {|io| @serialiser.write io, hash }
47
+ end
48
+
49
+ alias :[]= :write
50
+ private
51
+ def add_key hash, key
52
+ hash.meta_eval {
53
+ define_method(:id) { key }
54
+ }
55
+ hash
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ module FlatHash
2
+ Entry = Struct.new(:name, :content)
3
+ end
@@ -0,0 +1,67 @@
1
+ require 'flat_hash/vcs'
2
+ require 'flat_hash/changeset'
3
+ require 'grit'
4
+
5
+ class FlatHash::Git < FlatHash::Vcs
6
+ def git
7
+ @git ||= Grit::Repo.new('.')
8
+ end
9
+
10
+ def name
11
+ :git
12
+ end
13
+
14
+ def addremovecommit comment
15
+ execute "add -A"
16
+ execute "ls-files --deleted -z | xargs -0 git rm"
17
+ commit comment
18
+ end
19
+
20
+ def changesets path='.'
21
+ sh("git log --format=%H -- #{path}") do |status, lines|
22
+ raise "failed with status #{status}:\n#{lines.join("\n")}" unless status.exitstatus == 128
23
+ []
24
+ end
25
+ end
26
+
27
+ def changeset id
28
+ change = FlatHash::Changeset.new
29
+ commit = git.commit(id)
30
+ change.id = commit.sha
31
+ change.time = commit.date
32
+ change.author = commit.author.to_s
33
+ all_changes = execute("show --pretty=\"format:\" --name-only #{commit}")
34
+ all_changes.shift
35
+ change.additions = commit.diffs.select {|diff| diff.new_file}.map {|diff| diff.a_path }.uniq
36
+ change.deletions = []
37
+ change.modifications = []
38
+ all_changes.each do |path|
39
+ if commit_contains(commit,path)
40
+ change.modifications << path
41
+ else
42
+ change.deletions << path
43
+ end
44
+ end
45
+ change.modifications = change.modifications - change.additions
46
+ change.description = commit.message
47
+ change
48
+ end
49
+
50
+ def content_at path, commit
51
+ execute("show #{commit}:#{path}").join("\n")
52
+ end
53
+
54
+ def files_at commit, path=nil
55
+ tree = git.commit(commit).tree
56
+ tree = tree/path if path
57
+ tree ? tree.blobs.map {|blob| path ? "#{path}/#{blob.name}" : blob.name } : []
58
+ end
59
+
60
+ def contains? id, path
61
+ commit_contains git.commit(id), path
62
+ end
63
+ private
64
+ def commit_contains commit, path
65
+ commit.tree/path
66
+ end
67
+ end
@@ -0,0 +1,54 @@
1
+ require 'flat_hash/vcs'
2
+ require 'flat_hash/changeset'
3
+
4
+ class FlatHash::Hg < FlatHash::Vcs
5
+ def name
6
+ :hg
7
+ end
8
+
9
+ def addremovecommit comment
10
+ execute "addremove"
11
+ commit comment
12
+ end
13
+
14
+ def changesets path='.'
15
+ execute "log --removed --template \"{node}\\n\" #{path}"
16
+ end
17
+
18
+ def changeset id
19
+ change = FlatHash::Changeset.new
20
+ style = File.join(File.dirname(__FILE__), 'delta.hg')
21
+ lines = sh("hg log --style \"#{style}\" -r #{id} --removed")
22
+ change.id = lines.shift
23
+ change.time = lines.shift
24
+ change.author = lines.shift
25
+ change.modifications = read_until(lines, 'additions:')
26
+ change.additions = read_until(lines, 'deletions:')
27
+ change.deletions = read_until(lines, 'description:')
28
+ change.modifications = change.modifications - change.additions
29
+ change.modifications = change.modifications - change.deletions
30
+ change.description = lines.join("\n")
31
+ change
32
+ end
33
+
34
+ def content_at path, commit
35
+ execute("cat -r #{commit} #{path}").join("\n")
36
+ end
37
+
38
+ def files_at commit, path='.'
39
+ sh "hg locate -r #{commit} '#{path}/*'" do |status, lines|
40
+ raise "failed with status #{status.exitstatus}:\n#{lines.join("\n")}" unless status.exitstatus == 1
41
+ []
42
+ end
43
+ end
44
+ private
45
+ def read_until lines, end_line
46
+ entries = []
47
+ loop do
48
+ line = lines.shift
49
+ break if line == end_line
50
+ entries << line
51
+ end
52
+ entries
53
+ end
54
+ end
@@ -0,0 +1,18 @@
1
+ require 'forwardable'
2
+ require 'flat_hash/directory'
3
+ require 'flat_hash/git'
4
+ require 'flat_hash/hg'
5
+
6
+ module FlatHash
7
+ class Repository < Directory
8
+ extend Forwardable
9
+ def_delegators :@vcs, :changesets
10
+
11
+ def initialize serialiser, path
12
+ super
13
+ @vcs = Git.new if File.exist?('.git')
14
+ @vcs = Hg.new if File.exist?('.hg')
15
+ raise "could not determine repository type" unless @vcs
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,50 @@
1
+ module FlatHash; end
2
+
3
+ class FlatHash::Serialiser
4
+ def read io
5
+ hash = {}
6
+ first = true
7
+ key, value = nil, ''
8
+ io.each do |line|
9
+ line.chomp!
10
+ return read_as_yaml(io, line) if first and line =~ /^--- /
11
+ first = false
12
+ if key
13
+ if line == '<----->'
14
+ hash[key] = value
15
+ key, value = nil, ''
16
+ else
17
+ value << "\n" if value.length > 0
18
+ value << line
19
+ end
20
+ else
21
+ key = line
22
+ end
23
+ end
24
+ hash[key] = value if key
25
+ hash
26
+ end
27
+
28
+ def write io, hash
29
+ first = true
30
+ hash.keys.sort.each do |key|
31
+ io.puts '<----->' unless first
32
+ io.puts key
33
+ io.puts hash[key]
34
+ first = false
35
+ end
36
+ end
37
+ private
38
+ def read_as_yaml io, line
39
+ sio = StringIO.new(line)
40
+ io.each { |line| sio.puts line.chomp }
41
+ hash = YAML::load(sio.string)
42
+ hash['table'].instance_of?(Hash) ? convert_keys(hash['table']) : hash
43
+ end
44
+
45
+ def convert_keys hash
46
+ converted = {}
47
+ hash.keys.each {|k| converted[k.to_s] = hash[k]}
48
+ converted
49
+ end
50
+ end
@@ -0,0 +1,23 @@
1
+ class FlatHash::Vcs
2
+ def init
3
+ execute "init"
4
+ end
5
+
6
+ def commit comment
7
+ execute "commit -m \"#{comment}\""
8
+ end
9
+
10
+ def execute command
11
+ sh "#{name} #{command}"
12
+ end
13
+
14
+ def sh command
15
+ lines = []
16
+ IO.popen("#{command} 2>&1") {|io| io.each {|l| lines << l.chomp } }
17
+ unless $?.success?
18
+ raise "\"#{command}\" failed with status #{$?}:\n#{lines.join("\n")}" unless block_given?
19
+ return yield($?, lines)
20
+ end
21
+ lines
22
+ end
23
+ end
data/lib/flat_hash.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'flat_hash/entry'
2
+ require 'flat_hash/directory'
3
+ require 'flat_hash/serialiser'
4
+ require 'flat_hash/repository'
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flat_hash
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Mark Ryall
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-15 00:00:00 +10:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: grit
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 1
30
+ - 0
31
+ version: 2.1.0
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rake
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ - 8
44
+ - 7
45
+ version: 0.8.7
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 1
57
+ - 3
58
+ - 0
59
+ version: 1.3.0
60
+ type: :development
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: gemesis
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ - 0
72
+ - 3
73
+ version: 0.0.3
74
+ type: :development
75
+ version_requirements: *id004
76
+ description: |
77
+ Hash serialisation that writes key/value pairs in a predicatable order
78
+
79
+ email: mark@ryall.name
80
+ executables: []
81
+
82
+ extensions: []
83
+
84
+ extra_rdoc_files: []
85
+
86
+ files:
87
+ - lib/flat_hash/changeset.rb
88
+ - lib/flat_hash/delta.hg
89
+ - lib/flat_hash/directory.rb
90
+ - lib/flat_hash/entry.rb
91
+ - lib/flat_hash/git.rb
92
+ - lib/flat_hash/hg.rb
93
+ - lib/flat_hash/repository.rb
94
+ - lib/flat_hash/serialiser.rb
95
+ - lib/flat_hash/vcs.rb
96
+ - lib/flat_hash.rb
97
+ - README.rdoc
98
+ - MIT-LICENSE
99
+ has_rdoc: true
100
+ homepage: http://github.com/markryall/flat_hash
101
+ licenses: []
102
+
103
+ post_install_message:
104
+ rdoc_options: []
105
+
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ requirements: []
123
+
124
+ rubyforge_project:
125
+ rubygems_version: 1.3.6
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: trivial but predictable serialisation for hashes
129
+ test_files: []
130
+