class ScoobySnacks::MetadataSchema

Constants

SS_BOOLEAN_ATTRIBUTES
SS_DISPLAY_GROUPS
SS_STRING_ATTRIBUTES

Attributes

fields[R]
namespaces[R]

Public Class Methods

boolean_attributes() click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 45
def self.boolean_attributes
  SS_BOOLEAN_ATTRIBUTES + custom_boolean_attributes
end
custom_boolean_attributes() click to toggle source

override this locally to define app-specific attributes

# File lib/scooby_snacks/metadata_schema.rb, line 31
def self.custom_boolean_attributes
  []
end
custom_display_groups() click to toggle source

override this locally to define app-specific display groups

# File lib/scooby_snacks/metadata_schema.rb, line 41
def self.custom_display_groups
  []
end
custom_string_attributes() click to toggle source

override this locally to define app-specific attributes

# File lib/scooby_snacks/metadata_schema.rb, line 36
def self.custom_string_attributes
  []
end
define_boolean_attribute_methods(boolean_attributes) click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 85
def self.define_boolean_attribute_methods boolean_attributes

  # Define methods to cache and return lists of fields & field names
  # that share certain boolean characteristics (controlled, required, etc).
  boolean_attributes.each do |attribute|
    # Skip any attribute we have a custom method for
    next if [:work_title].include? attribute
    define_method("#{attribute}_fields".to_sym) do
      @fields.values.select{|field| field.send("#{attribute}?".to_sym)}
    end
    define_method("#{attribute}_field_names".to_sym) do
      field_names = send("#{attribute}_fields".to_sym).map{|field| field.name}
      instance_variable_set("@#{attribute}_field_names".to_sym, field_names)
    end
  end
end
define_display_group_methods(display_groups) click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 57
def self.define_display_group_methods display_groups
  # define methods to list display group contents
  display_groups.each do |display_group|

    define_method("#{display_group}_display_field_names") do 
      field_names = instance_variable_get("@#{display_group}_display_field_names")
      if field_names.nil?
        send("#{display_group}_display_fields".to_sym).map{|field| field.name}
      else
        field_names
      end
    end

    define_method("#{display_group}_display_fields") do 
      field_names = instance_variable_get("@#{display_group}_display_field_names")
      if field_names.nil?
        fields = all_fields.select{|field| field.in_display_group?(display_group)}
        instance_variable_set("@#{display_group}_display_field_names", fields.map{|field| field.name}) 
        fields
      else
        field_names.map{|name| get_field(name)}
      end
    end   
  end
end
display_groups() click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 53
def self.display_groups
  SS_DISPLAY_GROUPS + custom_display_groups
end
new(schema_config_path: nil, raw_schema: nil) click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 104
def initialize (schema_config_path: nil, raw_schema: nil)
  schema_config_path ||= default_schema_config_path
  raw_schema ||= YAML.load_file(schema_config_path)
  @namespaces = raw_schema['namespaces']
  raw_fields = (raw_schema['fields'] || raw_schema['properties'])
  @fields = raw_fields.except('default').keys.reduce({}) do |fields, field_name|
    field = raw_fields['default'].deep_merge raw_fields[field_name]
    fields[field_name] = ScoobySnacks::Field.new(field_name,field)
    fields
  end
end
string_attributes() click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 49
def self.string_attributes
  SS_STRING_ATTRIBUTES + custom_string_attributes
end

Public Instance Methods

all_field_names() click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 124
def all_field_names
  @fields.keys
end
all_fields() click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 128
def all_fields
  @fields.values
end
default_text_search_solrized_field_names() click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 132
def default_text_search_solrized_field_names
  # Include all fields marked for full text search that are also individual search fields
  # and therefore excluded from the 'all_text_timv' search field
  field_names = (full_text_searchable_field_names & searchable_field_names).uniq
  field_solr_names = field_names.map{|field_name| get_field(field_name).solr_search_name }
  return( field_solr_names + [full_text_field_name] )
end
display_field_names() click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 152
def display_field_names
  primary_display_field_names + secondary_display_field_names
end
display_fields() click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 156
def display_fields
  primary_display_fields + secondary_display_fields
end
full_text_field_name() click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 140
def full_text_field_name
  "all_text_timv"
end
get_field(name) click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 116
def get_field(name)
  @fields[name.to_s] || @fields[label_map[name.to_s]]
end
get_property(name) click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 120
def get_property(name)
  get_field(name)
end
search_field_names() click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 173
def search_field_names
  searchable_field_names
end
search_fields() click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 169
def search_fields
  searchable_fields
end
sort_field_names() click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 165
def sort_field_names
  sortable_field_names
end
sort_fields() click to toggle source

A few aliases for these methods since I've been using both conventions

# File lib/scooby_snacks/metadata_schema.rb, line 161
def sort_fields
  sortable_fields
end
work_title_field() click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 144
def work_title_field
  @fields.values.select{|field| field.in_display_group?("title") || field.work_title?}.first
end
work_title_field_name() click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 148
def work_title_field_name
  work_title_field.name
end

Private Instance Methods

default_schema_config_path() click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 183
def default_schema_config_path
  @schema_config_path ||= File.join(Rails.root.to_s,'config',schema_config_filename)
end
label_map() click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 179
def label_map
  @label_map ||= @fields.values.reduce({}){|map,field| map[field.label] = field.name if field.label.present?; map }
end
schema_config_filename() click to toggle source
# File lib/scooby_snacks/metadata_schema.rb, line 187
def schema_config_filename
  "metadata.yml"
end