Module: FedoraLens::Lenses

Included in:
TestClass
Defined in:
lib/fedora_lens/lenses.rb

Class Method Summary collapse

Class Method Details

.as_domObject


86
87
88
89
90
91
92
93
# File 'lib/fedora_lens/lenses.rb', line 86

def as_dom
  Lens[
    # TODO figure out a memoizing strategy so we don't parse multiple times
    get: lambda {|xml| Nokogiri::XML(xml)},
    put: lambda {|xml, value| value.to_xml},
    create: lambda {|value| value.to_xml}
  ]
end

.at_css(selector) ⇒ Object


95
96
97
98
99
100
101
102
# File 'lib/fedora_lens/lenses.rb', line 95

def at_css(selector)
  Lens[
    get: lambda {|doc| doc.at_css(selector).content},
    put: lambda {|doc, value| 
    doc.at_css(selector).content = value; doc},
    # can't do a css create because we don't know the structure
  ]
end

.compose(outer, inner) ⇒ Object


184
185
186
187
188
189
190
191
192
193
# File 'lib/fedora_lens/lenses.rb', line 184

def compose(outer, inner)
  Lens[
    get: lambda do |source|
      inner.get(outer.get(source))
    end,
    put: lambda do |source, value|
      outer.put(source, inner.put(outer.get(source), value))
    end
  ]
end

.empty_property(graph, rdf_subject, predicate, should_delete) ⇒ Object


104
105
106
107
108
109
110
111
112
113
114
# File 'lib/fedora_lens/lenses.rb', line 104

def empty_property(graph, rdf_subject, predicate, should_delete)
  if should_delete
    graph.query([rdf_subject, predicate, nil]).each_statement do |statement|
      if should_delete.call(statement.object)
        graph.delete(statement)
      end
    end
  else
    graph.delete([rdf_subject, predicate, nil])
  end
end

.get_predicate(predicate, opts = {}) ⇒ Object

Parameters:

  • predicate (RDF::URI)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :select (Proc)

    a proc that takes the object and returns true if it should be included in this subset of the predicates. Used for storing different classes of objects under different attribute names even though they all use the same predicate.


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/fedora_lens/lenses.rb', line 124

def get_predicate(predicate, opts = {})
  Lens[
    get: lambda do |orm|
      values = orm.value(predicate)
      if opts[:select]
        values.select { |val| opts[:select].call(val) }
      else
        values
      end
    end,
    put: lambda do |orm, values|
      empty_property(orm.graph, orm.resource.subject_uri, predicate, opts[:select])
      Array(values).each do |value|
        next if value.nil?
        orm.graph.insert([orm.resource.subject_uri, predicate, value])
      end
      orm
    end,
    create: lambda do |values|
      orm = Ldp::Orm.new(Ldp::Resource::RdfSource.new(FedoraLens.connection, '', RDF::Graph.new))
      values.each do |value|
        orm.graph.insert([orm.resource.subject_uri, predicate, value])
      end
      orm
    end
  ]
end

.hash_updateObject


63
64
65
66
67
68
# File 'lib/fedora_lens/lenses.rb', line 63

def hash_update
  Lens[
    get: lambda {|hash| hash[key]},
    put: lambda {|hash, pair| hash.merge(pair[0] => pair[1])}
  ]
end

.literal_to_stringObject


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fedora_lens/lenses.rb', line 24

def literal_to_string
  Lens[
    get: :to_s.to_proc,
    put: lambda do |source, value|
      if source.is_a?(RDF::Literal)
        RDF::Literal.new(value, language: source.language, datatype: source.datatype)
      else
        RDF::Literal.new(value)
      end
    end
  ]
end

.literals_to_stringsObject


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fedora_lens/lenses.rb', line 37

def literals_to_strings
  Lens[
    get: lambda do |source|
      source.map(&:to_s)
    end,
    put: lambda do |sources, values|
      Array(values).map do |value|
        RDF::Literal.new(value)
      end
    end
  ]
end

.load_model(klass) ⇒ Object


152
153
154
155
156
157
158
159
160
161
162
# File 'lib/fedora_lens/lenses.rb', line 152

def load_model(klass)
  Lens[
    get: lambda do |id|
      klass.find(id)
    end,
    put: lambda do |id, model|
      model.save
      id
    end
  ]
end

.load_or_build_ormObject


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/fedora_lens/lenses.rb', line 164

def load_or_build_orm
  Lens[
    get: lambda do |uri|
      if uri.present?
        Ldp::Orm.new(Ldp::Resource::RdfSource.new(FedoraLens.connection, uri.to_s))
      else
        Ldp::Orm.new(Ldp::Resource::RdfSource.new(FedoraLens.connection, nil, RDF::Graph.new))
      end
    end,
    put: lambda do |uri, orm|
      if orm.resource.subject.present?
        orm.save
      else
        orm.resource.create
      end
      orm.resource.subject_uri
    end
  ]
end

.orm_to_hash(name_to_lens) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fedora_lens/lenses.rb', line 70

def orm_to_hash(name_to_lens)
  Lens[
    get: lambda do |orm|
      name_to_lens.inject({}) do |hash, (key, lens)|
        hash.merge(key => lens.get(orm))
      end
    end,
    put: lambda do |orm, hash|
      name_to_lens.each do |(key, lens)|
        lens.put(orm, hash[key])
      end
      orm
    end
  ]
end

.singleObject


16
17
18
19
20
21
22
# File 'lib/fedora_lens/lenses.rb', line 16

def single
  Lens[
    get: :first.to_proc,
    put: lambda {|source, value| source[0] = value; source},
    create: lambda {|value| [value]}
  ]
end

.uris_to_idsObject


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fedora_lens/lenses.rb', line 50

def uris_to_ids
  Lens[
    get: lambda do |source|
      source.map { |uri| FedoraLens.uri_to_id(URI.parse(uri).to_s) }
    end,
    put: lambda do |sources, values|
      Array(values).compact.map do |value|
        RDF::URI.new(FedoraLens.id_to_uri(value))
      end
    end
  ]
end

.zip(first, second) ⇒ Object


195
196
# File 'lib/fedora_lens/lenses.rb', line 195

def zip(first, second)
end