Sha256: de09f1e7b17d354b660fd58794e7e08880a6e75b44ac17b1c631f18feac09874

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

require 'simple_facade/version'

module SimpleFacade
  class Error < StandardError; end

  class Base
    def initialize(*attrs)
      @subjects = []
      if attrs.size == 1 && attrs.first.is_a?(Hash)
        @subjects += attrs.first.to_a
      else
        @subjects += attrs.select {|el| el.is_a(Array) && el.size == 2}
      end
    end

    def members
      subjects.to_h
    end

    def push(k, v)
      subjects.push([k, v])
    end

    def pop
      subjects.pop
    end

    def enqueue(k, v)
      subjects.unshift([k, v])
    end

    def dequeue
      subjects.shift
    end

    def insert(index, k, v)
      subjects.insert(index, [k, v])
    end

    def delete(key)
      subjects.delete_at(subjects.index { |el| el.first == key })
    end

    def method_missing(method_name, *attrs, &block)
      responder = members.values.detect { |obj| obj.respond_to?(method_name) }
      if responder
        responder.public_send(method_name, *attrs, &block)
      else
        super
      end
    end

    def respond_to_missing?(method_name, _include_private = false)
      members.values.any? { |obj| obj.respond_to?(method_name) }
    end

    private

    attr_reader :subjects
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_facade-0.1.1 lib/simple_facade.rb