class JiraMule::Style

Style is a formula for formatting the results of a query. This currently only works with single queries.

Constants

FORMAT_TYPES

Attributes

default_query[RW]

May need to split this into two classes. One that is the above methods and one that is the below methods. The below one is used just for the construction of a Style. While the above is the usage of a style.

Maybe the above are in a Module, that is included as part of fetch?

header[RW]
prefix_query[RW]

May need to split this into two classes. One that is the above methods and one that is the below methods. The below one is used just for the construction of a Style. While the above is the usage of a style.

Maybe the above are in a Module, that is included as part of fetch?

suffix_query[RW]

May need to split this into two classes. One that is the above methods and one that is the below methods. The below one is used just for the construction of a Style. While the above is the usage of a style.

Maybe the above are in a Module, that is included as part of fetch?

Public Class Methods

add(style, &block) click to toggle source
# File lib/JiraMule/StyleLoader.rb, line 27
def self.add(style, &block)
  @@styles = {} unless defined?(@@styles)

  if style.kind_of?(String) or style.kind_of?(Symbol) then
    style = Style.new(style, &block)
  end

  @@styles[style.name] = style
end
fetch(name) click to toggle source
# File lib/JiraMule/StyleLoader.rb, line 37
def self.fetch(name)
  @@styles[name.to_sym]
end
list() click to toggle source
# File lib/JiraMule/StyleLoader.rb, line 41
def self.list
  @@styles.keys
end
new(name, &block) click to toggle source
# File lib/JiraMule/StyleLoader.rb, line 7
def initialize(name, &block)
  @name = name.to_sym
  @fields = [:key, :summary]
  @header = [:key, :summary]
  @footer = nil
  @format_type = :strings
  @format = %{{{key}} {{summary}}}

  @custom_tags = {}

  @prefix_query = nil
  @default_query = nil
  @suffix_query = nil

  @bolden_tag = :bolden

  block.call(self) if block_given?
end

Public Instance Methods

add_tag(name, &block) click to toggle source

Create a custom tag for formatted output

# File lib/JiraMule/StyleLoader.rb, line 186
def add_tag(name, &block)
  @custom_tags[name.to_sym] = block
end
apply(issues) click to toggle source

Apply this style to Issues @param issues [Array] Issues from the Jira query to format @return formatted string

# File lib/JiraMule/StyleLoader.rb, line 49
def apply(issues)
  if @format_type == :strings then
    keys = issues.map do |issue|
      fmt = @format
      fmt = fmt.join(' ') if fmt.kind_of? Array
      res = JiraMule::IssueRender.render(fmt, issue.merge(issue[:fields]), @custom_tags)
      bolden(issue, res)
    end
    (@header or '').to_s + keys.join("\n") + (@footer or '').to_s

  elsif [:table, :table_rows, :table_columns].include? @format_type then
    @format = [@format] unless @format.kind_of? Array
    rows = issues.map do |issue|
      issue = issue.merge(issue[:fields])
      @format.map do |col|
        if col.kind_of? Hash then
          col = col.dup
          str = col[:value] or ""
          res = JiraMule::IssueRender.render(str, issue, @custom_tags)
          col[:value] = bolden(issue, res)
          col
        else
          res = JiraMule::IssueRender.render(col, issue, @custom_tags)
          bolden(issue, res)
        end
      end
    end
    if @format_type == :table_columns then
      rows = rows.transpose
    end
    header = (@header or [])
    header = [header] unless header.kind_of? Array
    Terminal::Table.new :headings => header, :rows=>rows
  end
end
bolden(issue, row, color=:bold) click to toggle source

If this issue should be bolded or not.

# File lib/JiraMule/StyleLoader.rb, line 86
def bolden(issue, row, color=:bold)
  bld = issue[@bolden_tag]
  bld = @custom_tags[@bolden_tag] if bld.nil?
  bld = bld.call(issue.dup) if bld.kind_of? Proc
  # ? truthy other than Ruby default?
  return row unless bld
  if row.kind_of? Array then
    row.map{|r| HighLine.color(r, color)}
  elsif row.kind_of? Hash then
    hsh={}
    row.each_pair{|k,v| hsh[k] = HighLine.color(v, color)}
  else
    HighLine.color(row.to_s, color)
  end
end
build_query(*args) click to toggle source

Build a query based on this Style and other bits from command line @param args [Array<String>] Other bits of JQL to use instead of default_query

# File lib/JiraMule/StyleLoader.rb, line 106
def build_query(*args)
  opts = {}
  opts = args.pop if args.last.kind_of? Hash

  # Add the default query, if there is one
  if not @default_query.nil? then
    # make sure there is an AND if working with a cmdline supplied part
    args.push('AND') unless args.empty?
    case @default_query
    when Array
      args.push @default_query.join(' AND ')
    when Proc
      args.push @default_query.call()
    else
      args.push @default_query.to_s
    end
  end

  # Get prefix as a String.
  case @prefix_query
  when Array
    prefix = @prefix_query.join(' AND ') + ' AND'
  when Proc
    prefix = @prefix_query.call()
  else
    prefix = @prefix_query.to_s
  end
  args.unshift(prefix) unless opts.has_key? :noprefix

  # Get suffix as a String.
  case @suffix_query
  when Array
    suffix = 'AND ' + @suffix_query.join(' AND ')
  when Proc
    suffix = @suffix_query.call()
  else
    suffix = @suffix_query.to_s
  end
  args.push(suffix) unless opts.has_key? :nosuffix

  args.flatten.compact.join(' ')
end
fields(*args) click to toggle source

takes a single flat array of key names.

# File lib/JiraMule/StyleLoader.rb, line 164
def fields(*args)
  return @fields if args.empty?
  @fields = args.flatten.compact.map{|i| i.to_sym}
end
Also aliased as: fields=
fields=(*args)
Alias for: fields
format(*args) click to toggle source
# File lib/JiraMule/StyleLoader.rb, line 178
def format(*args)
  return @format if args.empty?
  args.flatten! if args.kind_of? Array
  @format = args
end
Also aliased as: format=
format=(*args)
Alias for: format
format_type(type) click to toggle source
# File lib/JiraMule/StyleLoader.rb, line 171
def format_type(type)
  return @format_type if type.nil?
  raise "Unknown format type: \"#{type}\"" unless FORMAT_TYPES.include? type
  @format_type = type
end
Also aliased as: format_type=
format_type=(type)
Alias for: format_type
name() click to toggle source
# File lib/JiraMule/StyleLoader.rb, line 159
def name
  @name
end