Sha256: 71728dd16d5b93d993c3e6354d61e1347029406258e14998a2480875bebda4f1

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

require 'vedeu/models/geometry'
require 'vedeu/support/interface_store'
require 'vedeu/support/terminal'

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

  class InterfaceTemplate
    def self.save(name, &block)
      new(name).save(&block)
    end

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

    def save(&block)
      self.instance_eval(&block)

      InterfaceStore.create(attributes)
    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

    private

    attr_reader :name

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

    def method_missing(method_name, arg, &block)
      attributes[method_name] = arg
    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vedeu-0.1.4 lib/vedeu/support/interface_template.rb