tastyhat-cul-fedora-arm 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/cul-fedora-arm.gemspec +1 -1
- data/lib/cul/fedora/arm/builder.rb +47 -6
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/cul-fedora-arm.gemspec
CHANGED
@@ -1,20 +1,44 @@
|
|
1
|
+
|
2
|
+
|
1
3
|
module Cul
|
2
4
|
module Fedora
|
3
5
|
module Arm
|
6
|
+
|
7
|
+
# This class is for building ARM resource models in Fedora
|
8
|
+
#
|
9
|
+
# Authors:: James Stuart (mailto:james.stuart at columbia.edu), Benjamin Armintor (mailto: ba2213 at columbia.edu)
|
10
|
+
# License::
|
11
|
+
# Dependencies:: activesupport
|
12
|
+
|
4
13
|
class Builder
|
14
|
+
|
15
|
+
# TODO: abstract this to allow for multiple patterns
|
16
|
+
|
17
|
+
# list of columns for a template with no header row
|
5
18
|
DEFAULT_TEMPLATE_HEADER = [:sequence, :aggregate_under, :metadata, :metadata_type, :content, :content_type, :id, :license]
|
6
19
|
|
20
|
+
# columns that must have values
|
7
21
|
REQUIRED_COLUMNS = [:sequence]
|
22
|
+
|
23
|
+
# columns that must be included in a template
|
8
24
|
MANDATORY_COLUMNS = [:sequence, :aggregate_under, :metadata]
|
25
|
+
|
26
|
+
# list of columns which may have values
|
9
27
|
VALID_COLUMNS = [:sequence, :aggregate_under, :metadata, :metadata_type, :content, :content_type, :id, :license]
|
10
28
|
|
29
|
+
|
30
|
+
# array of individual hash: each hash corresponds to a metadata or resource.
|
11
31
|
attr_reader :parts
|
12
32
|
|
33
|
+
|
34
|
+
# creates a Builder object. Can be used with no arguments, or with ONE of the following options
|
35
|
+
# [:template]:: builds parts based on an enumerable list of strings (for example, the result of File.open)
|
13
36
|
def initialize(*args)
|
14
37
|
options = args.extract_options!
|
15
38
|
|
16
39
|
@parts = []
|
17
40
|
|
41
|
+
# TODO: add file option to avoid :template => File.open(file_name,"r")
|
18
42
|
|
19
43
|
if template = options.delete(:template)
|
20
44
|
parse_template(template)
|
@@ -23,7 +47,8 @@ module Cul
|
|
23
47
|
raise ArgumentError, "arguments should only include one of the following: :template" unless options.empty?
|
24
48
|
end
|
25
49
|
|
26
|
-
|
50
|
+
# adds one part to the parts array
|
51
|
+
# arguments are a hash array of column name (underscored symbols) to values
|
27
52
|
def add_part(*args)
|
28
53
|
value_hash = args.extract_options!
|
29
54
|
|
@@ -34,29 +59,41 @@ module Cul
|
|
34
59
|
|
35
60
|
@parts << value_hash
|
36
61
|
end
|
37
|
-
|
62
|
+
|
63
|
+
# looks for a part by :sequence key
|
64
|
+
# note: if loading from a template, sequence will not be an integer, but rather a string
|
38
65
|
def part_by_sequence(sequence_id)
|
39
66
|
@parts.detect { |p| p[:sequence] == sequence_id}
|
40
67
|
end
|
41
68
|
|
42
69
|
protected
|
43
70
|
|
71
|
+
|
72
|
+
# checks keys of a hash to make sure all elements of REQUIRED_COLUMNS are contained.
|
44
73
|
def test_for_required_columns(value_hash)
|
45
74
|
missing_values = REQUIRED_COLUMNS.select { |col| !value_hash.has_key?(col) || value_hash[col].nil? }
|
46
75
|
raise "Missing required values #{missing_values.join(",")}" unless missing_values.empty?
|
47
76
|
end
|
48
77
|
|
78
|
+
# checks list of column names to make sure every one is in VALID_COLUMNS
|
49
79
|
def test_for_invalid_columns(columns)
|
50
80
|
invalid_columns = columns.select { |c| !VALID_COLUMNS.include?(c) }
|
51
81
|
raise "Invalid column(s) found: #{invalid_columns.join(",")}" unless invalid_columns.empty?
|
52
82
|
end
|
53
83
|
|
84
|
+
# parses an enumerable of strings to build parts
|
85
|
+
# an optional header row specifies the columns, however sequence must be included and must be first
|
54
86
|
def parse_template(template)
|
55
|
-
header_columns = []
|
56
|
-
custom_header = false
|
87
|
+
header_columns = [] # list of columns
|
88
|
+
custom_header = false # indicates whether a header_row was used
|
89
|
+
|
90
|
+
|
57
91
|
template.each_with_index do |line, i|
|
58
|
-
|
92
|
+
|
93
|
+
# if first row, check for header
|
59
94
|
if i == 0
|
95
|
+
|
96
|
+
# assumes that if the first character is a letter, this is a header row, otherwise, use default_columns
|
60
97
|
case line.to_s
|
61
98
|
when /^[0-9]/
|
62
99
|
header_columns = DEFAULT_TEMPLATE_HEADER
|
@@ -67,19 +104,23 @@ module Cul
|
|
67
104
|
end
|
68
105
|
|
69
106
|
|
107
|
+
# check to make sure all mandatory columns are in the template
|
70
108
|
missing_mandatory_columns = MANDATORY_COLUMNS.select { |c| !header_columns.include?(c) }
|
71
109
|
raise "Missing mandatory column(s) found: #{missing_mandatory_columns.join(",")}" unless missing_mandatory_columns.empty?
|
72
110
|
|
73
111
|
test_for_invalid_columns(header_columns)
|
74
112
|
|
113
|
+
# skip header_row
|
75
114
|
next if custom_header
|
76
115
|
|
77
116
|
end
|
117
|
+
|
118
|
+
# create hash out of columns and whitespace-stripped values
|
78
119
|
value_hash = Hash[*header_columns.zip(line.split("\t").collect(&:strip)).flatten]
|
79
120
|
add_part(value_hash)
|
80
121
|
end
|
81
122
|
|
82
|
-
|
123
|
+
parts
|
83
124
|
end
|
84
125
|
|
85
126
|
|