Class: ScoobySnacks::Field
- Inherits:
-
Object
- Object
- ScoobySnacks::Field
- Defined in:
- lib/scooby_snacks/field.rb
Instance Attribute Summary collapse
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#oai_element ⇒ Object
readonly
Returns the value of attribute oai_element.
-
#oai_ns ⇒ Object
readonly
Returns the value of attribute oai_ns.
Instance Method Summary collapse
- #controlled? ⇒ Boolean
- #date? ⇒ Boolean
- #display_group ⇒ Object
- #display_groups ⇒ Object
- #display_options ⇒ Object
- #example ⇒ Object
- #helper_method ⇒ Object
- #in_display_group?(group_name) ⇒ Boolean
- #index_itemprop ⇒ Object
-
#initialize(name, raw_array) ⇒ Field
constructor
A new instance of Field.
- #itemprop ⇒ Object
- #oai? ⇒ Boolean
- #predicate ⇒ Object
- #primary_vocabulary ⇒ Object
- #search? ⇒ Boolean
- #search_result_display? ⇒ Boolean
- #solr_data_type ⇒ Object
- #solr_descriptors ⇒ Object
- #solr_facet_name ⇒ Object
- #solr_name(descriptor = nil) ⇒ Object
- #solr_names ⇒ Object
- #solr_search_name ⇒ Object
- #solr_sort_name ⇒ Object
- #sort? ⇒ Boolean
- #vocabularies ⇒ Object
Constructor Details
#initialize(name, raw_array) ⇒ Field
Returns a new instance of Field.
11 12 13 14 15 16 17 18 19 20 |
# 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 |
Instance Attribute Details
#label ⇒ Object (readonly)
Returns the value of attribute label.
4 5 6 |
# File 'lib/scooby_snacks/field.rb', line 4 def label @label end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/scooby_snacks/field.rb', line 4 def name @name end |
#oai_element ⇒ Object (readonly)
Returns the value of attribute oai_element.
4 5 6 |
# File 'lib/scooby_snacks/field.rb', line 4 def oai_element @oai_element end |
#oai_ns ⇒ Object (readonly)
Returns the value of attribute oai_ns.
4 5 6 |
# File 'lib/scooby_snacks/field.rb', line 4 def oai_ns @oai_ns end |
Instance Method Details
#controlled? ⇒ Boolean
58 59 60 61 62 63 64 65 66 67 |
# 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? ⇒ Boolean
88 89 90 |
# 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 ⇒ Object
140 141 142 |
# File 'lib/scooby_snacks/field.rb', line 140 def display_group display_groups.first end |
#display_groups ⇒ Object
136 137 138 |
# File 'lib/scooby_snacks/field.rb', line 136 def display_groups @raw_array['display_groups'] || Array.wrap(@raw_array['display_group']) end |
#display_options ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/scooby_snacks/field.rb', line 117 def = {label: label} if date? [:render_as] = :date elsif searchable? [:render_as] = :linked [:search_field] = name end return end |
#example ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# 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 ⇒ Object
96 97 98 99 |
# 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) ⇒ Boolean
128 129 130 |
# 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 ⇒ Object
109 110 111 |
# File 'lib/scooby_snacks/field.rb', line 109 def index_itemprop itemprop end |
#itemprop ⇒ Object
92 93 94 |
# File 'lib/scooby_snacks/field.rb', line 92 def itemprop @raw_array['index_itemprop'] || @raw_array['itemprop'] end |
#oai? ⇒ Boolean
113 114 115 |
# File 'lib/scooby_snacks/field.rb', line 113 def oai? !@oai_element.nil? && !@oai_ns.nil? end |
#predicate ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# 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 ⇒ Object
148 149 150 |
# File 'lib/scooby_snacks/field.rb', line 148 def primary_vocabulary vocabularies.first end |
#search? ⇒ Boolean
101 102 103 |
# File 'lib/scooby_snacks/field.rb', line 101 def search? searchable? end |
#search_result_display? ⇒ Boolean
132 133 134 |
# File 'lib/scooby_snacks/field.rb', line 132 def search_result_display? in_display_group? "search_result" end |
#solr_data_type ⇒ Object
185 186 187 |
# File 'lib/scooby_snacks/field.rb', line 185 def solr_data_type @raw_array['data_type'].downcase.to_sym || :text end |
#solr_descriptors ⇒ Object
176 177 178 179 180 181 182 183 |
# 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 ⇒ Object
166 167 168 169 |
# File 'lib/scooby_snacks/field.rb', line 166 def solr_facet_name return "" unless facet? solr_name(:facetable) end |
#solr_name(descriptor = nil) ⇒ Object
156 157 158 159 |
# 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 ⇒ Object
152 153 154 |
# File 'lib/scooby_snacks/field.rb', line 152 def solr_names solr_descriptors.reduce([]){|names, desc| names << solr_name(desc)} end |
#solr_search_name ⇒ Object
161 162 163 164 |
# File 'lib/scooby_snacks/field.rb', line 161 def solr_search_name return "" unless searchable? solr_name(:stored_searchable) end |
#solr_sort_name ⇒ Object
6 7 8 9 |
# 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? ⇒ Boolean
105 106 107 |
# File 'lib/scooby_snacks/field.rb', line 105 def sort? sortable? end |
#vocabularies ⇒ Object
144 145 146 |
# File 'lib/scooby_snacks/field.rb', line 144 def vocabularies @raw_array['vocabularies'] || Array.wrap(@raw_array['vocabulary']) end |