Skip to content

Instantly share code, notes, and snippets.

View dchandekstark's full-sized avatar

David Chandek-Stark dchandekstark

  • Duke University Libraries
  • Durham, North Carolina USA
View GitHub Profile
@dchandekstark
dchandekstark / README.md
Last active August 29, 2015 14:01
UniquenessValidator for ActiveFedora

Notes

  • Modeled after ActiveRecord::Validations::UniquenessValidator
  • Only supports uniqueness for attributes, not associations.
  • Because indexing can vary, only supports one attribute at a time. Defaults to the :symbol indexer (*_ssim). Index field can be declared explicitly with :index_field option, or implicitly with :index_type and :data_type options.
  • Designed for single-valued attributes, i.e., :multiple => false. Should work for multi-valued attributes having single value; may work (perhaps not exactly as desired) for attributes having multiple values.
  • Does not support scoping or extra conditions.

Instructions

@dchandekstark
dchandekstark / preservation_event_type.rb
Created June 3, 2014 18:52
RDF Vocabulary: Preservation Event Types
require 'rdf'
class PreservationEventType < RDF::StrictVocabulary("http://id.loc.gov/vocabulary/preservation/eventType/")
property :cap, label: "capture"
property :com, label: "compression"
property :cre, label: "creation"
property :dea, label: "deaccession"
property :dec, label: "decompression"
property :der, label: "decryption"
property :del, label: "deletion"
@dchandekstark
dchandekstark / add_duke_terms_to_desc_metadata.rb
Created June 17, 2014 14:31
Add Duke terms to descMetadata
ActiveFedora::Base.find_each({}, {cast: true}) do |obj|
unless obj.datastreams["descMetadata"].is_a? DulHydra::Datastreams::DescriptiveMetadataDatastream
puts "SKIPPED #{obj.pid} -- descMetadata is not a DulHydra::Datastreams::DescriptiveMetadataDatastream"
next
end
doc = obj.descMetadata.ng_xml
if doc.namespaces.include? "xmlns:#{DulHydra::Metadata::DukeTerms::NAMESPACE_PREFIX}"
puts "SKIPPED #{obj.pid} -- already has Duke terms namespace"
next
end
2.1.2 :001 > roles = RoleAssertionsResource.new
=> #<RoleAssertionsResource:0x3fdf099b3110(default)>
2.1.2 :002 > user = UserResource.new.tap {|u| u.name = "bob"}
=> #<UserResource: @name=["bob"]>
2.1.2 :003 > user2 = UserResource.new.tap { |u| u.name = "sally" }
=> #<UserResource: @name=["sally"]>
2.1.2 :004 > roles[:owner] = user
=> #<UserResource: @name=["bob"]>
2.1.2 :005 > roles[:owner]
=> [#<UserResource: @name=["bob"]>]
describe "inheritance" do
before do
class PrincipalResource < ActiveTriples::Resource
configure type: RDF::FOAF.Agent
property :name, predicate: RDF::FOAF.name
end
class UserResource < PrincipalResource
configure type: RDF::FOAF.Person
end
class DummyResource < ActiveTriples::Resource
require 'forwardable'
module Hydra
module Validations
class EnumerableValidator < ActiveModel::EachValidator
extend Forwardable
def_delegator :@member_validator, :validate_each, :validate_member
def_delegator :@member_validator, :kind
@dchandekstark
dchandekstark / at_resource.rb
Created December 17, 2014 19:50
ActiveFedore attribute w/multiple: false setting to ActiveTriples::Resource
require "rdf-vocab"
module Ddr
module Metadata
class Identifier < ActiveTriples::Resource
PREMIS = Ddr::Vocab::PREMIS
ARK_RE = /^ark:/
DOI_RE = /^doi:/
@dchandekstark
dchandekstark / README.md
Last active August 29, 2015 14:13
Indexing API

ActiveFedora Indexing API

TL;DR: This Gist sketches out an API for providing a means of managing inheritable indexing configurations in ActiveFedora. Primary motivations are (1) to avoid (for most purposes) putting indexing logic in an overriden #to_solr method, and (2) to make it possible to introspect a model's or instance’s indexing.

An earlier version of this idea was kicked around on IRC, and I've refined it significantly since then, mainly to accommodate the current property indexing API (from ActiveTriples). Originally, I just aimed to deal with indexing calculated values (basically model methods, as opposed to attributes or properties which already support indexing), for which one currently has to override #to_solr.

Essentially, I wanted to be able to do this type of thing on a model:

# Stupid example
@dchandekstark
dchandekstark / model.rb
Created February 6, 2015 00:26
Validation
class Model
attr_accessor :title
# has-a validator
def validator
@validator ||= ModelValidator.new(self)
end
end
# Represents a validation attempt
class Validation
@dchandekstark
dchandekstark / sessions_controller.rb
Last active August 29, 2015 14:15
SessionsController - Devise + Ominauth + Shibboleth
class Users::SessionsController < Devise::SessionsController
def new
store_location_for(:user, request.referrer) # return to previous page after authn
if Ddr::Auth.require_shib_user_authn
# don't want the "sign in or sign up" flash in this case
flash.discard(:alert)
redirect_to user_omniauth_authorize_path(:shibboleth)
else
super