Sha256: ffd80857a0948be33166c62a173d339d8b1b4513fd9e882355616d496bef5bd4

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

module SFKB
  # Methods to smartly apply singleton methods to REST objects
  module Decoration
    def decorate(object)
      return object unless block_given?
      yield object
      object
    end

    def define_link(object, name, url, &block)
      getter = -> (url) { get(url).body }
      decorator = -> (o) { decorate(o, &block) }
      object.define_singleton_method(name) { decorator.call(getter.call(url)) }
    end

    def define_links(object, urls, &block)
      urls.each { |k, v| define_link(object, k, v, &block) }
    end

    def define_predicate(object, name, value)
      object.define_singleton_method(:"#{name}?") { value }
    end

    def autodefine(object)
      return unless object.respond_to?(:additionalInformation)
      return unless info = object.additionalInformation
      info.each do |k, v|
        if [true, false].include?(v)
          define_predicate(object, k, v)
        elsif k.to_s == 'data'
          define_link(object, :data, v)
        elsif k.to_s == 'urls'
          define_links(object, v) { |o| autodefine(o) }
        end
      end
      object
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sfkb-0.1.2 lib/sfkb/decoration.rb
sfkb-0.1.0 lib/sfkb/decoration.rb