-
Notifications
You must be signed in to change notification settings - Fork 6
Modifiers
Modifiers are methods that provide an easy way to accomplish common tasks to manipulate or retrieve data in specific ways.
The modifiers below can be used in the attribute definition blocks.
There are two ways to extract data in order to manipulate it for a specific attribute. Both ways then allow to chain other modifiers to actually make the changes to the data.
The get method retrieves the value of a attribute that has already been processed. (The attributes are always processed from top to bottom).
attribute :subjects, xpath: "//subject"
attribute :something_else do
get(:subjects)
endThe fetch method is provided to have access to the the raw data.
attribute :creator do
fetch("//author")
endattribute :creator do
fetch("author")
endThe following set of methods allow the operator to inspect the values of a particular field. These methods are specially usefull when the operator needs to acomplish conditional
Checks if there is any value at all. Returns true or false.
attribute :category, xpath: "//category" do
category = get(:category)
category += ["Images"] if get(:thumbnail_url).present?
category
endChecks if the value provided is included in the set.
attribute :category, xpath: "//category" do
category = get(:category)
category += ["News"] if get(:dc_type).includes?("News")
category
endConverts the AttributeValue object to a regular array.
attribute :category, xpath: "//category" do
get(:category).to_a
endIt returns the first value
attribute :category, xpath: "//category" do
get(:category).first
endIt returns the first element that matches the regular expression.
attribute :identifier do
get(:identifier).find_with(/IE:\w/)
endIt returns all the elements that matches the regular expression.
attribute :identifier do
get(:identifier).find_all_with(/IE:\w/)
endIt returns the first element that doesn't match the regular expression.
attribute :identifier do
get(:identifier).find_without(/http/)
endIt returns all the elements that don't match the regular expression.
attribute :identifier do
get(:identifier).find_all_without(/http/)
endIt replaces all the values with the regular expression specified
attribute :enrichment_url do
get(:identifier).mapping(/.*handle.net(.*)/ => 'https://researchspace.auckland.ac.nz/handle\\1?show=full')
endThe mapping key value pairs can also be repeated to perform multiple substitutions on the same value. For exmaple:
attribute :enrichment_url do
get(:identifier).mapping(/width=[\d]{1,4}/ => 'width=520', /height=[\d]{1,4}/ => 'height=310')
endTo use captured values from the regular expression use \1 for the first captured value \2 for the second on so on.
It allows you to select any element or range of elements within an array
attribute :creator do
get(:creator).select(:first)
endattribute :creator do
get(:creator).select(:last)
endattribute :creator do
get(:creator).select(2, :last)
endattribute :creator do
get(:creator).select(:first, -2)
endIt appends a value to a attribute.
attribute :category, default: ["Newspapers"] do
get(:category).add("Images") if get(:thumbnail_url).present?
endIt joins multiple values from potentially different places.
attribute :subjects do
compose(get(:tag), "New Zealand", get(:title))
endIt tries to parse the values into real dates.
attribute :date do
get(:display_date).to_date
endIt will try and split all values by the value specified.
attribute :subject do
get(:subject).split(",")
endThe above code will convert a string in the following format "dogs, cats, puma" to ["dogs", " cats", " puma"]
attribute :subject do
get(:subject).split(/\d/)
endThe splitter can also split based on a regular expression.
It will truncate all values to the specified length. The omission defaults to three dots "...", but a different string can be specified.
attribute :description do
get(:description).truncate(300)
endIt will truncate the string to 300 charachters.
attribute :description do
get(:description).truncate(10, "")
endIt will truncate the string to 10 characters and it won't add anything at the end of the string.
It will downcase all the values
attribute :identifier do
get(:identifier).downcase
end