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 / clean-resource.rb
Created August 3, 2021 14:05
Ruby script to strip out content from OKD resources for helm
#!/usr/bin/env ruby
require 'yaml'
data = YAML.load(STDIN.read)
kind = data['kind']
# status
if kind == 'Route'
<foxml:datastream ID="RELS-EXT" STATE="A" CONTROL_GROUP="X" VERSIONABLE="true">
<foxml:datastreamVersion ID="RELS-EXT.0" LABEL="Fedora Object-to-Object Relationship Metadata" CREATED="2014-01-27T19:33:31.713Z" MIMETYPE="application/rdf+xml" SIZE="411">
<foxml:contentDigest TYPE="SHA-256" DIGEST="dc9dd47194e9e6796ca727b232d1b597afde15c430b0777c2a4e1defa5f4f3ea"/>
<foxml:xmlContent>
<rdf:RDF xmlns:ns0="http://projecthydra.org/ns/relations#" xmlns:ns1="info:fedora/fedora-system:def/model#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="info:fedora/duke:1556">
<ns0:isGovernedBy rdf:resource="info:fedora/duke:1555"/>
<ns1:hasModel rdf:resource="info:fedora/afmodel:Collection"/>
</rdf:Description>
</rdf:RDF>
scope :in_collection, ->(collection) {
where(Ddr::Index::Fields::IS_MEMBER_OF_COLLECTION => collection.respond_to?(:id) ? collection.id : collection)
}
scope :having_local_id, ->(local_id) {
where(Ddr::Index::Fields::LOCAL_ID => local_id)
}
def self.having_local_id!(local_id)
results = having_local_id(local_id).to_a
if results.size > 1
@dchandekstark
dchandekstark / virtus_model.rb
Created December 17, 2015 18:08
Virtus model example
require "virtus"
# BEFORE
class Filter
attr_accessor :clauses
def initialize(clauses = nil)
@clauses = Array(clauses)
end
end
@dchandekstark
dchandekstark / autoversion.rb
Last active December 8, 2015 19:14
Auto-versioning for ActiveFedora::File
module ActiveFedora
module Autoversion
def self.included(base)
base.class_eval do
after_save :create_version
end
end
end
end
@dchandekstark
dchandekstark / ostruct_delegator.rb
Created September 16, 2015 21:03
OpenStructDelegator
require "ostruct"
require "delegate"
class OpenStructDelegator < SimpleDelegator
def initialize(args)
super OpenStruct.new(args)
end
require 'net/http'
FITS_SERVLET = "http://127.0.0.1:8080/FITSservlet/FitsServlet_2"
uri = URI(FITS_SERVLET)
# replace value with path to file
uri.query = URI.encode_www_form(file: "/tmp/logo.png")
response = Net::HTTP.get_response(uri)
@dchandekstark
dchandekstark / unconcerned.rb
Last active March 10, 2016 00:59
Unconcerned
ActiveSupport::Concern.module_eval do
WARNINGS = [
"Are you sure you want to this?",
"Come on, now, really?",
"Time to refactor?",
"Read http://blog.coreyhaines.com/2012/12/why-i-dont-use-activesupportconcern.html ?",
"Please don't do this without a good reason.",
"No, chunking your mega-class is not a good reason.",
"What objects are you trying to hide?",
@dchandekstark
dchandekstark / ability.rb
Created May 5, 2015 21:44
Ability Class
class Ability
include CanCan::Ability
class_attribute :permissions
attr_reader :user
def initialize(user)
@user = user
apply_permissions
@dchandekstark
dchandekstark / delws.sh
Last active August 29, 2015 14:20
Delete trailing whitespace
#!/bin/sh
TYPES="*.rb *.css *.js *.rake"
git ls-files ${TYPES} | xargs sed -E -i "" "s/[[:space:]]+$//g"