Sha256: 3f8c3eade12d03aba26a90d1abddc1e557015d28fe61696aab8526e0dcab1b9b

Contents?: true

Size: 1.13 KB

Versions: 6

Compression:

Stored size: 1.13 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
      unless key.nil?
        options[:path] << key
        options[:key] = key
      end
      options[:label] = label
      type ||= Fields::Field
      custom_field type, options, &block
    end

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

    def label(label, options={}, &block)
      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

6 entries across 6 versions & 1 rubygems

Version Path
hammer_cli-3.13.0 lib/hammer_cli/output/dsl.rb
hammer_cli-3.12.0 lib/hammer_cli/output/dsl.rb
hammer_cli-3.10.0 lib/hammer_cli/output/dsl.rb
hammer_cli-3.9.0 lib/hammer_cli/output/dsl.rb
hammer_cli-3.8.0 lib/hammer_cli/output/dsl.rb
hammer_cli-3.7.0 lib/hammer_cli/output/dsl.rb