document_mapper 0.1.6 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/document_mapper/document.rb +12 -12
- data/lib/document_mapper/version.rb +1 -1
- data/lib/document_mapper/yaml_parsing.rb +9 -1
- data/test/document_mapper/document_mapper_test.rb +30 -0
- data/test/other_documents/2011-12-26-some-test-document.textile +5 -0
- data/test/test_base.rb +4 -0
- metadata +61 -54
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1bc09066c1fbf38ca380a29e31d9d8c4f74ffb914e2b412d721bbbb5842709bf
|
4
|
+
data.tar.gz: 12de89688244203d412513377cb8ab1151bdbb2215a8674ac21be54958ee597d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c812a89c9c773906f3770e645db50deca4fd57730af0906df72e77fbe6b156465b0f8fbe43dd054608f0e9dfd54d0738e3bf6a789c366bec6c9bfa5f43f52e8e
|
7
|
+
data.tar.gz: 46718a0fe7a278b3f501eb9949d07c3133b16655356b9fed997444ef4c7f138e8089f4a767b88e36aeb980f66a5d130e12c6066670814d48d432af157635f640
|
@@ -11,7 +11,7 @@ module DocumentMapper
|
|
11
11
|
attr_accessor :attributes, :content
|
12
12
|
|
13
13
|
included do
|
14
|
-
|
14
|
+
@documents = []
|
15
15
|
end
|
16
16
|
|
17
17
|
def ==(other_document)
|
@@ -21,12 +21,12 @@ module DocumentMapper
|
|
21
21
|
|
22
22
|
module ClassMethods
|
23
23
|
def reset
|
24
|
-
|
24
|
+
@documents = []
|
25
25
|
end
|
26
26
|
|
27
27
|
def reload
|
28
28
|
self.reset
|
29
|
-
self.directory =
|
29
|
+
self.directory = @directory.path
|
30
30
|
end
|
31
31
|
|
32
32
|
def from_file(file_path)
|
@@ -38,22 +38,22 @@ module DocumentMapper
|
|
38
38
|
:file_path => File.expand_path(file_path)
|
39
39
|
}
|
40
40
|
document.read_yaml
|
41
|
-
|
41
|
+
@documents << document
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
45
|
def directory=(new_directory)
|
46
46
|
raise FileNotFoundError unless File.directory?(new_directory)
|
47
47
|
self.reset
|
48
|
-
|
49
|
-
|
48
|
+
@directory = Dir.new File.expand_path(new_directory)
|
49
|
+
@directory.each do |file|
|
50
50
|
next if file[0,1] == '.'
|
51
|
-
self.from_file [
|
51
|
+
self.from_file [@directory.path, file].join('/')
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
55
|
def select(options = {})
|
56
|
-
documents =
|
56
|
+
documents = @documents.dup
|
57
57
|
options[:where].each do |selector, selector_value|
|
58
58
|
documents = documents.select do |document|
|
59
59
|
next unless document.attributes.has_key? selector.attribute
|
@@ -95,19 +95,19 @@ module DocumentMapper
|
|
95
95
|
end
|
96
96
|
|
97
97
|
def all
|
98
|
-
|
98
|
+
@documents
|
99
99
|
end
|
100
100
|
|
101
101
|
def first
|
102
|
-
|
102
|
+
@documents.first
|
103
103
|
end
|
104
104
|
|
105
105
|
def last
|
106
|
-
|
106
|
+
@documents.last
|
107
107
|
end
|
108
108
|
|
109
109
|
def attributes
|
110
|
-
|
110
|
+
@documents.map(&:attributes).map(&:keys).flatten.uniq.sort
|
111
111
|
end
|
112
112
|
end
|
113
113
|
end
|
@@ -1,4 +1,6 @@
|
|
1
1
|
module DocumentMapper
|
2
|
+
class YamlParsingError < StandardError; end
|
3
|
+
|
2
4
|
module YamlParsing
|
3
5
|
def read_yaml
|
4
6
|
file_path = self.attributes[:file_path]
|
@@ -6,7 +8,7 @@ module DocumentMapper
|
|
6
8
|
|
7
9
|
if @content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
|
8
10
|
@content = @content[($1.size + $2.size)..-1]
|
9
|
-
self.attributes.update(
|
11
|
+
self.attributes.update(yaml_load($1, file_path).symbolize_keys)
|
10
12
|
end
|
11
13
|
|
12
14
|
file_name = File.basename(file_path)
|
@@ -35,5 +37,11 @@ module DocumentMapper
|
|
35
37
|
self.class.define_attribute_methods self.attributes.keys
|
36
38
|
self.attributes.keys.each { |attr| self.class.define_read_method attr }
|
37
39
|
end
|
40
|
+
|
41
|
+
def yaml_load(yaml, file)
|
42
|
+
YAML.load(yaml)
|
43
|
+
rescue ArgumentError, Psych::SyntaxError
|
44
|
+
raise YamlParsingError, "Unable to parse YAML of #{file}"
|
45
|
+
end
|
38
46
|
end
|
39
47
|
end
|
@@ -336,6 +336,36 @@ EOS
|
|
336
336
|
end
|
337
337
|
end
|
338
338
|
|
339
|
+
describe 'loading a document with invalid yaml' do
|
340
|
+
it 'should raise with a decent error message' do
|
341
|
+
@file_path = File.expand_path('test/documents/invalid_yaml.textile')
|
342
|
+
File.open(@file_path, 'w') do |f|
|
343
|
+
f.write <<-EOS
|
344
|
+
---
|
345
|
+
title: Look: Invalid YAML!
|
346
|
+
---
|
347
|
+
|
348
|
+
This is definitely gonna blow up.
|
349
|
+
EOS
|
350
|
+
end
|
351
|
+
proc { MyDocument.reload }.must_raise(DocumentMapper::YamlParsingError, "Unable to parse YAML of #{@file_path}")
|
352
|
+
end
|
353
|
+
|
354
|
+
def teardown
|
355
|
+
File.delete @file_path
|
356
|
+
end
|
357
|
+
end
|
358
|
+
|
359
|
+
describe 'multiple document classes' do
|
360
|
+
it 'can serve multiple document directories' do
|
361
|
+
MyDocument.directory = 'test/documents'
|
362
|
+
MyOtherDocument.directory = 'test/other_documents'
|
363
|
+
|
364
|
+
assert_equal 4, MyDocument.all.count
|
365
|
+
assert_equal 1, MyOtherDocument.all.count
|
366
|
+
end
|
367
|
+
end
|
368
|
+
|
339
369
|
def sample_file_path_1
|
340
370
|
'test/documents/2010-08-08-test-document-file.textile'
|
341
371
|
end
|
data/test/test_base.rb
CHANGED
@@ -12,6 +12,10 @@ class MyDocument
|
|
12
12
|
include DocumentMapper::Document
|
13
13
|
end
|
14
14
|
|
15
|
+
class MyOtherDocument
|
16
|
+
include DocumentMapper::Document
|
17
|
+
end
|
18
|
+
|
15
19
|
module MiniTest::Assertions
|
16
20
|
def assert_equal_set exp, act, msg = nil
|
17
21
|
msg = message(msg) { "Expected #{mu_pp(exp)}, not #{mu_pp(act)}" }
|
metadata
CHANGED
@@ -1,90 +1,104 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: document_mapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.2.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Ralph von der Heyden
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2022-07-27 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: activesupport
|
16
|
-
requirement:
|
17
|
-
none: false
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
19
|
+
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
|
-
version_requirements:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
25
27
|
- !ruby/object:Gem::Dependency
|
26
28
|
name: activemodel
|
27
|
-
requirement:
|
28
|
-
none: false
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
29
30
|
requirements:
|
30
|
-
- -
|
31
|
+
- - ">="
|
31
32
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
33
|
+
version: '0'
|
33
34
|
type: :runtime
|
34
35
|
prerelease: false
|
35
|
-
version_requirements:
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
36
41
|
- !ruby/object:Gem::Dependency
|
37
42
|
name: rake
|
38
|
-
requirement:
|
39
|
-
none: false
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
40
44
|
requirements:
|
41
|
-
- -
|
45
|
+
- - ">="
|
42
46
|
- !ruby/object:Gem::Version
|
43
|
-
version: 0
|
47
|
+
version: '0'
|
44
48
|
type: :runtime
|
45
49
|
prerelease: false
|
46
|
-
version_requirements:
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
47
55
|
- !ruby/object:Gem::Dependency
|
48
56
|
name: tilt
|
49
|
-
requirement:
|
50
|
-
none: false
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
51
58
|
requirements:
|
52
|
-
- -
|
59
|
+
- - ">="
|
53
60
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
61
|
+
version: '0'
|
55
62
|
type: :runtime
|
56
63
|
prerelease: false
|
57
|
-
version_requirements:
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
58
69
|
- !ruby/object:Gem::Dependency
|
59
70
|
name: RedCloth
|
60
|
-
requirement:
|
61
|
-
none: false
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
62
72
|
requirements:
|
63
|
-
- -
|
73
|
+
- - ">="
|
64
74
|
- !ruby/object:Gem::Version
|
65
|
-
version:
|
75
|
+
version: '0'
|
66
76
|
type: :development
|
67
77
|
prerelease: false
|
68
|
-
version_requirements:
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: minitest
|
71
|
-
requirement:
|
72
|
-
none: false
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
73
86
|
requirements:
|
74
|
-
- -
|
87
|
+
- - ">="
|
75
88
|
- !ruby/object:Gem::Version
|
76
|
-
version:
|
89
|
+
version: '0'
|
77
90
|
type: :development
|
78
91
|
prerelease: false
|
79
|
-
version_requirements:
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: " DocumentMapper is an object mapper for plain text documents. The documents
|
98
|
+
look like the ones used in jekyll (http://github.com/mojombo/jekyll). They consist
|
99
|
+
of a preambel written in YAML (also called YAML front matter), and some content
|
100
|
+
in the format you prefer, e.g. Textile. This enables you to write documents in your
|
101
|
+
favorite editor and access the content and metadata of these in your Ruby scripts.\n"
|
88
102
|
email: ralph@rvdh.de
|
89
103
|
executables: []
|
90
104
|
extensions: []
|
@@ -92,6 +106,7 @@ extra_rdoc_files: []
|
|
92
106
|
files:
|
93
107
|
- LICENSE
|
94
108
|
- README.md
|
109
|
+
- lib/document_mapper.rb
|
95
110
|
- lib/document_mapper/attribute_methods.rb
|
96
111
|
- lib/document_mapper/constants.rb
|
97
112
|
- lib/document_mapper/core_ext/object.rb
|
@@ -103,7 +118,6 @@ files:
|
|
103
118
|
- lib/document_mapper/to_html.rb
|
104
119
|
- lib/document_mapper/version.rb
|
105
120
|
- lib/document_mapper/yaml_parsing.rb
|
106
|
-
- lib/document_mapper.rb
|
107
121
|
- test/document_mapper/core_ext/symbol_test.rb
|
108
122
|
- test/document_mapper/document_mapper_test.rb
|
109
123
|
- test/document_mapper/selector_test.rb
|
@@ -111,34 +125,27 @@ files:
|
|
111
125
|
- test/documents/2010-08-09-another-test-document.textile
|
112
126
|
- test/documents/document_with_date_in_yaml.textile
|
113
127
|
- test/documents/document_without_date.textile
|
128
|
+
- test/other_documents/2011-12-26-some-test-document.textile
|
114
129
|
- test/test_base.rb
|
115
130
|
homepage: http://github.com/ralph/document_mapper
|
116
131
|
licenses: []
|
132
|
+
metadata: {}
|
117
133
|
post_install_message:
|
118
134
|
rdoc_options: []
|
119
135
|
require_paths:
|
120
136
|
- lib
|
121
137
|
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
-
none: false
|
123
138
|
requirements:
|
124
|
-
- -
|
139
|
+
- - ">="
|
125
140
|
- !ruby/object:Gem::Version
|
126
141
|
version: '0'
|
127
|
-
segments:
|
128
|
-
- 0
|
129
|
-
hash: 4508131542455826029
|
130
142
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
-
none: false
|
132
143
|
requirements:
|
133
|
-
- -
|
144
|
+
- - ">="
|
134
145
|
- !ruby/object:Gem::Version
|
135
146
|
version: '0'
|
136
|
-
segments:
|
137
|
-
- 0
|
138
|
-
hash: 4508131542455826029
|
139
147
|
requirements: []
|
140
|
-
|
141
|
-
rubygems_version: 1.8.10
|
148
|
+
rubygems_version: 3.1.6
|
142
149
|
signing_key:
|
143
150
|
specification_version: 3
|
144
151
|
summary: DocumentMapper is an object mapper for plain text documents.
|