module ActiveFedora::Attributes::ClassMethods
def add_attribute_indexing_config(name, &block)
def add_attribute_indexing_config(name, &block) # TODO the hash can be initalized to return on of these index_config[name] ||= ActiveFedora::Indexing::Map::IndexObject.new &block end
def association_attributes
def association_attributes outgoing_reflections.values.map { |reflection| reflection.foreign_key.to_s } end
def attribute_names
def attribute_names @attribute_names ||= delegated_attributes.keys + association_attributes - system_attributes end
def create_attribute_reader(field, dsid, args)
def create_attribute_reader(field, dsid, args) find_or_create_defined_attribute(field, dsid, args) define_method field do |*opts| array_reader(field, *opts) end end
def create_attribute_setter(field, dsid, args)
def create_attribute_setter(field, dsid, args) find_or_create_defined_attribute(field, dsid, args) define_method "#{field}=".to_sym do |v| self[field]=v end end
def datastream_class_for_name(dsid)
-
(Class)
- the class of the datastream
Parameters:
-
dsid
(String
) -- the datastream id
def datastream_class_for_name(dsid) reflection = reflect_on_association(dsid) reflection ? reflection.klass : ActiveFedora::File end
def define_active_triple_accessor(name, properties, &block)
def define_active_triple_accessor(name, properties, &block) warn_duplicate_predicates name, properties properties = { multiple: true }.merge(properties) find_or_create_defined_attribute(name, nil, properties) raise ArgumentError, "#{name} is a keyword and not an acceptable property name." if protected_property_name? name reflection = ActiveFedora::Attributes::PropertyBuilder.build(self, name, properties, &block) ActiveTriples::Reflection.add_reflection self, name, reflection add_attribute_indexing_config(name, &block) if block_given? end
def define_delegated_accessor(*fields, &block)
def define_delegated_accessor(*fields, &block) options = fields.pop datastream = options.delete(:datastream).to_s raise ArgumentError, "You must provide a datastream to has_attributes" if datastream.blank? define_attribute_methods fields fields.each do |f| create_attribute_reader(f, datastream, options) create_attribute_setter(f, datastream, options) add_attribute_indexing_config(f, &block) if block_given? end end
def defined_attributes
def defined_attributes Deprecation.warn Attributes, "defined_attributes has been renamed to delegated_attributes. defined_attributes will be removed in ActiveFedora 9" delegated_attributes end
def delegated_attributes
def delegated_attributes @delegated_attributes ||= {}.with_indifferent_access return @delegated_attributes unless superclass.respond_to?(:delegated_attributes) and value = superclass.delegated_attributes @delegated_attributes = value.dup if @delegated_attributes.empty? @delegated_attributes end
def delegated_attributes= val
def delegated_attributes= val @delegated_attributes = val end
def find_or_create_defined_attribute(field, dsid, args)
def find_or_create_defined_attribute(field, dsid, args) delegated_attributes[field] ||= DelegatedAttribute.new(field, dsid, datastream_class_for_name(dsid), args) end
def has_attributes(*fields, &block)
def has_attributes(*fields, &block) Deprecation.warn(Attributes, "has_attributes is deprecated and will be removed in ActiveFedora 10.0. Use property instead") define_delegated_accessor(*fields, &block) end
def local_attributes
def local_attributes association_attributes + properties.keys - system_attributes end
def multiple?(field)
-
(Boolean)
-
Parameters:
-
field
(Symbol
) -- the field to query
def multiple?(field) raise UnknownAttributeError, "#{self} does not have an attribute `#{field}'" unless delegated_attributes.key?(field) delegated_attributes[field].multiple end
def property name, properties={}, &block
def property name, properties={}, &block if properties.key?(:predicate) define_active_triple_accessor(name, properties, &block) elsif properties.key?(:datastream) define_delegated_accessor(name, properties.merge(multiple: true), &block) else raise "You must provide `:datastream' or `:predicate' options to property" end end
def system_attributes
def system_attributes ['has_model', 'create_date', 'modified_date'] end
def unique?(field)
-
(Boolean)
-
Parameters:
-
field
(Symbol
) -- the field to query
def unique?(field) !multiple?(field) end
def warn_duplicate_predicates new_name, new_properties
def warn_duplicate_predicates new_name, new_properties new_predicate = new_properties[:predicate] self.properties.select{|k, existing| existing.predicate == new_predicate}.each do |key, value| ActiveFedora::Base.logger.warn "Same predicate (#{new_predicate}) used for properties #{key} and #{new_name}" end end