Sha256: 0f3e3c5bb48fbb0b453fddad3c99073ac5bdd6e463806ad5acfa585b9bbc152c

Contents?: true

Size: 1.09 KB

Versions: 8

Compression:

Stored size: 1.09 KB

Contents

module HammerCLI::Output

  class Dsl

    def initialize(options={})
      @current_path = options[:path] || []
    end

    def fields
      @fields ||= []
      @fields
    end

    def build(&block)
      self.instance_eval &block
    end

    protected

    def field(key, label, type=nil, options={}, &block)
      options[:path] = current_path.clone
      options[:path] << key if !key.nil?

      options[:label] = label
      type ||= Fields::DataField
      custom_field type, options, &block
    end

    def custom_field(type, options={}, &block)
      self.fields << type.new(options, &block)
    end

    def label(label, &block)
      options = {}
      options[:path] = current_path.clone
      options[:label] = label
      custom_field Fields::Label, options, &block
    end

    def from(key)
      self.current_path.push key
      yield if block_given?
      self.current_path.pop
    end

    def collection(key, label, options={}, &block)
      field key, label, Fields::Collection, options, &block
    end


    def current_path
      @current_path ||= []
      @current_path
    end

  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
hammer_cli-0.0.18 lib/hammer_cli/output/dsl.rb
hammer_cli-0.0.16 lib/hammer_cli/output/dsl.rb
hammer_cli-0.0.15 lib/hammer_cli/output/dsl.rb
hammer_cli-0.0.14 lib/hammer_cli/output/dsl.rb
hammer_cli-0.0.13 lib/hammer_cli/output/dsl.rb
hammer_cli-0.0.12 lib/hammer_cli/output/dsl.rb
hammer_cli-0.0.11 lib/hammer_cli/output/dsl.rb
hammer_cli-0.0.10 lib/hammer_cli/output/dsl.rb