Class: ActiveFedora::LoadableFromJson::SolrBackedResource

Inherits:
Object
  • Object
show all
Defined in:
lib/active_fedora/loadable_from_json.rb

Defined Under Namespace

Classes: FakeQuery

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ SolrBackedResource

Returns a new instance of SolrBackedResource.



34
35
36
37
# File 'lib/active_fedora/loadable_from_json.rb', line 34

def initialize(model)
  @model = model
  @hash = {}
end

Instance Method Details

#freezeObject



30
31
32
# File 'lib/active_fedora/loadable_from_json.rb', line 30

def freeze
  @hash.freeze
end

#get_values(k) ⇒ Object



49
50
51
# File 'lib/active_fedora/loadable_from_json.rb', line 49

def get_values(k)
  @hash[k]
end

#insert(vals) ⇒ Object

Called by Associations::RDF#replace to add data to this resource represenation

Parameters:

  • vals (Array)

    an array of 3 elements (subject, predicate, object) to insert



100
101
102
103
104
105
106
107
108
# File 'lib/active_fedora/loadable_from_json.rb', line 100

def insert(vals)
  _, pred, val = vals
  k = reflection(pred)
  if @hash[k].is_a?(Array)
    set_value(k, @hash[k] << val)
  else
    set_value(k, [val])
  end
end

#persist!Object



53
54
55
# File 'lib/active_fedora/loadable_from_json.rb', line 53

def persist!(*)
  true
end

#query(args = {}) ⇒ Object



88
89
90
91
92
# File 'lib/active_fedora/loadable_from_json.rb', line 88

def query(args = {})
  predicate = args[:predicate]
  reflection = reflection(predicate)
  FakeQuery.new(get_values(reflection))
end

#rdf_subjectObject



94
95
96
# File 'lib/active_fedora/loadable_from_json.rb', line 94

def rdf_subject
  ::RDF::URI.new(nil)
end

#reflection(predicate) ⇒ Object

Find the reflection on the model that uses the given predicate



111
112
113
114
115
# File 'lib/active_fedora/loadable_from_json.rb', line 111

def reflection(predicate)
  result = Array(@model.outgoing_reflections.find { |_key, reflection| reflection.predicate == predicate }).first
  return result if result
  fail "Unable to find reflection for #{predicate} in #{@model}"
end

#set_value(k, v) ⇒ Object

It is expected that the singular filter gets applied after fetching the value from this resource, so cast everything back to an array.



45
46
47
# File 'lib/active_fedora/loadable_from_json.rb', line 45

def set_value(k, v)
  @hash[k] = Array(v)
end

#to_sObject



39
40
41
# File 'lib/active_fedora/loadable_from_json.rb', line 39

def to_s
  @hash.to_s
end