Sha256: 08a30e1b60debbdb7d4d3bc1bd642ccca39151a109db8f38396a2bf8b11091cd

Contents?: true

Size: 1.71 KB

Versions: 7

Compression:

Stored size: 1.71 KB

Contents

require 'json'

module Prospectus
  ##
  # Define item objects that defined expected vs actual state
  class Item
    attr_reader :list

    def initialize(params = {})
      @options = params
      @list = List.new(params)
      @dir = Dir.pwd
    end

    def name
      return @name if @name
      @name = File.basename @dir
      @name << "::#{File.basename @options[:file]}" if @options[:suffix_file]
      @name
    end

    def prefix(value)
      raise('Name not set for sub-item') unless @name
      @name = value + '::' + @name
    end

    def noop
      x = State.new
      x.value = 'noop'
      @expected = x
      @actual = x
    end

    def expected
      @expected || raise("No expected state was loaded for #{name}")
    end

    def actual
      @actual || raise("No actual state was loaded for #{name}")
    end

    def to_json(_ = {})
      { name: name, expected: expected.value, actual: actual.value }.to_json
    end
  end

  ##
  # DSL for wrapping eval of item files
  class ItemDSL
    def initialize(item, params)
      @item = item
      @options = params
    end

    def name(value)
      @item.instance_variable_set(:@name, value)
    end

    def noop
      @item.noop
    end

    def expected(&block)
      state(:@expected, &block)
    end

    def actual(&block)
      state(:@actual, &block)
    end

    def deps(&block)
      dsl = ListDSL.new(@item.list, @options)
      dsl.instance_eval(&block)
    end

    private

    def state(name, &block)
      state = Prospectus::State.from_block(@options, &block)
      @item.instance_variable_set(name, state)
    rescue => e # rubocop:disable Style/RescueStandardError
      raise("Failed to set #{name} state for #{@item.name}: #{e.message}")
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
prospectus-0.9.0 lib/prospectus/item.rb
prospectus-0.8.1 lib/prospectus/item.rb
prospectus-0.8.0 lib/prospectus/item.rb
prospectus-0.6.0 lib/prospectus/item.rb
prospectus-0.5.8 lib/prospectus/item.rb
prospectus-0.5.7 lib/prospectus/item.rb
prospectus-0.5.6 lib/prospectus/item.rb