lib/ezid/metadata.rb in ezid-client-0.10.0 vs lib/ezid/metadata.rb in ezid-client-0.11.0
- old
+ new
@@ -35,11 +35,11 @@
# A line ending
LINE_ENDING_RE = /\r?\n/
# A metadata element
- Element = Struct.new(:name, :writer)
+ Element = Struct.new(:name, :reader, :writer)
# Metadata profiles
PROFILES = {
"dc" => %w( creator title publisher date type ).freeze,
"datacite" => %w( creator title publisher publicationyear resourcetype ).freeze,
@@ -76,17 +76,25 @@
register_reserved_elements
end
def self.register_element(accessor, opts={})
if element = registered_elements[accessor.to_sym]
- raise Error, "Element \"#{element.name}\" already registered under key :#{accessor}"
+ raise Error, "Element \"#{element.name}\" is registered under the accessor :#{accessor}."
end
- writer = opts.fetch(:writer, true)
- name = opts.fetch(:name, accessor.to_s)
- registered_elements[accessor.to_sym] = Element.new(name, writer).freeze
+ element = Element.new(opts.fetch(:name, accessor.to_s))
+ element.reader = define_reader(accessor, element.name)
+ element.writer = define_writer(accessor, element.name) if opts.fetch(:writer, true)
+ registered_elements[accessor.to_sym] = element
end
+ def self.unregister_element(accessor)
+ element = registered_elements.delete(accessor)
+ raise Error, "No element is registered under the accessor :#{accessor}." unless element
+ remove_method(element.reader)
+ remove_method(element.writer) if element.writer
+ end
+
def self.register_profile_element(profile, element)
register_element("#{profile}_#{element}", name: "#{profile}.#{element}")
end
def self.register_profile_elements(profile = nil)
@@ -105,13 +113,28 @@
accessor = (element == "_crossref") ? element : element.sub("_", "")
register_element(accessor, name: element, writer: RESERVED_READWRITE_ELEMENTS.include?(element))
end
end
+ def self.define_reader(accessor, element)
+ define_method(accessor) do
+ reader(element)
+ end
+ end
+
+ def self.define_writer(accessor, element)
+ define_method("#{accessor}=") do |value|
+ writer(element, value)
+ end
+ end
+
private_class_method :register_elements,
:register_reserved_elements,
- :register_profile_elements
+ :register_profile_elements,
+ :unregister_element,
+ :define_reader,
+ :define_writer
def initialize(data={})
super(coerce(data))
end
@@ -130,45 +153,18 @@
def registered_elements
self.class.registered_elements
end
- protected
-
- def method_missing(method, *args)
- return registered_reader(method) if registered_reader?(method, *args)
- return registered_writer(method, *args) if registered_writer?(method, *args)
- super
- end
-
private
- def registered_reader?(accessor, *args)
- args.empty? && registered_elements.include?(accessor)
- end
-
- def registered_reader(accessor)
- reader registered_elements[accessor].name
- end
-
def reader(element)
value = self[element]
if RESERVED_TIME_ELEMENTS.include?(element)
time = value.to_i
value = (time == 0) ? nil : Time.at(time).utc
end
value
- end
-
- def registered_writer?(method, *args)
- return false unless method.to_s.end_with?("=") && args.size == 1
- accessor = method.to_s.sub("=", "").to_sym
- registered_elements.include?(accessor) && registered_elements[accessor].writer
- end
-
- def registered_writer(method, *args)
- accessor = method.to_s.sub("=", "").to_sym
- writer(registered_elements[accessor].name, *args)
end
def writer(element, value)
self[element] = value
end