Sha256: 5a54e62e8effaff3caca3a79d18b12ca90c2fa47ee506ddcd38bf49f9eb6385b

Contents?: true

Size: 950 Bytes

Versions: 7

Compression:

Stored size: 950 Bytes

Contents

require_relative './errors'

module Materialist
  module MaterializedRecord

    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      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
    end

    def source
      source_raw.body
    end

    private

    def source_raw
      Materialist.configuration.api_client.get(source_url)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
materialist-3.8.3 lib/materialist/materialized_record.rb
materialist-3.8.2 lib/materialist/materialized_record.rb
materialist-3.8.1 lib/materialist/materialized_record.rb
materialist-3.8.0 lib/materialist/materialized_record.rb
materialist-3.7.0 lib/materialist/materialized_record.rb
materialist-3.6.0 lib/materialist/materialized_record.rb
materialist-3.5.0 lib/materialist/materialized_record.rb