document_hash 0.0.1
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.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +30 -0
- data/Rakefile +1 -0
- data/document_hash.gemspec +21 -0
- data/lib/.document.rb.swo +0 -0
- data/lib/.document.rb.swp +0 -0
- data/lib/document.rb +59 -0
- data/lib/document_hash/.version.rb.swp +0 -0
- data/lib/document_hash/version.rb +3 -0
- data/spec/.spec_helper.rb.swp +0 -0
- data/spec/lib/.document_spec.rb.swo +0 -0
- data/spec/lib/.document_spec.rb.swp +0 -0
- data/spec/lib/document_spec.rb +71 -0
- data/spec/spec_helper.rb +8 -0
- metadata +84 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Giancarlo Palavicini
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# Document
|
2
|
+
|
3
|
+
Manages a Document, the document contains multiple nested objects, but changes on any sub-level is notified to the parent
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'document'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install document
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
It mainly iterates from a hash, so any way that's valid for creating a hash should be valid for creating a Document, be aware that if you create a document with a multi-level hash, it will convert any inner hash to a Document in order to privide its main functionality
|
22
|
+
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
|
26
|
+
1. Fork it
|
27
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
28
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
29
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
30
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'document_hash/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "document_hash"
|
8
|
+
gem.version = Document::VERSION
|
9
|
+
gem.authors = ["Giancarlo Palavicini"]
|
10
|
+
gem.email = ["kasthor@gmail.com"]
|
11
|
+
gem.description = %q{Implements a multi-level nested document, that notifies about changes, and some other related features}
|
12
|
+
gem.summary = %q{Document Object}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency "rspec"
|
21
|
+
end
|
Binary file
|
Binary file
|
data/lib/document.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require "document_hash/version"
|
2
|
+
|
3
|
+
module Document
|
4
|
+
class Core < Hash
|
5
|
+
def self.[] *attr
|
6
|
+
super(*attr).tap do|new|
|
7
|
+
new.each do |k,v|
|
8
|
+
p v.class
|
9
|
+
new[k] = new.class[v] if v.is_a?(Hash) && ! v.is_a?(self.class)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def changed
|
15
|
+
changed_attributes.dup.freeze
|
16
|
+
end
|
17
|
+
|
18
|
+
def changed?
|
19
|
+
! changed.empty?
|
20
|
+
end
|
21
|
+
|
22
|
+
def [] key
|
23
|
+
super key.to_sym
|
24
|
+
end
|
25
|
+
|
26
|
+
def []= key, val
|
27
|
+
key = key.to_sym
|
28
|
+
|
29
|
+
if val.is_a? Hash
|
30
|
+
val = self.class[val]
|
31
|
+
val.__send__ :parent=, self;
|
32
|
+
val.__send__ :parent_key=, key;
|
33
|
+
end
|
34
|
+
|
35
|
+
super key, val
|
36
|
+
changed_key key
|
37
|
+
|
38
|
+
parent.__send__ :changed_key, parent_key if parent
|
39
|
+
end
|
40
|
+
|
41
|
+
def reset!
|
42
|
+
changed_attributes.clear
|
43
|
+
|
44
|
+
values.select{|v| v.is_a? self.class }.each{ |v| v.reset! }
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
attr_accessor :parent, :parent_key
|
50
|
+
|
51
|
+
def changed_key key
|
52
|
+
changed_attributes << key
|
53
|
+
end
|
54
|
+
|
55
|
+
def changed_attributes
|
56
|
+
@changed ||= Set.new
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,71 @@
|
|
1
|
+
describe Document::Core do
|
2
|
+
it "inherits from a hash" do
|
3
|
+
subject.is_a? Hash
|
4
|
+
end
|
5
|
+
|
6
|
+
it "knows when a key has changed" do
|
7
|
+
subject[:test] = :test
|
8
|
+
|
9
|
+
subject.should be_changed
|
10
|
+
end
|
11
|
+
|
12
|
+
it "enumerates the changed keys" do
|
13
|
+
subject[:test] = :test
|
14
|
+
|
15
|
+
subject.should include :test
|
16
|
+
end
|
17
|
+
|
18
|
+
it "matches string keys to symbols" do
|
19
|
+
subject[:test] = "test"
|
20
|
+
|
21
|
+
subject["test"].should == "test"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "converts any internal hashes to Documents" do
|
25
|
+
subject[:test] = { inner: "test" }
|
26
|
+
|
27
|
+
subject[:test].should be_a_kind_of Document::Core
|
28
|
+
end
|
29
|
+
|
30
|
+
it "makes it child documents to refer its parent" do
|
31
|
+
subject[:test] = { inner: "test" }
|
32
|
+
|
33
|
+
subject[:test].__send__(:parent).should == subject
|
34
|
+
subject[:test].__send__(:parent_key).should == :test
|
35
|
+
end
|
36
|
+
|
37
|
+
it "notifies its parent when a change ocurr" do
|
38
|
+
subject[:test] = { inner: "test" }
|
39
|
+
subject.__send__(:changed_attributes).should_receive(:<<).with(:test)
|
40
|
+
subject[:test][:inner] = "modified"
|
41
|
+
end
|
42
|
+
|
43
|
+
it "reset its changed status" do
|
44
|
+
subject[:test] = "xxx"
|
45
|
+
expect{
|
46
|
+
subject.reset!
|
47
|
+
}.to change(subject, :changed?).from(true).to(false)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "has a changed status if a child changed" do
|
51
|
+
subject[:test] = { inner: "test" }
|
52
|
+
subject.reset!
|
53
|
+
expect{
|
54
|
+
subject[:test][:inner] = "modified"
|
55
|
+
}.to change(subject, :changed?).from(false).to(true)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "resets child changed status when reseting the root" do
|
59
|
+
subject[:test] = { inner: "test" }
|
60
|
+
subject[:test][:inner] = "modified"
|
61
|
+
subject.reset!
|
62
|
+
|
63
|
+
subject[:test].should_not be_changed
|
64
|
+
end
|
65
|
+
|
66
|
+
it "converts inner hashes into Documents" do
|
67
|
+
subject = Document::Core[ { test: { inner: "test" } } ]
|
68
|
+
|
69
|
+
subject[:test].should be_a_kind_of Document::Core
|
70
|
+
end
|
71
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: document_hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Giancarlo Palavicini
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Implements a multi-level nested document, that notifies about changes,
|
31
|
+
and some other related features
|
32
|
+
email:
|
33
|
+
- kasthor@gmail.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- .rspec
|
40
|
+
- Gemfile
|
41
|
+
- LICENSE.txt
|
42
|
+
- README.md
|
43
|
+
- Rakefile
|
44
|
+
- document_hash.gemspec
|
45
|
+
- lib/.document.rb.swo
|
46
|
+
- lib/.document.rb.swp
|
47
|
+
- lib/document.rb
|
48
|
+
- lib/document_hash/.version.rb.swp
|
49
|
+
- lib/document_hash/version.rb
|
50
|
+
- spec/.spec_helper.rb.swp
|
51
|
+
- spec/lib/.document_spec.rb.swo
|
52
|
+
- spec/lib/.document_spec.rb.swp
|
53
|
+
- spec/lib/document_spec.rb
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
homepage: ''
|
56
|
+
licenses: []
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.8.24
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Document Object
|
79
|
+
test_files:
|
80
|
+
- spec/.spec_helper.rb.swp
|
81
|
+
- spec/lib/.document_spec.rb.swo
|
82
|
+
- spec/lib/.document_spec.rb.swp
|
83
|
+
- spec/lib/document_spec.rb
|
84
|
+
- spec/spec_helper.rb
|