app/models/krikri/provider.rb in krikri-0.7.6 vs app/models/krikri/provider.rb in krikri-0.8.0

- old
+ new

@@ -4,22 +4,21 @@ # indexed in Solr. # # This is a read-only object. It does not write new providers to Solr, nor # does it update or delete providers. # + # If Provider is initialized with a valid value for :rdf_subject, the model + # can extrapolate all other attributes. + # # A DPLA::MAP::Agent object can be constructed from an instance of this # ActiveModel object using the :agent method. The DPLA::MAP::Agent object # interacts with Marmotta, rather than Solr. # - # @example Krikri::Provider.find(rdf_subject: 'http://blah.com/123').agent + # @example Krikri::Provider.new(rdf_subject: 'http://blah.com/123').agent # => [DPLA::MAP::Agent] # - class Provider - include ActiveModel::Validations - include ActiveModel::Conversion - extend ActiveModel::Naming - + class Provider < Krikri::ActiveModelBase ## # @!attribute :rdf_subject [String] the URI identifying the provider # @example: "http://blah.com/contributor/123" # # @!attribute :id [String] the stub id @@ -27,35 +26,16 @@ # # @!attribute :name [String] the human-redable name of the provider # # @!attribute :agent [DPLA::MAP::Agent] an ActiveTriples representation of # the provider, read from Marmotta. - attr_accessor :rdf_subject, :name + # + # @!attribute :field_value_reports [Array<Krikri::FieldValueReport>] + attr_accessor :rdf_subject, :name, :field_value_reports attr_reader :agent, :id ## - # Initializes a Provider object. - # - # @param attributes [Hash] - # If the params Hash contains a valid value for :rdf_subject, the model can - # extrapolate all other attributes. - # - # @example - # Krikri::Provider.new({ rdf_subject: 'http:://blah.com/contributor/123', - # name: 'Moomin Valley Historical Society' }) - # - # @raise [NoMethodError] if the params Hash includes a key that does not - # match listed attr_accessors - # - # @return [<Krikri::Provider>] - def initialize(attributes = {}) - attributes.each do |name, value| - send("#{name}=", value) - end - end - - ## # Gives a list of all providers, ignoring those with no URI (bnodes). # # Providers will be initialized with both an :rdf_subject and a :name, # if both exist in the Solr index. # @@ -67,11 +47,12 @@ 'facet.pivot' => 'provider_id,provider_name' } response = Krikri::SolrResponseBuilder.new(query_params).response response.facet_pivot['provider_id,provider_name'].map do |facet| rdf_subject = facet['value'] - name = facet['pivot'].present? ? facet['pivot'].first['value'] : nil + name = facet['pivot'].present? ? facet['pivot'].first['value'] : + rdf_subject provider = new({ :rdf_subject => rdf_subject, :name => name }) provider.valid_rdf_subject? ? provider : nil end.compact end @@ -87,12 +68,14 @@ :q => rdf_subject, :qf => 'provider_id', :fl => 'provider_id provider_name' } response = Krikri::SolrResponseBuilder.new(query_params).response return nil unless response.docs.count > 0 + name = response.docs.first['provider_name'].respond_to?(:first) ? + response.docs.first['provider_name'].first : rdf_subject new({ :rdf_subject => rdf_subject, - :name => response.docs.first['provider_name'].first }) + :name => name }) end ## # Get the base of the :rdf_subject for any provider # @return [String] ending in "/" @@ -111,25 +94,23 @@ def agent @agent ||= initialize_agent end + def field_value_reports + @field_value_reports ||= initialize_field_value_reports + end + ## # Tests for providers that have valid a :rdf_subject (not a bnode). # A valid :rdf_subject does not necessarily match and :rdf_subject in the # Solr index, but it has the correct URI format. def valid_rdf_subject? return false unless rdf_subject.present? rdf_subject.start_with?(self.class.base_uri) ? true : false end - ## - # Required ActiveModel method. - def persisted? - false - end - private ## # Gives the last path fragment for :rdf_subject in HTML escaped form, # ignoring :rdf_subjects, ignoring bnodes. @@ -167,8 +148,14 @@ # # @return [DPLA::MAP::Agent] def initialize_agent return nil unless valid_rdf_subject? DPLA::MAP::Agent.new(rdf_subject).tap { |agent| agent.label = name } + end + + def initialize_field_value_reports + Krikri::FieldValueReport.fields.map do |field| + Krikri::FieldValueReport.new({ field: field, provider: self }) + end end end end