document_file 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
data/lib/document_file/base.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'yaml'
|
2
|
+
require 'active_support/inflector'
|
2
3
|
|
3
4
|
module DocumentFile
|
4
5
|
class Base
|
@@ -59,44 +60,75 @@ module DocumentFile
|
|
59
60
|
end
|
60
61
|
|
61
62
|
def define_dynamic_methods
|
62
|
-
@data.each do |
|
63
|
-
|
64
|
-
instance_eval "
|
63
|
+
@data.each do |attribute_name, value|
|
64
|
+
instance_variable_set("@#{attribute_name}", value)
|
65
|
+
self.class.instance_eval "attr_reader :#{attribute_name}"
|
65
66
|
|
66
67
|
if value.is_a? Array
|
67
|
-
|
68
|
-
|
69
|
-
documents = self.all
|
70
|
-
#{method_name} = {}
|
71
|
-
documents.each do |document|
|
72
|
-
document.#{method_name}.each do |single_item|
|
73
|
-
if #{method_name}.has_key? single_item
|
74
|
-
#{method_name}[single_item] << document
|
75
|
-
else
|
76
|
-
#{method_name}[single_item] = [document]
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
#{method_name}
|
81
|
-
end
|
82
|
-
eos
|
83
|
-
self.class.send(:module_eval, by_attribute_method)
|
68
|
+
define_by_attribute_finder(attribute_name)
|
69
|
+
define_find_all_by_attribute_finder(attribute_name)
|
84
70
|
end
|
85
71
|
|
86
|
-
define_attribute_finder(
|
72
|
+
define_attribute_finder(attribute_name)
|
87
73
|
end
|
88
74
|
@@dynamic_methods_defined = true
|
89
75
|
end
|
90
76
|
|
91
|
-
|
77
|
+
# Defines an attribute finder, e.g.
|
78
|
+
# MyDocument.find_by_title('some_title') => some_document
|
79
|
+
def define_attribute_finder(attribute_name)
|
92
80
|
find_by_attribute_method = <<-eos
|
93
|
-
def self.find_by_#{
|
94
|
-
all.detect
|
81
|
+
def self.find_by_#{attribute_name}(attribute)
|
82
|
+
all.detect do |document|
|
83
|
+
if document.respond_to?('#{attribute_name}')
|
84
|
+
document.#{attribute_name} == attribute
|
85
|
+
else
|
86
|
+
false
|
87
|
+
end
|
88
|
+
end
|
95
89
|
end
|
96
90
|
eos
|
97
91
|
self.class.send(:module_eval, find_by_attribute_method)
|
98
92
|
end
|
99
93
|
|
94
|
+
# Defines a by attribute finder for Array attributes, e.g.
|
95
|
+
# MyDocument.by_tags
|
96
|
+
# => {
|
97
|
+
# "tag_1" => [document_1, document_3],
|
98
|
+
# "tag_2" => [document_2]
|
99
|
+
# }
|
100
|
+
def define_by_attribute_finder(array_attribute)
|
101
|
+
by_attribute_method = <<-eos
|
102
|
+
def self.by_#{array_attribute}
|
103
|
+
documents = self.all
|
104
|
+
#{array_attribute}_items = {}
|
105
|
+
documents.each do |document|
|
106
|
+
document.#{array_attribute}.each do |single_item|
|
107
|
+
if #{array_attribute}_items.has_key? single_item
|
108
|
+
#{array_attribute}_items[single_item] << document
|
109
|
+
else
|
110
|
+
#{array_attribute}_items[single_item] = [document]
|
111
|
+
end
|
112
|
+
end if document.#{array_attribute}
|
113
|
+
end
|
114
|
+
#{array_attribute}_items
|
115
|
+
end
|
116
|
+
eos
|
117
|
+
self.class.send(:module_eval, by_attribute_method)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Finds documents by a specific Array attribute value , e.g.
|
121
|
+
# MyDocument.find_all_by_tag('my_tag') => [document_1, document_2, ...]
|
122
|
+
def define_find_all_by_attribute_finder(array_attribute)
|
123
|
+
singular_array_attribute = ActiveSupport::Inflector.singularize array_attribute
|
124
|
+
find_all_by_attribute_method = <<-eos
|
125
|
+
def self.find_all_by_#{singular_array_attribute}(singular_array_attribute)
|
126
|
+
self.by_#{array_attribute}[singular_array_attribute]
|
127
|
+
end
|
128
|
+
eos
|
129
|
+
self.class.send(:module_eval, find_all_by_attribute_method)
|
130
|
+
end
|
131
|
+
|
100
132
|
def self.method_missing(method_name, *args)
|
101
133
|
self.all unless @@documents
|
102
134
|
respond_to?(method_name) ? self.send(method_name, *args) : super
|
data/test/document_file_test.rb
CHANGED
@@ -56,7 +56,7 @@ describe MyDocument do
|
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
-
describe 'when listing document_files by an
|
59
|
+
describe 'when listing document_files by an Array attribute' do
|
60
60
|
it 'should return a Hash' do
|
61
61
|
assert_equal Hash, MyDocument.by_tags.class
|
62
62
|
end
|
@@ -70,6 +70,29 @@ describe MyDocument do
|
|
70
70
|
assert_equal Set.new([1, 2]), document_files['tag'].map(&:id).to_set
|
71
71
|
assert_equal Set.new([2]), document_files['tug'].map(&:id).to_set
|
72
72
|
end
|
73
|
+
|
74
|
+
it 'should not be confused by attributes that only some documents have' do
|
75
|
+
document_files_by_authors = MyDocument.by_authors
|
76
|
+
assert_equal 1, document_files_by_authors['Frank'].first.id
|
77
|
+
|
78
|
+
document_files_by_friends = MyDocument.by_friends
|
79
|
+
assert_equal 2, document_files_by_friends['Anton'].first.id
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe 'when finding document_files by an Array attribute value' do
|
84
|
+
it 'should return an Array' do
|
85
|
+
assert_equal Array, MyDocument.find_all_by_tag('tag').class
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'should containt documents' do
|
89
|
+
assert_equal MyDocument, MyDocument.find_all_by_tag('tag').first.class
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'should return the right documents' do
|
93
|
+
assert_equal [1, 2], MyDocument.find_all_by_tag('tag').map(&:id)
|
94
|
+
assert_equal [2], MyDocument.find_all_by_tag('tug').map(&:id)
|
95
|
+
end
|
73
96
|
end
|
74
97
|
|
75
98
|
describe 'when finding a document_file' do
|
@@ -84,6 +107,11 @@ describe MyDocument do
|
|
84
107
|
document_file = MyDocument.find_by_file_name file_name
|
85
108
|
assert_equal document_file.file_name, file_name
|
86
109
|
end
|
110
|
+
|
111
|
+
it 'should not be confused by attributes that only some dcuments have' do
|
112
|
+
document_file = MyDocument.find_by_special_attribute 'Yes!'
|
113
|
+
assert_equal 'Yes!', document_file.special_attribute
|
114
|
+
end
|
87
115
|
end
|
88
116
|
|
89
117
|
describe 'when getting the file name or file path' do
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ralph von der Heyden
|
@@ -14,10 +14,24 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-08-
|
17
|
+
date: 2010-08-23 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
|
-
dependencies:
|
20
|
-
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: activesupport
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 3
|
30
|
+
- 0
|
31
|
+
- 0
|
32
|
+
version: 3.0.0
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
21
35
|
description: " Makes your plain text files accessible in Ruby. Supports YAML front matter.\n"
|
22
36
|
email: ralph@rvdh.de
|
23
37
|
executables: []
|