Sha256: 3dab7e82cd2f3cbc478d5ba6224ebce7b9eb7a73111d34e4ba3f5728ce028ffc

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

module Vedeu
  InvalidHeight = Class.new(StandardError)
  InvalidWidth  = Class.new(StandardError)
  XOutOfBounds  = Class.new(StandardError)
  YOutOfBounds  = Class.new(StandardError)

  module API
    class Interface
      def self.save(name, &block)
        new(name).save(&block)
      end

      def initialize(name)
        @name = name.to_s
      end

      def save(&block)
        instance_eval(&block) if block_given?

        stored_attributes = Store.create(attributes)
        interface = Vedeu::Interface.new(stored_attributes)

        Vedeu::Buffers.create(interface)

        interface
      end

      private

      attr_reader :name

      def use(value)
        Vedeu.use(value)
      end

      def x(value)
        fail XOutOfBounds if x_out_of_bounds?(value)

        attributes[:geometry][:x] = value
      end

      def y(value)
        fail YOutOfBounds if y_out_of_bounds?(value)

        attributes[:geometry][:y] = value
      end

      def width(value)
        fail InvalidWidth if x_out_of_bounds?(value)

        attributes[:geometry][:width] = value
      end

      def height(value)
        fail InvalidHeight if y_out_of_bounds?(value)

        attributes[:geometry][:height] = value
      end

      def centred(value)
        attributes[:geometry][:centred] = value
      end

      def y_out_of_bounds?(value)
        value < 1 || value > Terminal.height
      end

      def x_out_of_bounds?(value)
        value < 1 || value > Terminal.width
      end

      def method_missing(method_name, arg, &block)
        attributes[method_name] = arg
      end

      def attributes
        @attributes ||= { name: name, geometry: {} }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vedeu-0.1.10 lib/vedeu/api/interface.rb