class ScoobySnacks::Field

Attributes

label[R]
name[R]
oai_element[R]
oai_ns[R]

Public Class Methods

new(name, raw_array) click to toggle source
# File lib/scooby_snacks/field.rb, line 11
def initialize name, raw_array
  @raw_array = raw_array
  @name = name
  @label = raw_array['label'] || name.underscore.humanize

  if @raw_array['OAI'] && (oai_split =  @raw_array['OAI'].split(':',2))
    @oai_ns = oai_split.first.downcase
    @oai_element = oai_split.last
  end
end

Public Instance Methods

controlled?() click to toggle source
# File lib/scooby_snacks/field.rb, line 58
def controlled?
  return @controlled unless @controlled.nil?
  @controlled = false
  @controlled = true if @raw_array['controlled'].to_s == "true"
  @controlled = true if @raw_array['input'].to_s.include? "controlled"
  @controlled = true if (@raw_array['vocabularies'].is_a?(Array) && !@raw_array['vocabularies'].empty?)
  @controlled = true if (@raw_array['vocabulary'].is_a?(Array) && !@raw_array['vocabulary'].empty?)
  @controlled = true if (@raw_array['vocabulary'].is_a?(Hash) && !@raw_array['vocabulary'].empty?)
  return @controlled
end
date?() click to toggle source
# File lib/scooby_snacks/field.rb, line 88
def date?
  @date ||= (@raw_array['input'].to_s.downcase.include? "date") || (@raw_array['data_type'].to_s.downcase.include? "date")  
end
display_group() click to toggle source
# File lib/scooby_snacks/field.rb, line 140
def display_group
  display_groups.first
end
display_groups() click to toggle source
# File lib/scooby_snacks/field.rb, line 136
def display_groups
  @raw_array['display_groups'] || Array.wrap(@raw_array['display_group'])     
end
display_options() click to toggle source
# File lib/scooby_snacks/field.rb, line 117
def display_options
  options = {label: label}
  if date?
    options[:render_as] = :date
  elsif searchable?
    options[:render_as] = :linked 
    options[:search_field] = name
  end
  return options
end
example() click to toggle source
# File lib/scooby_snacks/field.rb, line 47
def example
  return @raw_array["example"] if @raw_array["example"]
  if controlled?
    return "http://id.loc.gov/authorities/names/n2002034393"
  elsif date?
    return "01-01-1901"
  else
    return "Example #{name.titleize}"
  end
end
helper_method() click to toggle source
# File lib/scooby_snacks/field.rb, line 96
def helper_method
  method_name = (@raw_array['index_helper_method'] || @raw_array['helper_method'])
  method_name.to_sym unless method_name.nil?
end
in_display_group?(group_name) click to toggle source
# File lib/scooby_snacks/field.rb, line 128
def in_display_group? group_name
  display_groups.each { |display_group| break true if (display_group.downcase == group_name.to_s.downcase) }==true
end
index_itemprop() click to toggle source
# File lib/scooby_snacks/field.rb, line 109
def index_itemprop
  itemprop
end
itemprop() click to toggle source
# File lib/scooby_snacks/field.rb, line 92
def itemprop
  @raw_array['index_itemprop'] || @raw_array['itemprop']
end
oai?() click to toggle source
# File lib/scooby_snacks/field.rb, line 113
def oai?
  !@oai_element.nil? && !@oai_ns.nil?
end
predicate() click to toggle source
# File lib/scooby_snacks/field.rb, line 69
def predicate
  return @predicate if @predicate
  raise ArgumentError.new("invalid predicate definition. Raw array: #{@raw_array.inspect}") if  @raw_array["predicate"].nil?
  namespace_prefix = @raw_array["predicate"].split(":").first
  predicate_name = @raw_array["predicate"].split(":",2).last
  #sort out the predicate namespace if necessary
  namespaces = schema.namespaces
  if namespaces.key?(namespace_prefix) 
    namespace_url = namespaces[namespace_prefix]
    raise ArgumentError.new("invalid predicate definition: #{@raw_array['predicate']}") unless namespace_url.include?("http")
    @predicate = ::RDF::URI.new(namespace_url + predicate_name)
  elsif defined?("::RDF::Vocab::#{namespace_prefix}".constantize)
    @predicate = "::RDF::Vocab::#{namespace_prefix}".constantize.send predicate_name
  else
    raise ArgumentError.new("invalid predicate definition: #{@raw_array['predicate']}")
  end
  @predicate
end
primary_vocabulary() click to toggle source
# File lib/scooby_snacks/field.rb, line 148
def primary_vocabulary
  vocabularies.first
end
search?() click to toggle source
# File lib/scooby_snacks/field.rb, line 101
def search?
  searchable?
end
search_result_display?() click to toggle source
# File lib/scooby_snacks/field.rb, line 132
def search_result_display?
  in_display_group? "search_result"
end
solr_data_type() click to toggle source
# File lib/scooby_snacks/field.rb, line 185
def solr_data_type
  @raw_array['data_type'].downcase.to_sym || :text
end
solr_descriptors() click to toggle source
# File lib/scooby_snacks/field.rb, line 176
def solr_descriptors
  descriptors = []
  descriptors << :symbol if (symbol? or [:string,:symbol].include?(@raw_array['data_type'].downcase.to_sym))
  descriptors << :stored_searchable if (searchable? and !descriptors.include?(:symbol))
  descriptors << :facetable if facet?
  descriptors << :displayable if (descriptors.empty? && stored_in_solr?)
  return descriptors
end
solr_facet_name() click to toggle source
# File lib/scooby_snacks/field.rb, line 166
def solr_facet_name
  return "" unless facet?
  solr_name(:facetable)
end
solr_name(descriptor = nil) click to toggle source
# File lib/scooby_snacks/field.rb, line 156
def solr_name(descriptor = nil)
  descriptor ||= solr_descriptors.first
  Solrizer.solr_name(name,descriptor,type: solr_data_type)
end
solr_names() click to toggle source
# File lib/scooby_snacks/field.rb, line 152
def solr_names
  solr_descriptors.reduce([]){|names, desc| names << solr_name(desc)}
end
solr_search_name() click to toggle source
# File lib/scooby_snacks/field.rb, line 161
def solr_search_name
  return "" unless searchable?
  solr_name(:stored_searchable)
end
solr_sort_name() click to toggle source
# File lib/scooby_snacks/field.rb, line 6
def solr_sort_name
  return false unless sort?
  @solr_sort_name ||= Solrizer.solr_name(name, :stored_sortable)
end
sort?() click to toggle source
# File lib/scooby_snacks/field.rb, line 105
def sort?
  sortable?
end
vocabularies() click to toggle source
# File lib/scooby_snacks/field.rb, line 144
def vocabularies
  @raw_array['vocabularies'] || Array.wrap(@raw_array['vocabulary'])
end

Private Instance Methods

schema() click to toggle source
# File lib/scooby_snacks/field.rb, line 191
def schema
  if defined? ScoobySnacks::METADATA_SCHEMA
    return ScoobySnacks::METADATA_SCHEMA
  else
    @schema ||= ScoobySnacks::MetadataSchema.new
  end
end