module ActiveFedora::LoadableFromJson

Public Instance Methods

init_with_json(json) { |self| ... } click to toggle source

@param json [String] json to be parsed into attributes @yield [self] Yields self after attributes from json have been assigned

but before callbacks and before the object is frozen.
# File lib/active_fedora/loadable_from_json.rb, line 121
def init_with_json(json)
  attrs = JSON.parse(json)
  id = attrs.delete('id')

  @ldp_source = build_ldp_resource(id)
  @association_cache = {}
  datastream_keys = self.class.child_resource_reflections.keys
  datastream_keys.each do |key|
    attached_files[key] = SolrBackedMetadataFile.new
  end
  @resource = SolrBackedResource.new(self.class)
  self.attributes = adapt_attributes(attrs)
  # TODO: Should we clear the change tracking, or make this object Read-only?
  # See https://github.com/samvera/active_fedora/issues/1342

  yield self if block_given?

  run_callbacks :find
  run_callbacks :initialize
  freeze
  self
end

Private Instance Methods

adapt_attribute_value(attrs, attribute_name) click to toggle source

Adapts a single attribute from the given attributes hash to fit the data model. @param attrs [Hash] attributes read from Solr @param attribute_name [String] the name of the attribute to adapt @return [Object] the adapted value

# File lib/active_fedora/loadable_from_json.rb, line 162
def adapt_attribute_value(attrs, attribute_name)
  reflection = property_reflection(attribute_name)
  # if this isn't a property, copy value verbatim
  return attrs[attribute_name] unless reflection
  multiple = reflection.multiple?
  # if value is missing in attrs, return [] or nil as appropriate
  return multiple ? [] : nil unless attrs.key?(attribute_name)

  if multiple
    Array(attrs[attribute_name]).map do |value|
      adapt_single_attribute_value(value, attribute_name)
    end
  else
    adapt_single_attribute_value(attrs[attribute_name], attribute_name)
  end
end
adapt_attributes(attrs) click to toggle source

Adapt attributes read from Solr to fit the data model. @param attrs [Hash] attributes read from Solr @return [Hash] the adapted attributes

# File lib/active_fedora/loadable_from_json.rb, line 149
def adapt_attributes(attrs)
  result = {}
  self.class.attribute_names.each do |attribute_name|
    result[attribute_name] = adapt_attribute_value(attrs, attribute_name)
  end
  result
end
adapt_single_attribute_value(value, attribute_name) click to toggle source

Adapts a single attribute value to fit the data model. If the attribute is multi-valued, each value is passed separately to this method. @param value [Object] attribute value read from Solr @param attribute_name [String] the name of the attribute to adapt @return [Object] the adapted value

# File lib/active_fedora/loadable_from_json.rb, line 197
def adapt_single_attribute_value(value, attribute_name)
  if value && date_attribute?(attribute_name)
    return nil if value.blank?
    DateTime.parse(value)
  else
    value
  end
end
date_attribute?(attribute_name) click to toggle source
# File lib/active_fedora/loadable_from_json.rb, line 186
def date_attribute?(attribute_name)
  reflection = self.class.reflect_on_property(attribute_name)
  return false unless reflection
  reflection.type == :date || reflection.class_name == DateTime
end
property_reflection(attribute_name) click to toggle source
# File lib/active_fedora/loadable_from_json.rb, line 179
def property_reflection(attribute_name)
  self.class.reflect_on_property(attribute_name)
rescue ActiveTriples::UndefinedPropertyError
  ActiveFedora::Base.logger.info "Undefined property #{attribute_name} reflected."
  nil
end