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 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
- @@documents = []
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
- @@documents = []
24
+ @documents = []
25
25
  end
26
26
 
27
27
  def reload
28
28
  self.reset
29
- self.directory = @@directory.path
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
- @@documents << document
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
- @@directory = Dir.new File.expand_path(new_directory)
49
- @@directory.each do |file|
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 [@@directory.path, file].join('/')
51
+ self.from_file [@directory.path, file].join('/')
52
52
  end
53
53
  end
54
54
 
55
55
  def select(options = {})
56
- documents = @@documents.dup
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
- @@documents
98
+ @documents
99
99
  end
100
100
 
101
101
  def first
102
- @@documents.first
102
+ @documents.first
103
103
  end
104
104
 
105
105
  def last
106
- @@documents.last
106
+ @documents.last
107
107
  end
108
108
 
109
109
  def attributes
110
- @@documents.map(&:attributes).map(&:keys).flatten.uniq.sort
110
+ @documents.map(&:attributes).map(&:keys).flatten.uniq.sort
111
111
  end
112
112
  end
113
113
  end
@@ -1,3 +1,3 @@
1
1
  module DocumentMapper
2
- VERSION = '0.1.6'
2
+ VERSION = '0.2.0'
3
3
  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(YAML.load($1).symbolize_keys)
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
@@ -0,0 +1,5 @@
1
+ ---
2
+ title: Some test document
3
+ ---
4
+
5
+ This is just some test document.
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.1.6
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: 2011-09-14 00:00:00.000000000Z
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: &70138859185020 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - ~>
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
- version: 3.1.0
19
+ version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
- version_requirements: *70138859185020
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: &70138859184180 !ruby/object:Gem::Requirement
28
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
- - - ~>
31
+ - - ">="
31
32
  - !ruby/object:Gem::Version
32
- version: 3.1.0
33
+ version: '0'
33
34
  type: :runtime
34
35
  prerelease: false
35
- version_requirements: *70138859184180
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: &70138859183360 !ruby/object:Gem::Requirement
39
- none: false
43
+ requirement: !ruby/object:Gem::Requirement
40
44
  requirements:
41
- - - ~>
45
+ - - ">="
42
46
  - !ruby/object:Gem::Version
43
- version: 0.9.0
47
+ version: '0'
44
48
  type: :runtime
45
49
  prerelease: false
46
- version_requirements: *70138859183360
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: &70138859182700 !ruby/object:Gem::Requirement
50
- none: false
57
+ requirement: !ruby/object:Gem::Requirement
51
58
  requirements:
52
- - - ~>
59
+ - - ">="
53
60
  - !ruby/object:Gem::Version
54
- version: 1.3.0
61
+ version: '0'
55
62
  type: :runtime
56
63
  prerelease: false
57
- version_requirements: *70138859182700
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: &70138859182100 !ruby/object:Gem::Requirement
61
- none: false
71
+ requirement: !ruby/object:Gem::Requirement
62
72
  requirements:
63
- - - ~>
73
+ - - ">="
64
74
  - !ruby/object:Gem::Version
65
- version: 4.2.0
75
+ version: '0'
66
76
  type: :development
67
77
  prerelease: false
68
- version_requirements: *70138859182100
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: &70138859181500 !ruby/object:Gem::Requirement
72
- none: false
85
+ requirement: !ruby/object:Gem::Requirement
73
86
  requirements:
74
- - - ~>
87
+ - - ">="
75
88
  - !ruby/object:Gem::Version
76
- version: 2.6.0
89
+ version: '0'
77
90
  type: :development
78
91
  prerelease: false
79
- version_requirements: *70138859181500
80
- description: ! ' DocumentMapper is an object mapper for plain text documents. The
81
- documents look like the ones used in jekyll (http://github.com/mojombo/jekyll).
82
- They consist of a preambel written in YAML (also called YAML front matter), and
83
- some content in the format you prefer, e.g. Textile. This enables you to write documents
84
- in your favorite editor and access the content and metadata of these in your Ruby
85
- scripts.
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
- rubyforge_project:
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.