Module: TaliaCore::ActiveSourceParts::Xml::GenericReaderAddStatements
- Included in:
- GenericReader
- Defined in:
- lib/talia_core/active_source_parts/xml/generic_reader_add_statements.rb
Overview
These are statements that can be used in handlers (see GenericReaderImportStatements to learn about handlers). They will add data to the source that is currently being imported.
Instance Method Summary collapse
-
#add(predicate, object, required = false) ⇒ Object
Adds a value for the given predicate (may also be a database field).
-
#add_date(predicate, date, required = false, fmt = nil) ⇒ Object
Adds a date field.
-
#add_date_interval(predicate, start_date, end_date, fmt = nil) ⇒ Object
Adds a date interval as an ISO 8061 compliant date string.
-
#add_file(urls, options = {}) ⇒ Object
Add a file to the source being imported.
-
#add_i18n(predicate, object, lang, type = nil) ⇒ Object
Works as #add, but also encodes the language (and potentially the type of the literal) into the value.
-
#add_rel(predicate, object, required = false) ⇒ Object
Adds a relation for the given predicate.
Instance Method Details
#add(predicate, object, required = false) ⇒ Object
Adds a value for the given predicate (may also be a database field). Example:
add :uri, "http://foobar.org" # Set the uri of the current source to http://foobar.org
add 'dct:creator', ['John Doe', 'Jane Doe'] # Sets the dct:creator property
To add relations between source, see #add_rel
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/talia_core/active_source_parts/xml/generic_reader_add_statements.rb', line 16 def add(predicate, object, required = false) # We need to check if the object elements are already strings - # otherwise we would *.to_s the PropertyString objects, which would # destroy the metadata in them. if(object.kind_of?(Array)) object.each { |obj| set_element(predicate, obj.is_a?(String) ? obj : obj.to_s, required) } else set_element(predicate, object.is_a?(String) ? object : object.to_s, required) end end |
#add_date(predicate, date, required = false, fmt = nil) ⇒ Object
Adds a date field. This will attempt to parse the original string and write the result as an ISO 8061 compliant date string. Note that this won’t be able to parse everything you throw at it, though.
37 38 39 |
# File 'lib/talia_core/active_source_parts/xml/generic_reader_add_statements.rb', line 37 def add_date(predicate, date, required = false, fmt = nil) add(predicate, to_iso8601(parse_date(date, fmt)), required) end |
#add_date_interval(predicate, start_date, end_date, fmt = nil) ⇒ Object
Adds a date interval as an ISO 8061 compliant date string. See add_date for more info. If only one of the dates is given this will add a normal date string instead of an interval.
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/talia_core/active_source_parts/xml/generic_reader_add_statements.rb', line 44 def add_date_interval(predicate, start_date, end_date, fmt = nil) return if(start_date.blank? && end_date.blank?) if(start_date.blank?) add_date(predicate, start_date, true, fmt) elsif(end_date.blank?) add_date(predicate, end_date, true, fmt) else add(predicate, "#{to_iso8601(parse_date(start_date, fmt))}/#{to_iso8601(parse_date(end_date, fmt))}", required) end end |
#add_file(urls, options = {}) ⇒ Object
Add a file to the source being imported. See the DataLoader module for a description of the possible options.
Note that the import reader will not be able to resolve URLs or file names that are relative to the original XML file or URL. File names should be absolute (otherwise they’ll be treated as relative to the current Talia working directory), as should be URLs. Furthermore, the file names/paths must be valid on the machine _where the import takes place_.
84 85 86 87 88 89 |
# File 'lib/talia_core/active_source_parts/xml/generic_reader_add_statements.rb', line 84 def add_file(urls, = {}) return if(urls.blank?) urls = [ urls ] unless(urls.is_a?(Array)) files = urls.collect { |url| { :url => get_absolute_file_url(url), :options => } } @current.attributes[:files] = files if(files.size > 0) end |
#add_i18n(predicate, object, lang, type = nil) ⇒ Object
Works as #add, but also encodes the language (and potentially the type of the literal) into the value.
29 30 31 32 |
# File 'lib/talia_core/active_source_parts/xml/generic_reader_add_statements.rb', line 29 def add_i18n(predicate, object, lang, type=nil) object = object.blank? ? nil : TaliaCore::PropertyString.new(object, lang, type) add(predicate, object) end |
#add_rel(predicate, object, required = false) ⇒ Object
Adds a relation for the given predicate. This works as #add, but with the difference that it takes an object uri instead of a literal value. (See the #add method to add literal values):
add_rel 'dct:create', 'local:John', 'local:Jane'
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/talia_core/active_source_parts/xml/generic_reader_add_statements.rb', line 60 def add_rel(predicate, object, required = false) object = check_objects(object) if(!object) raise(ArgumentError, "Relation with empty object on #{predicate} (#{@current.attributes['uri']}).") if(required) return end if(object.kind_of?(Array)) object.each do |obj| raise(ArgumentError, "Cannot add relation on database field <#{predicate}> - <#{object.inspect}>") if(ActiveSource.db_attr?(predicate)) set_element(predicate, "<#{irify(obj)}>", required) end else raise(ArgumentError, "Cannot add relation on database field") if(ActiveSource.db_attr?(predicate)) set_element(predicate, "<#{irify(object)}>", required) end end |