Module: Materialist::MaterializedRecord::ClassMethods

Defined in:
lib/materialist/materialized_record.rb

Instance Method Summary collapse

Instance Method Details



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/materialist/materialized_record.rb', line 12

def source_link_reader(*keys, via: nil, allow_nil: false)
  keys.each do |key|
    define_method(key) do
      (via ? [via, key] : [key])
        .inject(source_raw) do |res, path|
          begin
            (res && res.body._links.include?(path)) ?
              res.send(path).show :
              (allow_nil ? nil : raise(ResourceNotFound))
          rescue Routemaster::Errors::ResourceNotFound
            (allow_nil ? nil : raise(ResourceNotFound))
          end
        end
        &.body
    end
  end
end