def create_filter(values)
        values.scan(/(?<=,|^)(BEGIN.*?END|\/.*?\/|.*?)(?=,|$)/i).flatten.each do |value|
          send(value)
        end unless values.nil?
      end

def create_filter ( values ) values . scan ( / \/ \/ /i ) . flatten . each do | value | send ( value ) end unless values . nil? end

Creates a filter based on the provided rows and columns select criteria


def equal(value, args, block)
        filter << value.to_i unless filter.index(value.to_i)
      end

def equal ( value , args , block ) filter << value . to_i unless filter . index ( value . to_i ) end


def equal_type(type, value, args, block)
        filter_size_before = filter.size
        equal(value, args, block)
        types << type if filter_size_before < filter.size
      end

def equal_type ( type , value , args , block ) filter_size_before = filter . size equal ( value , args , block ) types << type if filter_size_before < filter . size end

Adds a single value and an associated type to the filter


def range(start_value, end_value, args, block)
        filter << (start_value.to_i..end_value.to_i).to_a
      end

def range ( start_value , end_value , args , block ) filter << ( start_value . to_i .. end_value . to_i ) . to_a end


def range_type(type, start_value, end_value, args, block)
        filter_size_before = filter.size
        range(start_value, end_value, args, block)
        (filter.size - filter_size_before).times { types << type }
      end

def range_type ( type , start_value , end_value , args , block ) filter_size_before = filter . size range ( start_value , end_value , args , block ) ( filter . size - filter_size_before ) . times { types << type } end

Adds a range and the associated types to the filter


def col_regex(col, r, args, block)
        operation = "'[value]' =~ Regexp.new('#{r}')"
        pivot[r] = { col: col, operation: operation }
      end

def
col_regex
(
col
,
r
,
args
,
block
)
operation
=
"'[value]' =~ Regexp.new('#{r}')"
pivot
[
r
]
=
{
col
:
col
,
operation
:
operation
}
end


def date(col, comparator, date, args, block)
        comparator = '==' if comparator == '='
        operation = "Date.strptime(\"[value]\", \"#{date_format}\") #{comparator} " +
                    "Date.strptime(\"#{date}\", \"#{date_format}\")"
        pivot["#{comparator}#{date}"] = { col: col, operation: operation }
      end


def date_range(col, start_date, end_date, args, block)
        operation = "   Date.strptime(\"#{start_date}\",  \"#{date_format}\") "    +
                    "<= Date.strptime(\"[value]\",        \"#{date_format}\") && " +
                    "   Date.strptime(\"[value]\",        \"#{date_format}\") "    +
                    "<= Date.strptime(\"#{end_date}\",    \"#{date_format}\")"
        pivot["#{start_date}-#{end_date}"] = { col: col, operation: operation }
      end


def number(col, comparator, number, args, block)
        comparator = '==' if comparator == '='
        operation = "[value] #{comparator} #{number}"
        pivot["#{comparator}#{number}"] = { col: col, operation: operation }
      end

def
number
(
col
,
comparator
,
number
,
args
,
block
)
comparator
=
'=='
if
comparator
==
'='
operation
=
"[value] #{comparator} #{number}"
pivot
[
"#{comparator}#{number}"
]
=
{
col
:
col
,
operation
:
operation
}
end


def number_range(col, start_number, end_number, arg, block)
        operation = " #{start_number} <= [value] && [value] <= #{end_number}"
        pivot["#{start_number}-#{end_number}"] = { col: col, operation: operation }
      end


def create_annotation(hook, annotation)
      # validate the arguments
      if hook.nil? then raise ArgumentError.new("Annotated caTissue object is missing") end
      if annotation.nil? then raise ArgumentError.new("Annotation caTissue object is missing") end
      # the annotated object must exist in the database
      unless hook.identifier then
        raise AnnotationError.new("Annotation writer does not support annotation of a caTissue object without an identifier: #{hook}")
      end
      # load the caRuby annotations if necessary
      hook.class.ensure_annotations_loaded
      # set the annotation hook reference
      annotation.hook = hook
      # create the annotation in the database
      annotation.create
    end

Creates a new annotation object in the caTissue database.

 @param [CaTissue::Resource] hook the existing static hook object to annotate
 @param [CaTissue::Annotation] annotation the annotation object to create
 @raise [AnnotationError] if the hook object does not have a database identifier

Creates a new annotation object in the caTissue database .


def join_as_series(options = {})
      if defined?(I18n)
        default_delimiter       = I18n.translate(:'series_joiner.default_delimiter',       :locale => options[:locale])
        default_final_delimiter = I18n.translate(:'series_joiner.default_final_delimiter', :locale => options[:locale])
        default_conjunction     = I18n.translate(:'series_joiner.default_conjunction',     :locale => options[:locale])
      else
        default_delimiter       = ', '
        default_final_delimiter = ''
        default_conjunction     = ' and '
      end

      delimiter = options[:delimiter] || default_delimiter
      final_delimiter = options[:final_delimiter] || default_final_delimiter
      conjunction = options[:conjunction] || default_conjunction

      sz = self.size
      if sz > 0
        r = self[0]
        if sz > 1
          if sz > 2
            for i in 1..(sz - 2)
              r += delimiter + self[i]
            end
            r += final_delimiter
          end
          r += conjunction + self[sz - 1]
        end
      end
      return r
    end

def
join_as_series
(
options
=
{
}
)
if
defined?
(
I18n
)
default_delimiter
=
I18n
.
translate
(
:'
'
,
:locale
=>
options
[
:locale
]
)
default_final_delimiter
=
I18n
.
translate
(
:'
'
,
:locale
=>
options
[
:locale
]
)
default_conjunction
=
I18n
.
translate
(
:'
'
,
:locale
=>
options
[
:locale
]
)
else
default_delimiter
=
', '
default_final_delimiter
=
''
default_conjunction
=
' and '
end
delimiter
=
options
[
:delimiter
]
||
default_delimiter
final_delimiter
=
options
[
:final_delimiter
]
||
default_final_delimiter
conjunction
=
options
[
:conjunction
]
||
default_conjunction
sz
=
self
.
size
if
sz
>
0
r
=
self
[
0
]
if
sz
>
1
if
sz
>
2
for
i
in
1
..
(
sz
2
)
r
+=
delimiter
+
self
[
i
]
end
r
+=
final_delimiter
end
r
+=
conjunction
+
self
[
sz
1
]
end
end
return
r
end

Joins items in an array together in a grammatically correct manner.

 Options:
 * `delimiter` - inserted between items, except for the final two (default: ', ')
 * `final_delimiter` - inserted between the final two items (if > 2), but before the conjunction (default: '')
 * `conjunction` - inserted between the final two items (default: ' and ')

Joins items in an array together in a grammatically correct manner .


def awesome_string(string)
      lexers = ::Rouge::Guessers::Source.new(string).filter(R_LEXERS)
      if !lexers.empty?
        format_syntax_string(string, lexers.first)
      elsif string =~ /(?:\r?\n)(?!\z)/
        format_multiline_string(string)
      else
        format_plain_string(string)
      end
    end

def awesome_string ( string ) lexers = :: Rouge :: Guessers :: Source . new ( string ) . filter ( R_LEXERS ) if ! lexers . empty? format_syntax_string ( string , lexers . first ) elsif string =~ / \r \n \z / format_multiline_string ( string ) else format_plain_string ( string ) end end

Format a String for awesome_print display.

 @param string [String] the String to format
 @return [String] the formatted String

Format a String for awesome_print display .


def meta=val
      val = JSON.parse(val) if val.is_a?(String)
      write_store_attribute(:data, :meta, val)
    end

def meta = val val = JSON . parse ( val ) if val . is_a? ( String ) write_store_attribute ( :data , :meta , val ) end

ensure meta always will be saved as json instead of json string


def execute
      File.new(infile).each_with_index do |line, index|
        row = row_filter.process(line, row: index)
        next if row.nil? or row.chomp.empty?
        collection.each do |category, elements|
          values = elements[:filter].process(row)
          values.chomp.split(';').each do |value|
            elements[:entries] << value.chomp if elements[:entries].index(value.chomp).nil?
          end
        end
      end

      File.open(outfile, 'w') do |out|
        collection.each do |category, elements|
          out.puts "[#{category}]"
          elements[:entries].sort.each { |c| out.puts c }
        end
      end
    end

def execute File . new ( infile ) . each_with_index do | line , index | row = row_filter . process ( line , row : index ) next if row . nil? or row . chomp . empty? collection . each do | category , elements | values = elements [ :filter ] . process ( row ) values . chomp . split ( ';' ) . each do | value | elements [ :entries ] << value . chomp if elements [ :entries ] . index ( value . chomp ) . nil? end end end File . open ( outfile , 'w' ) do | out | collection . each do | category , elements | out . puts "[#{category}]" elements [ :entries ] . sort . each { | c | out . puts c } end end end

Creates a new Collector
 Execute the collector


def init_collection(column_filter)
        column_filter.split('+').each do |f|
          category, filter = f.split(':')
          collection[category] = { entries: [], filter: ColumnFilter.new(filter) }
        end
      end

def init_collection ( column_filter ) column_filter . split ( '+' ) . each do | f | category , filter = f . split ( ':' ) collection [ category ] = { entries : [ ] , filter : ColumnFilter . new ( filter ) } end end


def log
      out.sync = true
      @log ||= Logger.new(out)

      @log.formatter = proc do |severity, datetime, progname, msg|
        if verbose
          string = "#{severity} [#{datetime.strftime('%Y-%m-%d %H:%M:%S.%2N')}]: "
        else
          string = "[#{datetime.strftime('%H:%M:%S')}]: "
        end

        string += "#{msg}\n"

        string
      end
      @log
    end

Initialize with default stdout output and verbose false
 Gets the logging object


def require_aether *deps
      @mini_aether_require_spec ||= MiniAether::Spec.new
      @mini_aether_require_resolver ||= MiniAether::Resolver.new

      spec = @mini_aether_require_spec
      resolver = @mini_aether_require_resolver

      if deps.last.kind_of?(Hash)
        hash = deps.pop
        [hash[:source], hash[:sources]].flatten.compact.each do |source|
          spec.source(source)
        end
      end

      deps.each {|coords| spec.jar(coords) }
      resolver.require(spec.dependencies, spec.sources)
      nil
    end

def require_aether * deps @mini_aether_require_spec ||= MiniAether :: Spec . new @mini_aether_require_resolver ||= MiniAether :: Resolver . new spec = @mini_aether_require_spec resolver = @mini_aether_require_resolver if deps . last . kind_of? ( Hash ) hash = deps . pop [ hash [ :source ] , hash [ :sources ] ] . flatten . compact . each do | source | spec . source ( source ) end end deps . each { | coords | spec . jar ( coords ) } resolver . require ( spec . dependencies , spec . sources ) nil end

Experimental 'require_aether' method for use in irb or just for
 convenience.  Not threadsafe.

 @overload require_aether(*coords)
   @param [Array<String>] coords one or more colon-separated maven coordinate strings

 @overload require_aether(*coords, sources)
   @param [Array<String>] coords one or more colon-separated maven coordinate strings
   @param [Hash] sources a hash with a key +:source+ or +:sources+
   and a value of a single string or an array of sources that will be
   permanently added to the list of sources

Experimental require_aether method for use in irb or just for convenience . Not threadsafe .


def create_lookup_table
        File.open(source).each_with_index do |line|
          next if line.chomp.empty?

          values = unstring(line).chomp.split(';')

          next if values.empty?

          @joiners.each do |joiner|
            key = values[joiner.join[0]]
            joiner.lookup[:rows][key] = []

            joiner.cols.each do |i|
              joiner.lookup[:rows][key] << values[i]
            end
          end

        end
      end

def create_lookup_table File . open ( source ) . each_with_index do | line | next if line . chomp . empty? values = unstring ( line ) . chomp . split ( ';' ) next if values . empty? @joiners . each do | joiner | key = values [ joiner . join [ 0 ] ] joiner . lookup [ :rows ] [ key ] = [ ] joiner . cols . each do | i | joiner . lookup [ :rows ] [ key ] << values [ i ] end end end end

Creates a lookup table from the source file values. The join column of
 the source file is the key

Creates a lookup table from the source file values . The join column of the source file is the key


def col_positions(pos, cols)
        if pos.nil? || pos.empty?
          pos = []
          cols.each { |c| pos << Array.new(c.size) { |c| c } }
          pos
        else
          pos.split(';').collect { |p| p.split(',').collect { |p| p.to_i } }
        end
      end

def col_positions ( pos , cols ) if pos . nil? || pos . empty? pos = [ ] cols . each { | c | pos << Array . new ( c . size ) { | c | c } } pos else pos . split ( ';' ) . collect { | p | p . split ( ',' ) . collect { | p | p . to_i } } end end

Initializes the column positions where the source file columns have to
 be inserted. If no column positions are provided the inserted columns
 are put at the beginning of the row

Initializes the column positions where the source file columns have to be inserted . If no column positions are provided the inserted columns are put at the beginning of the row


def create_joiners(j, c, p)
        js = j.split(';').collect { |j| j.split('=').collect { |j| j.to_i } }
        cs = c.split(';').collect { |c| c.split(',').collect { |c| c.to_i } }
        ps = col_positions(p, cs)

        @joiners = []
        (0...js.size).each do |i|
          @joiners << Joiner.new(js[i], ps[i], cs[i], { rows: { } })
        end

        ps.flatten
      end

def create_joiners ( j , c , p ) js = j . split ( ';' ) . collect { | j | j . split ( '=' ) . collect { | j | j . to_i } } cs = c . split ( ';' ) . collect { | c | c . split ( ',' ) . collect { | c | c . to_i } } ps = col_positions ( p , cs ) @joiners = [ ] ( 0 ... js . size ) . each do | i | @joiners << Joiner . new ( js [ i ] , ps [ i ] , cs [ i ] , { rows : { } } ) end ps . flatten end

Initializes joiners based on joins, positions and columns

 Possible input forms are:
 joins:: "4=0;4=1" or "4=1"
 positions:: "1,2;4,5" or "1,2"
 columns:: "1,2;3,4"

 This has the semantic of 'insert columns 1 and 2 at positions 1 and 2
 for key 0 and columns 3 and 4 at positions 4 and 5 for key 1. Key 4 is
 the corresponding value in the source file

 Return value:: positions where to insert values from source file

Initializes joiners based on joins positions and columns


def add_defaults_local
      super
      self.title ||= short_title
      self.short_title ||= title
      self.start_date ||= Java::JavaUtil::Date.new
    end

def add_defaults_local super self . title ||= short_title self . short_title ||= title self . start_date ||= Java :: JavaUtil :: Date . new end

Sets the defaults if necessary. The start date is set to now. The title is
 set to the short title.

Sets the defaults if necessary . The start date is set to now . The title is set to the short title .


def [](*range)
      r, c = range
      r ||= 0..(nrows-1)
      c ||= 0..(ncols-1)

      row_selection = rows.values_at(*r)
      col_selection = []

      if rows_are_arrays?(row_selection)
        row_selection.each do |row|
          values = row.values_at(*c)
          col_selection << (values.respond_to?(:to_ary) ? values : [values])
        end
      else
        col_selection << row_selection[*c]
      end

      SpreadSheet.new(*col_selection,
                      row_labels: row_labels.values_at(*r),
                      col_labels: col_labels.values_at(*c))
    end

def [] ( * range ) r , c = range r ||= 0 .. ( nrows - 1 ) c ||= 0 .. ( ncols - 1 ) row_selection = rows . values_at ( * r ) col_selection = [ ] if rows_are_arrays? ( row_selection ) row_selection . each do | row | values = row . values_at ( * c ) col_selection << ( values . respond_to? ( :to_ary ) ? values : [ values ] ) end else col_selection << row_selection [ * c ] end SpreadSheet . new ( * col_selection , row_labels : row_labels . values_at ( * r ) , col_labels : col_labels . values_at ( * c ) ) end

Returns a subset of the spread sheet and returns a new spread sheet with
 the result and the corresponding row and column labels


def column_collect(&block)
      result = []
      0.upto(ncols-1) { |i| result << block.call(self[nil,i]) }
      result
    end

def column_collect ( & block ) result = [ ] 0 . upto ( ncols - 1 ) { | i | result << block . call ( self [ nil , i ] ) } result end

Collects the operation on each column and returns the result in an array


def rename(opts = {})
      if opts[:rows]
        opts[:rows] = opts[:rows][0,nrows]
        opts[:rows] += row_labels[opts[:rows].size, nrows]
      end

      if opts[:cols]
        opts[:cols] = opts[:cols][0,ncols]
        opts[:cols] += col_labels[opts[:cols].size, ncols]
      end

      @row_labels = opts[:rows] if opts[:rows]
      @col_labels = opts[:cols] if opts[:cols]
    end

def rename ( opts = { } ) if opts [ :rows ] opts [ :rows ] = opts [ :rows ] [ 0 , nrows ] opts [ :rows ] += row_labels [ opts [ :rows ] . size , nrows ] end if opts [ :cols ] opts [ :cols ] = opts [ :cols ] [ 0 , ncols ] opts [ :cols ] += col_labels [ opts [ :cols ] . size , ncols ] end @row_labels = opts [ :rows ] if opts [ :rows ] @col_labels = opts [ :cols ] if opts [ :cols ] end

Renames the row and column labels

   sheet.rename(rows: ['Row 1', 'Row 2'], cols: ['Col 1', 'Col 2'])

 If the provided rows and columns are larger than the spread sheet's rows
 and columns then only the respective row and column values are used. If
 the row and column labels are fewer than the respective row and column
 sizes the old labels are left untouched for the missing new labels


def to_s
      col_label_sizes = col_labels.collect { |c| c.to_s.size + 2 }
      row_label_size = row_labels.collect { |r| r.to_s.size + 2 }.max

      row_col_sizes = rows.transpose.collect { |r| r.collect { |c| c.to_s.size } }

      i = -1
      col_sizes = col_label_sizes.collect do |s|
        i += 1
        [row_col_sizes[i],s].flatten.max + 1
      end

      s = (sprintf("%#{row_label_size}s", " "))
      col_labels.each_with_index { |l,i| s << (sprintf("%#{col_sizes[i]}s",
                                                       "[#{l}]"))           }
      s << "\n"

      rows.each_with_index do |row, i|
        s << (sprintf("%#{row_label_size}s", "[#{row_labels[i]}]"))
        row.each_with_index { |c,j| s << (sprintf("%#{col_sizes[j]}s", c)) }
        s << "\n"
      end

      s
    end

def
to_s
col_label_sizes
=
col_labels
.
collect
{
|
c
|
c
.
to_s
.
size
+
2
}
row_label_size
=
row_labels
.
collect
{
|
r
|
r
.
to_s
.
size
+
2
}
.
max
row_col_sizes
=
rows
.
transpose
.
collect
{
|
r
|
r
.
collect
{
|
c
|
c
.
to_s
.
size
}
}
i
=
1
col_sizes
=
col_label_sizes
.
collect
do
|
s
|
i
+=
1
[
row_col_sizes
[
i
]
,
s
]
.
flatten
.
max
+
1
end
s
=
(
sprintf
(
"%#{row_label_size}s"
,
" "
)
)
col_labels
.
each_with_index
{
|
l
,
i
|
s
<<
(
sprintf
(
"%#{col_sizes[i]}s"
,
"[#{l}]"
)
)
}
s
<<
"\n"
rows
.
each_with_index
do
|
row
,
i
|
s
<<
(
sprintf
(
"%#{row_label_size}s"
,
"[#{row_labels[i]}]"
)
)
row
.
each_with_index
{
|
c
,
j
|
s
<<
(
sprintf
(
"%#{col_sizes[j]}s"
,
c
)
)
}
s
<<
"\n"
end
s
end

Prints the spread sheet in a matrix with column labels and row labels. If
 no labels are available the column number and row number is printed

Prints the spread sheet in a matrix with column labels and row labels . If no labels are available the column number and row number is printed


def rows_from_params(opts)
        col_count = opts[:cols]
        row_count = opts[:rows]

        size = row_count * col_count if row_count && col_count

        rows = []

        if values = opts[:values]
          if size
            values += [NotAvailable] * (size - values.size)
          elsif col_count
            values += [NotAvailable] * ((col_count - values.size) % col_count)
          elsif row_count
            values += [NotAvailable] * ((row_count - values.size) % row_count)
            col_count = values.size / row_count
          else
            col_count = Math.sqrt(values.size).ceil
            values += [NotAvailable] * ((col_count - values.size) % col_count)
          end
          values.each_slice(col_count) { |row| rows << row }
        elsif opts[:file]
          File.foreach(opts[:file]) do |line|
            next if line.chomp.empty?
            values = line.split(SEMICOLON) rescue str2utf8(line).split(SEMICOLON)
            rows << values.collect { |v|
              v.strip.empty? ? NotAvailable : str2num(v.chomp, opts[:ds])
            }
          end
        end

        rows
      end

def rows_from_params ( opts ) col_count = opts [ :cols ] row_count = opts [ :rows ] size = row_count * col_count if row_count && col_count rows = [ ] if values = opts [ :values ] if size values += [ NotAvailable ] * ( size - values . size ) elsif col_count values += [ NotAvailable ] * ( ( col_count - values . size ) % col_count ) elsif row_count values += [ NotAvailable ] * ( ( row_count - values . size ) % row_count ) col_count = values . size / row_count else col_count = Math . sqrt ( values . size ) . ceil values += [ NotAvailable ] * ( ( col_count - values . size ) % col_count ) end values . each_slice ( col_count ) { | row | rows << row } elsif opts [ :file ] File . foreach ( opts [ :file ] ) do | line | next if line . chomp . empty? values = line . split ( SEMICOLON ) rescue str2utf8 ( line ) . split ( SEMICOLON ) rows << values . collect { | v | v . strip . empty? ? NotAvailable : str2num ( v . chomp , opts [ :ds ] ) } end end rows end

Creates rows from provided array or file. If array doesn't provide
 equal column sizes the array is extended with NotAvailable values

Creates rows from provided array or file . If array doesn t provide equal column sizes the array is extended with NotAvailable values


def equalize_rows(rows)
        column_sizes = rows.collect { |r| r.size }

        return rows if column_sizes.uniq.size == 1

        max_size = column_sizes.max
        small_rows = []
        column_sizes.each_with_index { |c,i| small_rows << i if c < max_size }

        small_rows.each do |i|
          rows[i] += [NotAvailable] * (max_size - rows[i].size)
        end

        rows
      end

def equalize_rows ( rows ) column_sizes = rows . collect { | r | r . size } return rows if column_sizes . uniq . size == 1 max_size = column_sizes . max small_rows = [ ] column_sizes . each_with_index { | c , i | small_rows << i if c < max_size } small_rows . each do | i | rows [ i ] += [ NotAvailable ] * ( max_size - rows [ i ] . size ) end rows end

If rows are of different column size the rows are equalized in column
 size by filling missing columns with NA


def same_column_size?(rows)
        offset = opts[:c] ? 1 : 0
        return true if rows.size == 1 + offset
        (0 + offset).upto(rows.size - 2) do |i|
          return false unless rows[i].size == rows[i+1].size
        end
        true
      end

def same_column_size? ( rows ) offset = opts [ :c ] ? 1 : 0 return true if rows . size == 1 + offset ( 0 + offset ) . upto ( rows . size - 2 ) do | i | return false unless rows [ i ] . size == rows [ i + 1 ] . size end true end

Checks whether all rows have the same column size. Returns true if
 all columns have the same column size

Checks whether all rows have the same column size . Returns true if all columns have the same column size


def coerce(value)
        return SpreadSheet.new([value]) if value.is_a?(Numeric)
        return SpreadSheet.new(value)   if value.is_a?(Array)
      end

def coerce ( value ) return SpreadSheet . new ( [ value ] ) if value . is_a? ( Numeric ) return SpreadSheet . new ( value ) if value . is_a? ( Array ) end

Coerces a number or an array to a spread sheet


def process(operator, s)
        s = coerce(s) || s
        raise "operand needs to be a SpreadSheet, "+
              "Numeric or Array" unless s.is_a?(SpreadSheet)
        result = []
        rlabel = []
        clabel = []
        s1_row_count, s1_col_count = dim
        s2_row_count, s2_col_count = s.dim
        row_count = [s1_row_count, s2_row_count].max
        col_count = [s1_col_count, s2_col_count].max
        0.upto(row_count - 1) do |r|
          r1 = r % s1_row_count
          r2 = r % s2_row_count
          rlabel << "#{row_labels[r1]}#{operator}#{s.row_labels[r2]}"
          element = []
          0.upto(col_count - 1) do |c|
            c1 = c % s1_col_count
            c2 = c % s2_col_count
            clabel << "#{col_labels[c1]}#{operator}#{s.col_labels[c2]}"
            element << rows[r1][c1].send(operator, s.rows[r2][c2])
          end
          result << element
        end
        SpreadSheet.new(*result, row_labels: rlabel, col_labels: clabel)
      end

Conducts the calculation of this spread sheet with the provided value
 based on the operator. It s is a number or an array it is coerced into
 a spread sheet

Conducts the calculation of this spread sheet with the provided value based on the operator . It s is a number or an array it is coerced into a spread sheet


def process_count
      File.new(infile).each_with_index do |line, index|
        result = col_filter.process(row_filter.process(line.chomp, row: index))
        unless result.nil? or result.empty?
          key = unstring(line).split(';').values_at(*key_columns)
          key_value = key_values[key] || key_values[key] = { name: key,
                                                             elements: Hash.new(0),
                                                             sum: 0 }
          result.chomp.split(';').each do |column|
            heading << column if heading.index(column).nil?
            key_value[:elements][column] += 1
            key_value[:sum] += 1
            sums[column] += 1
          end
        end
      end
    end

def process_count File . new ( infile ) . each_with_index do | line , index | result = col_filter . process ( row_filter . process ( line . chomp , row : index ) ) unless result . nil? or result . empty? key = unstring ( line ) . split ( ';' ) . values_at ( * key_columns ) key_value = key_values [ key ] || key_values [ key ] = { name : key , elements : Hash . new ( 0 ) , sum : 0 } result . chomp . split ( ';' ) . each do | column | heading << column if heading . index ( column ) . nil? key_value [ :elements ] [ column ] += 1 key_value [ :sum ] += 1 sums [ column ] += 1 end end end end


def write_result
      sum_line = [sum_row_title] + [''] * (key_titles.size - 1)
      headline = heading_sort ? heading.sort : original_pivot_sequence_heading
      headline << add_sum_col unless sum_col_title.nil?
      headline.each do |h|
        sum_line << sums[h]
      end
      row = 0;
      File.open(outfile, 'w') do |out|
        out.puts sum_line.join(';') if row == sum_row ; row += 1
        out.puts (key_titles + headline).join(';')
        key_values.each do |k,v|
          out.puts sum_line.join(';') if row == sum_row ; row += 1
          line = [k]
          headline.each do |h|
            line << v[:elements][h] unless h == sum_col_title
          end
          line << v[:sum] unless sum_col_title.nil?
          out.puts line.join(';')
        end
      end
    end

def write_result sum_line = [ sum_row_title ] + [ '' ] * ( key_titles . size - 1 ) headline = heading_sort ? heading . sort : original_pivot_sequence_heading headline << add_sum_col unless sum_col_title . nil? headline . each do | h | sum_line << sums [ h ] end row = 0 ; File . open ( outfile , 'w' ) do | out | out . puts sum_line . join ( ';' ) if row == sum_row ; row += 1 out . puts ( key_titles + headline ) . join ( ';' ) key_values . each do | k , v | out . puts sum_line . join ( ';' ) if row == sum_row ; row += 1 line = [ k ] headline . each do | h | line << v [ :elements ] [ h ] unless h == sum_col_title end line << v [ :sum ] unless sum_col_title . nil? out . puts line . join ( ';' ) end end end


def init_sum_scheme(sum_scheme)

        return if sum_scheme.nil?

        re = /(\w+):(\d+)|(\w+)/

        sum_scheme.scan(re).each do |part|
          if part.compact.size == 2
            @sum_row_title = part[0]
            @sum_row       = part[1].to_i
          else
            @sum_col_title = part[2]
          end
        end

      end

def init_sum_scheme ( sum_scheme ) return if sum_scheme . nil? re = / \w \d \w / sum_scheme . scan ( re ) . each do | part | if part . compact . size == 2 @sum_row_title = part [ 0 ] @sum_row = part [ 1 ] . to_i else @sum_col_title = part [ 2 ] end end end

Initializes the sum row title an positions as well as the sum column
 title


def init_key_columns(key_scheme)

        @key_titles  = []
        @key_columns = []

        keys = key_scheme.scan(/(\d+):(\w+)/)

        keys.each do |key|
          @key_titles  << key[1]
          @key_columns << key[0].to_i
        end

      end

def init_key_columns ( key_scheme ) @key_titles = [ ] @key_columns = [ ] keys = key_scheme . scan ( / \d \w / ) keys . each do | key | @key_titles << key [ 1 ] @key_columns << key [ 0 ] . to_i end end


def original_pivot_sequence_heading
        (heading.sort - col_filter.pivot.keys << col_filter.pivot.keys).flatten
      end

def original_pivot_sequence_heading ( heading . sort - col_filter . pivot . keys << col_filter . pivot . keys ) . flatten end

Arrange heading in the original sequence regarding conditional column
 filters


def process(object, options={})
      object = unstring(object)
      return object unless has_filter?
      filtered = !filter.flatten.uniq.index(options[:row]).nil?
      pattern.each do |p|
        filtered = (filtered or !(object =~ Regexp.new(p)).nil?)
      end
      filtered = (filtered or match_boolean_filter?(object.split(';')))
      filtered ? object : nil
    end

def process ( object , options = { } ) object = unstring ( object ) return object unless has_filter? filtered = ! filter . flatten . uniq . index ( options [ :row ] ) . nil? pattern . each do | p | filtered = ( filtered or ! ( object =~ Regexp . new ( p ) ) . nil? ) end filtered = ( filtered or match_boolean_filter? ( object . split ( ';' ) ) ) filtered ? object : nil end


def messages
      data = api('pm.history', :receiverid => id)
      data['history'].map {|attrs| Message.new(client, attrs)}
    end

def messages data = api ( 'pm.history' , :receiverid => id ) data [ 'history' ] . map { | attrs | Message . new ( client , attrs ) } end

Gets the private conversation history with this user.

 @return [Array<Turntabler::Message>]
 @raise [Turntabler::Error] if the command fails
 @example
   user.messages   # => [#<Turntabler::Message ...>, ...]

Gets the private conversation history with this user .


def stalk
      become_fan unless client.user.fan_of.include?(self)
      client.rooms.with_friends.detect do |room|
        room.listener(id)
      end
    end

def stalk become_fan unless client . user . fan_of . include? ( self ) client . rooms . with_friends . detect do | room | room . listener ( id ) end end

Gets the location of the user.

 @note This will make the current user a fan of this user
 @return [Turntabler::Room]
 @raise [Turntabler::Error] if the command fails
 @example
   user.stalk    # => #<Turntabler::Room ...>


def boot(reason = '')
      api('room.boot_user', :roomid => room.id, :section => room.section, :target_userid => id, :reason => reason)
      true
    end

def boot ( reason = '' ) api ( 'room.boot_user' , :roomid => room . id , :section => room . section , :target_userid => id , :reason => reason ) true end

Boots the user for the specified reason.

 @param [String] reason The reason why the user is being booted
 @return [true]
 @raise [Turntabler::Error] if the command fails
 @example
   user.boot('Broke rules')    # => true

Boots the user for the specified reason .


def report(reason = '')
      api('room.report_user', :roomid => room.id, :section => room.section, :reported => id, :reason => reason)
      true
    end

def report ( reason = '' ) api ( 'room.report_user' , :roomid => room . id , :section => room . section , :reported => id , :reason => reason ) true end

Reports abuse by a user.

 @param [String] reason The reason the user is being reported
 @return [true]
 @raise [Turntabler::Error] if the command fails
 @example
   user.report('Verbal abuse ...')  # => true


def default_derived_characteristics
      chrs = specimen_characteristics || return
      pas = chrs.class.nondomain_attributes.reject { |pa| pa == :identifier }
      chrs.copy(pas)
    end

def default_derived_characteristics chrs = specimen_characteristics || return pas = chrs . class . nondomain_attributes . reject { | pa | pa == :identifier } chrs . copy ( pas ) end

Returns characteristics to use for a derived specimen. The new characteristics is copied from this
 parent specimen's characteristics, without the identifier.

 @return [CaTissue::SpecimenCharacteristics, nil] a copy of this Specimen's specimen_characteristics, or nil if none

Returns characteristics to use for a derived specimen . The new characteristics is copied from this parent specimen s characteristics without the identifier .


def region=(args)
      case args
        when Hash
          self.setRegion(CoordinateRegion.new(args[:region]).api, animated: args[:animated])
        else
          self.setRegion(CoordinateRegion.new(args).api, animated: false)
      end
    end

def region = ( args ) case args when Hash self . setRegion ( CoordinateRegion . new ( args [ :region ] ) . api , animated : args [ :animated ] ) else self . setRegion ( CoordinateRegion . new ( args ) . api , animated : false ) end end

Set the maps region

 * *Args*    :
    region = CoordinateRegion.new([56, 10.6], [3.1, 3.1])
    region = {:region => CoordinateRegion.new([56, 10.6], [3.1, 3.1]), :animated => false}


def collection_site
      return unless medical_identifiers.size == 1
      site = medical_identifiers.first.site
      return if site.nil?
      site.site_type == Site::SiteType::COLLECTION ? site : nil
    end

def collection_site return unless medical_identifiers . size == 1 site = medical_identifiers . first . site return if site . nil? site . site_type == Site :: SiteType :: COLLECTION ? site : nil end

Returns the collection site for which this participant has a MRN. If there is not exactly one
 such site, then this method returns nil. This method is a convenience for the common situation
 where a participant is enrolled at one site.

 @return [CaTissue::Site] the collection site

Returns the collection site for which this participant has a MRN . If there is not exactly one such site then this method returns nil . This method is a convenience for the common situation where a participant is enrolled at one site .


def match_characteristics(other)
      chr = characteristics
      ochr = other.characteristics
      chr and ochr and chr.tissue_side == ochr.tissue_side and chr.tissue_site == ochr.tissue_site
    end

def match_characteristics ( other ) chr = characteristics ochr = other . characteristics chr and ochr and chr . tissue_side == ochr . tissue_side and chr . tissue_site == ochr . tissue_site end

Returns whether this SpecimenRequirement characteristics matches the other SpecimenRequirement characteristics
 on the tissue site and tissue side.

Returns whether this SpecimenRequirement characteristics matches the other SpecimenRequirement characteristics on the tissue site and tissue side .


def default_user
      scg = specimen_collection_group || (specimen.specimen_collection_group if specimen) || return
      cp = scg.collection_protocol || return
      cp.coordinators.first || (cp.sites.first.coordinator if cp.sites.size === 1)
    end

def default_user scg = specimen_collection_group || ( specimen . specimen_collection_group if specimen ) || return cp = scg . collection_protocol || return cp . coordinators . first || ( cp . sites . first . coordinator if cp . sites . size === 1 ) end

Returns the first SCG CP coordinator, if any.

Returns the first SCG CP coordinator if any .


def export!
      puts "Exporting translations:\n"
      if config[:split]
        translations.keys.each do |locale|
          if translations[:en].nil?
            puts 'Missing english translation'
            exit
          end
          puts "\nLocale: #{locale}"
          fallback_english_hash = flat_hash(translations[:en])
          translations_hash = flat_hash(translations[locale])
          if locale != :en
            translations_hash.each do |key, value|
              english_fallback = fallback_english_hash[key]
              if value == nil || value == ""
                puts "  #{key} missing!"
                puts "     taking english default: '#{english_fallback}'"
                translations_hash[key] = english_fallback
              end
            end
          end
          save(translations_hash, File.join(export_dir, "translations_#{locale}.js"))
        end
      else
        save(flat_hash(translations), File.join(export_dir, 'translations.js'))
      end
    end

Export translations to JavaScript, considering settings
 from configuration file

Export translations to JavaScript considering settings from configuration file


def save(translations, file)
      file = ::Rails.root.join(file)
      FileUtils.mkdir_p File.dirname(file)

      variable_to_assign = config.fetch(:variable, 'Ember.I18n.translations')

      File.open(file, 'w+') do |f|
        f << variable_to_assign
        f << ' = '
        f << JSON.pretty_generate(translations).html_safe
        f << ';'
      end
    end

def
save
(
translations
,
file
)
file
=
::
Rails
.
root
.
join
(
file
)
FileUtils
.
mkdir_p
File
.
dirname
(
file
)
variable_to_assign
=
config
.
fetch
(
:variable
,
'Ember.I18n.translations'
)
File
.
open
(
file
,
'w+'
)
do
|
f
|
f
<<
variable_to_assign
f
<<
' = '
f
<<
JSON
.
pretty_generate
(
translations
)
.
html_safe
f
<<
';'
end
end

Convert translations to JSON string and save file.

Convert translations to JSON string and save file .


def translations
      ::I18n.load_path = default_locales_path
      ::I18n.backend.instance_eval do
        init_translations unless initialized?
        translations
      end
    end

def translations :: I18n . load_path = default_locales_path :: I18n . backend . instance_eval do init_translations unless initialized? translations end end


def load_properties
      # the properties file
      file = default_properties_file
      # the access properties
      props = file && File.exists?(file) ? load_properties_file(file) : {}
      # Load the Java application jar path.
      path = props[:classpath] || props[:path] || infer_classpath
      Java.expand_to_class_path(path) if path
      # Get the application login properties from the remoteService.xml, if necessary.
      unless props.has_key?(:host) or props.has_key?(:port) then
        url = remote_service_url
        if url then
          host, port = url.split(':')
          props[:host] = host
          props[:port] = port
        end
      end
      unless props.has_key?(:database) then
        props.merge(infer_database_properties)
      end
      props
    end

def load_properties file = default_properties_file props = file && File . exists? ( file ) ? load_properties_file ( file ) : { } path = props [ :classpath ] || props [ :path ] || infer_classpath Java . expand_to_class_path ( path ) if path unless props . has_key? ( :host ) or props . has_key? ( :port ) then url = remote_service_url if url then host , port = url . split ( ':' ) props [ :host ] = host props [ :port ] = port end end unless props . has_key? ( :database ) then props . merge ( infer_database_properties ) end props end

Loads the caTissue classpath and connection properties.

Loads the caTissue classpath and connection properties .


def parse
      download_files

      parsing_clients = false
      parsing_prefile = false
      parsing_general = false
      parsing_servers = false
      parsing_voice_servers = false

      File.open(DATA_FILE_PATH, 'r:ascii-8bit').each { |line|

        if line.start_with? ";"
          parsing_clients = false
          parsing_prefile = false
          parsing_general = false
          parsing_servers = false
          parsing_voice_servers = false
        elsif parsing_clients
          clienttype = line.split(":")[3]
          if clienttype.eql? "PILOT"
            @pilots << Pilot.new(line)
          elsif clienttype.eql? "ATC"
            @atc << ATC.new(line)
          end
        elsif parsing_prefile
          @prefiles << Prefile.new(line)
        elsif parsing_general
          line_split = line.split("=")
          @general[line_split[0].strip.downcase.gsub(" ", "_")] = line_split[1].strip
        elsif parsing_servers
          @servers << Server.new(line)
        elsif parsing_voice_servers
          @voice_servers << VoiceServer.new(line) if line.length > 2 # ignore last, empty line for voice server that contains 2 characters
        end

        parsing_clients = true if line.start_with? "!CLIENTS:"
        parsing_prefile = true if line.start_with? "!PREFILE:"
        parsing_general = true if line.start_with? "!GENERAL:"
        parsing_servers = true if line.start_with? "!SERVERS:"
        parsing_voice_servers = true if line.start_with? "!VOICE SERVERS:"
      }
    end

def
parse
download_files
parsing_clients
=
false
parsing_prefile
=
false
parsing_general
=
false
parsing_servers
=
false
parsing_voice_servers
=
false
File
.
open
(
DATA_FILE_PATH
,
'r:ascii-8bit'
)
.
each
{
|
line
|
if
line
.
start_with?
";"
parsing_clients
=
false
parsing_prefile
=
false
parsing_general
=
false
parsing_servers
=
false
parsing_voice_servers
=
false
elsif
parsing_clients
clienttype
=
line
.
split
(
":"
)
[
3
]
if
clienttype
.
eql?
"PILOT"
@pilots
<<
Pilot
.
new
(
line
)
elsif
clienttype
.
eql?
"ATC"
@atc
<<
ATC
.
new
(
line
)
end
elsif
parsing_prefile
@prefiles
<<
Prefile
.
new
(
line
)
elsif
parsing_general
line_split
=
line
.
split
(
"="
)
@general
[
line_split
[
0
]
.
strip
.
downcase
.
gsub
(
" "
,
"_"
)
]
=
line_split
[
1
]
.
strip
elsif
parsing_servers
@servers
<<
Server
.
new
(
line
)
elsif
parsing_voice_servers
@voice_servers
<<
VoiceServer
.
new
(
line
)
if
line
.
length
>
2
end
parsing_clients
=
true
if
line
.
start_with?
"!CLIENTS:"
parsing_prefile
=
true
if
line
.
start_with?
"!PREFILE:"
parsing_general
=
true
if
line
.
start_with?
"!GENERAL:"
parsing_servers
=
true
if
line
.
start_with?
"!SERVERS:"
parsing_voice_servers
=
true
if
line
.
start_with?
"!VOICE SERVERS:"
}
end

Parse the vatsim data file and store output as necessary


def download_files
      if !File.exists?(STATUS_FILE_PATH) or File.mtime(STATUS_FILE_PATH) < Time.now - STATUS_DOWNLOAD_INTERVAL
       download_to_file STATUS_URL, STATUS_FILE_PATH
      end

      if !File.exists?(DATA_FILE_PATH) or File.mtime(DATA_FILE_PATH) < Time.now - DATA_DOWNLOAD_INTERVAL
        download_to_file random_data_url, DATA_FILE_PATH
      end
    end

def download_files if ! File . exists? ( STATUS_FILE_PATH ) or File . mtime ( STATUS_FILE_PATH ) < Time . now - STATUS_DOWNLOAD_INTERVAL download_to_file STATUS_URL , STATUS_FILE_PATH end if ! File . exists? ( DATA_FILE_PATH ) or File . mtime ( DATA_FILE_PATH ) < Time . now - DATA_DOWNLOAD_INTERVAL download_to_file random_data_url , DATA_FILE_PATH end end

Initialize the system by downloading status and vatsim data files


def download_to_file url, file
      url = URI.parse(URI.encode(url.strip))

      File.new(file, File::CREAT)

      Net::HTTP.start(url.host) { |http|
        resp = http.get(url.path)
        open(file, "wb") { |file|
          file.write(resp.body)
        }
      }
    end

def download_to_file url , file url = URI . parse ( URI . encode ( url . strip ) ) File . new ( file , File :: CREAT ) Net :: HTTP . start ( url . host ) { | http | resp = http . get ( url . path ) open ( file , "wb" ) { | file | file . write ( resp . body ) } } end


def random_data_url
      url0s = Array.new
      file = File.open(STATUS_FILE_PATH)
      file.each {|line|
        if line.start_with? "url0"
          url0s << line.split("=").last
        end
      }
      return url0s[rand(url0s.length)]
     end

def random_data_url url0s = Array . new file = File . open ( STATUS_FILE_PATH ) file . each { | line | if line . start_with? "url0" url0s << line . split ( "=" ) . last end } return url0s [ rand ( url0s . length ) ] end

Return random vatsim data url from status file


def cachier!(var = nil)
      if var && instance_variable_get("@#{var}")
        # => Clear the Single Variable
        remove_instance_variable("@#{var}")
      else
        # => Clear the Whole Damned Cache
        instance_variables.each { |x| remove_instance_variable(x) }
      end
    end

def cachier! ( var = nil ) if var && instance_variable_get ( "@#{var}" ) remove_instance_variable ( "@#{var}" ) else instance_variables . each { | x | remove_instance_variable ( x ) } end end


def write_json(file, object)
      return unless file && object
      begin
        File.open(file, 'w') { |f| f.write(JSON.pretty_generate(object)) }
      end
    end

def write_json ( file , object ) return unless file && object begin File . open ( file , 'w' ) { | f | f . write ( JSON . pretty_generate ( object ) ) } end end


def filestring(file, size = 8192)
      return unless file
      return file unless file.is_a?(String) && File.file?(file) && File.size(file) <= size
      File.read(file)
    end

def filestring ( file , size = 8192 ) return unless file return file unless file . is_a? ( String ) && File . file? ( file ) && File . size ( file ) <= size File . read ( file ) end

=> Check if a string is an existing file, and return it's content

= > Check if a string is an existing file and return it s content


def common_array(ary) # rubocop: disable AbcSize
      return ary unless ary.is_a? Array
      count = ary.count
      return ary if count.zero?
      return ary.flatten.uniq if count == 1
      common = ary[0] & ary[1]
      return common if count == 2
      (count - 2).times { |x| common &= ary[x + 2] } if count > 2
      common
    end

def common_array ( ary ) return ary unless ary . is_a? Array count = ary . count return ary if count . zero? return ary . flatten . uniq if count == 1 common = ary [ 0 ] & ary [ 1 ] return common if count == 2 ( count - 2 ) . times { | x | common &= ary [ x + 2 ] } if count > 2 common end


def run
      bind_queues and return if test_env?
      start_shutdown_thread
      start_error_counter_thread
      client.on_exception do |e|
        count_error(e)
      end
      client.connect
      start_event_thread

      puts 'EventListener started. Hit Ctrl-C to exit'
      Thread.stop
      puts 'Main thread wakeup - exiting.'
      client.close
    end

Initialize a new EventListener daemon instance
 @param [Hash] options daemon options
 @option options [Integer] :error_limit (10) maximum number of errors that are allowed to occur within error_interval
   before the process terminates
 @option options [Integer] :error_interval (3600) time interval, in seconds, before the error counter is cleared
 @option options [String] :events_exchange ('events') name of AMQP exchange used for listening to event messages
 @option options [String] :models_exchange ('models') name of AMQP exchange used for listening to EventObserver
   instance create, update, and destroy messages
 @option options [String] :name ("#{Rails.application.class.parent_name.downcase}-listener") daemon instance name.
 Run the daemon process. This creates the event, error counter, and shutdown threads


def register_observer_class(observer_class)
      @observer_mutex.synchronize do
        return if @observed_models.include?(observer_class)
        @observed_models << observer_class
        log "EventListener: registering observer class #{observer_class}"
        observer_queue.bind(models_exchange, routing_key: "#{Isimud.model_watcher_schema}.#{observer_class.base_class.name}.*")
      end
    end


def register_observer(observer)
      @observer_mutex.synchronize do
        log "EventListener: registering observer #{observer.class} #{observer.id}"
        @observers[observer_key_for(observer.class, observer.id)] = observer.observe_events(client)
      end
    end

Register an observer instance, and start listening for events on its associated queue.
 Also ensure that we are listening for observer class update events

Register an observer instance and start listening for events on its associated queue . Also ensure that we are listening for observer class update events


def unregister_observer(observer_class, observer_id)
      @observer_mutex.synchronize do
        log "EventListener: un-registering observer #{observer_class} #{observer_id}"
        if (consumer = @observers.delete(observer_key_for(observer_class, observer_id)))
          consumer.cancel
        end
      end
    end

Unregister an observer instance, and cancel consumption of messages. Any pre-fetched messages will be returned to the queue.

Unregister an observer instance and cancel consumption of messages . Any pre - fetched messages will be returned to the queue .


def observer_queue
      @observer_queue ||= client.create_queue([name, 'listener', Socket.gethostname, Process.pid].join('.'),
                                              models_exchange,
                                              queue_options:     {exclusive: true},
                                              subscribe_options: {manual_ack: true})
    end

def observer_queue @observer_queue ||= client . create_queue ( [ name , 'listener' , Socket . gethostname , Process . pid ] . join ( '.' ) , models_exchange , queue_options : { exclusive : true } , subscribe_options : { manual_ack : true } ) end

Create or return the observer queue which listens for ModelWatcher events


def as_json(options = {})
      session_id = parameters.delete(:session_id) || Thread.current[:keas_session_id]

      data = {type:          type,
              action:        action,
              user_id:       user_id,
              occurred_at:   occurred_at,
              eventful_type: eventful_type,
              eventful_id:   eventful_id,
              session_id:    session_id}
      unless options[:omit_parameters]
        data[:parameters] = parameters
        data[:attributes] = attributes
      end
      data
    end

def as_json ( options = { } ) session_id = parameters . delete ( :session_id ) || Thread . current [ :keas_session_id ] data = { type : type , action : action , user_id : user_id , occurred_at : occurred_at , eventful_type : eventful_type , eventful_id : eventful_id , session_id : session_id } unless options [ :omit_parameters ] data [ :parameters ] = parameters data [ :attributes ] = attributes end data end

Return hash of data to be serialized to JSON
 @option options [Boolean] :omit_parameters when set, do not include attributes or parameters in data
 @return [Hash] data to serialize


def do_create(action)
      options = do_creation_options
      options.title ||= @params.first

      @helper.send("#{action}_usage") unless options.title
      fn = @commands.send(action, options)
      puts "#{@src_dir}/#{fn}"
    end

def do_create ( action ) options = do_creation_options options . title ||= @params . first @helper . send ( "#{action}_usage" ) unless options . title fn = @commands . send ( action , options ) puts "#{@src_dir}/#{fn}" end

action is a string, either 'post' or 'draft'


def do_move(action)
      options = do_move_options(action)
      path = @params.first

      @helper.send("#{action}_usage") unless path
      fn = @commands.send(action, path, options)
      puts "#{@src_dir}/#{fn}"
    end

def do_move ( action ) options = do_move_options ( action ) path = @params . first @helper . send ( "#{action}_usage" ) unless path fn = @commands . send ( action , path , options ) puts "#{@src_dir}/#{fn}" end

action is a string, either 'publish' or 'unpublish'

action is a string either publish or unpublish


def do_move_options(type)
      options = OpenStruct.new
      opt_parser = OptionParser.new do |opts|
        if type == 'publish'
          opts.on('-d', '--keep-draft', "Keep draft post") do |d|
            options.keep_draft = d
          end
        else
          opts.on('-p', '--keep-post', "Do not delete post") do |p|
            options.keep_post = p
          end
        end

        opts.on('-t', '--keep-timestamp', "Keep existing timestamp") do |t|
          options.keep_timestamp = t
        end
      end

      opt_parser.parse! @params
      options
    end

def
do_move_options
(
type
)
options
=
OpenStruct
.
new
opt_parser
=
OptionParser
.
new
do
|
opts
|
if
type
==
'publish'
opts
.
on
(
'-d'
,
'--keep-draft'
,
"Keep draft post"
)
do
|
d
|
options
.
keep_draft
=
d
end
else
opts
.
on
(
'-p'
,
'--keep-post'
,
"Do not delete post"
)
do
|
p
|
options
.
keep_post
=
p
end
end
opts
.
on
(
'-t'
,
'--keep-timestamp'
,
"Keep existing timestamp"
)
do
|
t
|
options
.
keep_timestamp
=
t
end
end
opt_parser
.
parse!
@params
options
end

pass a string, either publish or unpublish

pass a string either publish or unpublish


def resolve_links(text)
      html = Nokogiri::HTML.fragment(text)
      html.css("a[href]").each do |link|
        href = URI(link["href"])
        next unless href.relative? && markup_for_file(nil, href.path) == :markdown
        link.replace "{file:#{href} #{link.inner_html}}"
      end
      super(html.to_s)
    end

def
resolve_links
(
text
)
html
=
Nokogiri
::
HTML
.
fragment
(
text
)
html
.
css
(
"a[href]"
)
.
each
do
|
link
|
href
=
URI
(
link
[
"href"
]
)
next
unless
href
.
relative?
&&
markup_for_file
(
,
href
.
path
)
==
:markdown
link
.
replace
"{file:#{href} #{link.inner_html}}"
end
super
(
html
.
to_s
)
end

Resolves relative links to Markdown files.
 @param [String] text the HTML fragment in which to resolve links.
 @return [String] HTML with relative links to Markdown files converted to `{file:}` links.

Resolves relative links to Markdown files .


def nr_api
      # => Build the Faraday Connection
      @conn ||= Faraday::Connection.new('https://api.newrelic.com', conn_opts) do |client|
        client.use Faraday::Response::RaiseError
        client.use FaradayMiddleware::EncodeJson
        client.use FaradayMiddleware::ParseJson, content_type: /\bjson$/
        client.response :logger if Config.environment.to_s.casecmp('development').zero? # => Log Requests to STDOUT
        client.adapter Faraday.default_adapter #:net_http_persistent
      end
    end

def nr_api @conn ||= Faraday :: Connection . new ( 'https://api.newrelic.com' , conn_opts ) do | client | client . use Faraday :: Response :: RaiseError client . use FaradayMiddleware :: EncodeJson client . use FaradayMiddleware :: ParseJson , content_type : / \b / client . response :logger if Config . environment . to_s . casecmp ( 'development' ) . zero? client . adapter Faraday . default_adapter end end


def alert_add_entity(entity_id, condition_id, entity_type = 'Server')
      nr_api.put do |req|
        req.url url('alerts_entity_conditions', entity_id)
        req.params['entity_type'] = entity_type
        req.params['condition_id'] = condition_id
      end
    end

def alert_add_entity ( entity_id , condition_id , entity_type = 'Server' ) nr_api . put do | req | req . url url ( 'alerts_entity_conditions' , entity_id ) req . params [ 'entity_type' ] = entity_type req . params [ 'condition_id' ] = condition_id end end

=> Add an Entitity to an Existing Alert Policy

= > Add an Entitity to an Existing Alert Policy


def alert_delete_entity(entity_id, condition_id, entity_type = 'Server')
      nr_api.delete do |req|
        req.url url('alerts_entity_conditions', entity_id)
        req.params['entity_type'] = entity_type
        req.params['condition_id'] = condition_id
      end
    end

def alert_delete_entity ( entity_id , condition_id , entity_type = 'Server' ) nr_api . delete do | req | req . url url ( 'alerts_entity_conditions' , entity_id ) req . params [ 'entity_type' ] = entity_type req . params [ 'condition_id' ] = condition_id end end

=> Delete an Entitity from an Existing Alert Policy

= > Delete an Entitity from an Existing Alert Policy


def get_server_id(server_id)
      return nil unless server_id =~ /^[0-9]+$/
      ret = nr_api.get(url('servers', server_id)).body
      ret['server']
    rescue Faraday::ResourceNotFound, NoMethodError
      nil
    end

def get_server_id ( server_id ) return nil unless server_id =~ / / ret = nr_api . get ( url ( 'servers' , server_id ) ) . body ret [ 'server' ] rescue Faraday :: ResourceNotFound , NoMethodError nil end


def get_server_name(server, exact = true)
      ret = nr_api.get(url('servers'), 'filter[name]' => server).body
      return ret['servers'] unless exact
      ret['servers'].find { |x| x['name'].casecmp(server).zero? }
    rescue NoMethodError
      nil
    end

def get_server_name ( server , exact = true ) ret = nr_api . get ( url ( 'servers' ) , 'filter[name]' => server ) . body return ret [ 'servers' ] unless exact ret [ 'servers' ] . find { | x | x [ 'name' ] . casecmp ( server ) . zero? } rescue NoMethodError nil end


def get_servers_labeled(labels)
      label_query = Array(labels).reject { |x| !x.include?(':') }.join(';')
      return [] unless label_query
      nr_api.get(url('servers'), 'filter[labels]' => label_query).body
    end

def get_servers_labeled ( labels ) label_query = Array ( labels ) . reject { | x | ! x . include? ( ':' ) } . join ( ';' ) return [ ] unless label_query nr_api . get ( url ( 'servers' ) , 'filter[labels]' => label_query ) . body end


def observe_events(client)
      return unless enable_listener?
      queue = create_queue(client)
      client.subscribe(queue) do |message|
        event = Event.parse(message)
        handle_event(event)
      end
    end

def observe_events ( client ) return unless enable_listener? queue = create_queue ( client ) client . subscribe ( queue ) do | message | event = Event . parse ( message ) handle_event ( event ) end end

Create or attach to a queue on the specified exchange. When an event message that matches the observer's routing keys
 is received, parse the event and call handle_event on same.
 @param [Isimud::Client] client client instance
 @return queue or consumer object
 @see BunnyClient#subscribe
 @see TestClient#subscribe

Create or attach to a queue on the specified exchange . When an event message that matches the observer s routing keys is received parse the event and call handle_event on same .


def bind(queue_name, exchange_name, *routing_keys, &block)
      queue = create_queue(queue_name, exchange_name,
                           queue_options: {durable: true},
                           routing_keys:  routing_keys)
      subscribe(queue, &block) if block_given?
    end

def bind ( queue_name , exchange_name , * routing_keys , & block ) queue = create_queue ( queue_name , exchange_name , queue_options : { durable : true } , routing_keys : routing_keys ) subscribe ( queue , & block ) if block_given? end

Initialize a new BunnyClient instance. Note that a connection is not established until any other method is called

 @param [String, Hash] _url Server URL or options hash
 @param [Hash] _bunny_options optional Bunny connection options
 @see Bunny.new for connection options
 Convenience method that finds or creates a named queue, binds to an exchange, and subscribes to messages.
 If a block is provided, it will be called by the consumer each time a message is received.

 @param [String] queue_name name of the queue
 @param [String] exchange_name name of the AMQP exchange. Note that existing exchanges must be declared as Topic
   exchanges; otherwise, an error will occur
 @param [Array<String>] routing_keys list of routing keys to be bound to the queue for the specified exchange.
 @yieldparam [String] payload message text
 @return [Bunny::Consumer] Bunny consumer interface

Initialize a new BunnyClient instance . Note that a connection is not established until any other method is called


def create_queue(queue_name, exchange_name, options = {})
      queue_options = options[:queue_options] || {durable: true}
      routing_keys  = options[:routing_keys] || []
      log "Isimud::BunnyClient: create_queue #{queue_name}: queue_options=#{queue_options.inspect}"
      queue = find_queue(queue_name, queue_options)
      bind_routing_keys(queue, exchange_name, routing_keys) if routing_keys.any?
      queue
    end

Find or create a named queue and bind it to the specified exchange

 @param [String] queue_name name of the queue
 @param [String] exchange_name name of the AMQP exchange. Note that pre-existing exchanges must be declared as Topic
   exchanges; otherwise, an error will occur
 @param [Hash] options queue declaration options
 @option options [Boolean] :queue_options ({durable: true}) queue declaration options -- @see Bunny::Channel#queue
 @option options [Array<String>] :routing_keys ([]) routing keys to be bound to the queue. Use "*" to match any 1 word
   in a route segment. Use "#" to match 0 or more words in a segment.
 @return [Bunny::Queue] Bunny queue


def subscribe(queue, options = {}, &block)
      queue.subscribe(options.merge(manual_ack: true)) do |delivery_info, properties, payload|
        current_channel = delivery_info.channel
        begin
          log "Isimud: queue #{queue.name} received #{properties[:message_id]} routing_key: #{delivery_info.routing_key}", :debug
          Thread.current['isimud_queue_name']    = queue.name
          Thread.current['isimud_delivery_info'] = delivery_info
          Thread.current['isimud_properties']    = properties
          block.call(payload)
          if current_channel.open?
            log "Isimud: queue #{queue.name} finished with #{properties[:message_id]}, acknowledging", :debug
            current_channel.ack(delivery_info.delivery_tag)
          else
            log "Isimud: queue #{queue.name} unable to acknowledge #{properties[:message_id]}", :warn
          end
        rescue => e
          log("Isimud: queue #{queue.name} error processing #{properties[:message_id]} payload #{payload.inspect}: #{e.class.name} #{e.message}\n  #{e.backtrace.join("\n  ")}", :warn)
          retry_status = run_exception_handlers(e)
          log "Isimud: rejecting #{properties[:message_id]} requeue=#{retry_status}", :warn
          current_channel.open? && current_channel.reject(delivery_info.delivery_tag, retry_status)
        end
      end
    end

Subscribe to messages on the Bunny queue. The provided block will be called each time a message is received.
   The message will be acknowledged and deleted from the queue unless an exception is raised from the block.
   In the case that an uncaught exception is raised, the message is rejected, and any declared exception handlers
   will be called.

 @param [Bunny::Queue] queue Bunny queue
 @param [Hash] options {} subscription options -- @see Bunny::Queue#subscribe
 @yieldparam [String] payload message text

Subscribe to messages on the Bunny queue . The provided block will be called each time a message is received . The message will be acknowledged and deleted from the queue unless an exception is raised from the block . In the case that an uncaught exception is raised the message is rejected and any declared exception handlers will be called .


def channel
      if (channel = Thread.current[CHANNEL_KEY]).try(:open?)
        channel
      else
        new_channel = connection.channel
        new_channel.confirm_select
        new_channel.prefetch(Isimud.prefetch_count) if Isimud.prefetch_count
        Thread.current[CHANNEL_KEY] = new_channel
      end
    end

def channel if ( channel = Thread . current [ CHANNEL_KEY ] ) . try ( :open? ) channel else new_channel = connection . channel new_channel . confirm_select new_channel . prefetch ( Isimud . prefetch_count ) if Isimud . prefetch_count Thread . current [ CHANNEL_KEY ] = new_channel end end

Open a new, thread-specific AMQP connection channel, or return the current channel for this thread if it exists
   and is currently open. New channels are created with publisher confirms enabled. Messages will be prefetched
   according to Isimud.prefetch_count when declared.
 @return [Bunny::Channel] channel instance.

Open a new thread - specific AMQP connection channel or return the current channel for this thread if it exists and is currently open . New channels are created with publisher confirms enabled . Messages will be prefetched according to Isimud . prefetch_count when declared .


def publish(exchange, routing_key, payload, options = {})
      log "Isimud::BunnyClient#publish: exchange=#{exchange} routing_key=#{routing_key}", :debug
      channel.topic(exchange, durable: true).publish(payload, options.merge(routing_key: routing_key, persistent: true))
    end

Publish a message to the specified exchange, which is declared as a durable, topic exchange. Note that message
   is always persisted.
 @param [String] exchange AMQP exchange name
 @param [String] routing_key message routing key. This should always be in the form of words separated by dots
   e.g. "user.goal.complete"
 @param [String] payload message payload
 @param [Hash] options additional message options
 @see Bunny::Exchange#publish
 @see http://rubybunny.info/articles/exchanges.html

Publish a message to the specified exchange which is declared as a durable topic exchange . Note that message is always persisted .


def post(opts)
      opts = @helper.ensure_open_struct(opts)
      date = @helper.get_date_stamp

      # still want to escape any garbage in the slug
      slug = if opts.slug.nil? || opts.slug.empty?
               opts.title
             else
               opts.slug
             end
      slug = @helper.get_slug_for(slug)

      # put the metadata into the layout header
      head, ext = @helper.get_layout(opts.layout)
      head.sub!(/^title:\s*$/, "title: #{opts.title}")
      head.sub!(/^date:\s*$/, "date: #{date}")
      ext ||= @ext

      path = File.join(POSTS_FOLDER, "#{date}-#{slug}.#{ext}")
      f = File.open(path, "w")
      f.write(head)
      f.close
      @helper.open_in_editor(path) # open file if config key set
      path    # return the path, in case we want to do anything useful
    end

def
post
(
opts
)
opts
=
@helper
.
ensure_open_struct
(
opts
)
date
=
@helper
.
get_date_stamp
slug
=
if
opts
.
slug
.
nil?
||
opts
.
slug
.
empty?
opts
.
title
else
opts
.
slug
end
slug
=
@helper
.
get_slug_for
(
slug
)
head
,
ext
=
@helper
.
get_layout
(
opts
.
layout
)
head
.
sub!
(
/
\s
/
,
"title: #{opts.title}"
)
head
.
sub!
(
/
\s
/
,
"date: #{date}"
)
ext
||=
@ext
path
=
File
.
join
(
POSTS_FOLDER
,
"#{date}-#{slug}.#{ext}"
)
f
=
File
.
open
(
path
,
"w"
)
f
.
write
(
head
)
f
.
close
@helper
.
open_in_editor
(
path
)
path
end


def draft(opts)
      opts = @helper.ensure_open_struct(opts)

      # the drafts folder might not exist yet...create it just in case
      FileUtils.mkdir_p(DRAFTS_FOLDER)

      slug = if opts.slug.nil? || opts.slug.empty?
               opts.title
             else
               opts.slug
             end
      slug = @helper.get_slug_for(slug)

      # put the metadata into the layout header
      head, ext = @helper.get_layout(opts.layout)
      head.sub!(/^title:\s*$/, "title: #{opts.title}")
      head.sub!(/^date:\s*$/, "date: #{@helper.get_date_stamp}")
      ext ||= @ext

      path = File.join(DRAFTS_FOLDER, "#{slug}.#{ext}")
      f = File.open(path, "w")
      f.write(head)
      f.close
      @helper.open_in_editor(path) # open file if config key set
      path    # return the path, in case we want to do anything useful
    end

def
draft
(
opts
)
opts
=
@helper
.
ensure_open_struct
(
opts
)
FileUtils
.
mkdir_p
(
DRAFTS_FOLDER
)
slug
=
if
opts
.
slug
.
nil?
||
opts
.
slug
.
empty?
opts
.
title
else
opts
.
slug
end
slug
=
@helper
.
get_slug_for
(
slug
)
head
,
ext
=
@helper
.
get_layout
(
opts
.
layout
)
head
.
sub!
(
/
\s
/
,
"title: #{opts.title}"
)
head
.
sub!
(
/
\s
/
,
"date: #{@helper.get_date_stamp}"
)
ext
||=
@ext
path
=
File
.
join
(
DRAFTS_FOLDER
,
"#{slug}.#{ext}"
)
f
=
File
.
open
(
path
,
"w"
)
f
.
write
(
head
)
f
.
close
@helper
.
open_in_editor
(
path
)
path
end


def publish(draftpath, opts={})
      opts = @helper.ensure_open_struct(opts)
      tail = File.basename(draftpath)

      begin
        infile = File.open(draftpath, "r")
      rescue Errno::ENOENT
        @helper.bad_path(draftpath)
      end

      date = @helper.get_date_stamp
      time = @helper.get_time_stamp

      outpath = File.join(POSTS_FOLDER, "#{date}-#{tail}")
      outfile = File.open(outpath, "w")

      infile.each_line do |line|
        line.sub!(/^date:.*$/, "date: #{date} #{time}\n") unless opts.keep_timestamp
        outfile.write(line)
      end

      infile.close
      outfile.close
      FileUtils.rm(draftpath) unless opts.keep_draft

      outpath
    end

def
publish
(
draftpath
,
opts
=
{
}
)
opts
=
@helper
.
ensure_open_struct
(
opts
)
tail
=
File
.
basename
(
draftpath
)
begin
infile
=
File
.
open
(
draftpath
,
"r"
)
rescue
Errno
::
ENOENT
@helper
.
bad_path
(
draftpath
)
end
date
=
@helper
.
get_date_stamp
time
=
@helper
.
get_time_stamp
outpath
=
File
.
join
(
POSTS_FOLDER
,
"#{date}-#{tail}"
)
outfile
=
File
.
open
(
outpath
,
"w"
)
infile
.
each_line
do
|
line
|
line
.
sub!
(
/
/
,
"date: #{date} #{time}\n"
)
unless
opts
.
keep_timestamp
outfile
.
write
(
line
)
end
infile
.
close
outfile
.
close
FileUtils
.
rm
(
draftpath
)
unless
opts
.
keep_draft
outpath
end

Todo make this take a path instead?

 @param draftpath [String] path to the draft, relative to source directory
 @option options :keep_draft [Boolean] if true, keep the draft file
 @option options :keep_timestamp [Boolean] if true, don't change the timestamp


def terminal_size
    rows, cols = 25, 80
    buf = [0, 0, 0, 0].pack("SSSS")
    if $stdout.ioctl(TIOCGWINSZ, buf) >= 0 then
      rows, cols, row_pixels, col_pixels = buf.unpack("SSSS")
    end
    return [rows, cols]
  end

def terminal_size rows , cols = 25 , 80 buf = [ 0 , 0 , 0 , 0 ] . pack ( "SSSS" ) if $stdout . ioctl ( TIOCGWINSZ , buf ) >= 0 then rows , cols , row_pixels , col_pixels = buf . unpack ( "SSSS" ) end return [ rows , cols ] end


def prepare_modes
    buf = [0, 0, 0, 0, 0, 0, ''].pack("IIIICCA*")
    $stdout.ioctl(TCGETS, buf)
    @old_modes = buf.unpack("IIIICCA*")
    new_modes = @old_modes.clone
    new_modes[3] &= ~ECHO # echo off
    new_modes[3] &= ~ICANON # one char @ a time
    $stdout.ioctl(TCSETS, new_modes.pack("IIIICCA*"))
    print "\e[2J" # clear screen
    print "\e[H" # go home
    print "\e[?47h" # kick xterm into the alt screen
    print "\e[?1000h" # kindly ask for mouse positions to make up for it
    self.cursor = false
    flush
  end

def prepare_modes buf = [ 0 , 0 , 0 , 0 , 0 , 0 , '' ] . pack ( "IIIICCA*" ) $stdout . ioctl ( TCGETS , buf ) @old_modes = buf . unpack ( "IIIICCA*" ) new_modes = @old_modes . clone new_modes [ 3 ] &= ~ ECHO new_modes [ 3 ] &= ~ ICANON $stdout . ioctl ( TCSETS , new_modes . pack ( "IIIICCA*" ) ) print "\e[2J" print "\e[H" print "\e[?47h" print "\e[?1000h" self . cursor = false flush end


def extract_subjects(subject)
      return extract_subjects(subject.to_permission_instance) if subject.respond_to? :to_permission_instance

      return subject[:any] if subject.is_a? Hash and subject.key? :any

      [subject]
    end

def extract_subjects ( subject ) return extract_subjects ( subject . to_permission_instance ) if subject . respond_to? :to_permission_instance return subject [ :any ] if subject . is_a? Hash and subject . key? :any [ subject ] end

Override functionality from CanCan to allow objects to masquerade as other objects


def daemon # rubocop: disable AbcSize, MethodLength
      # => Windows Workaround (https://github.com/bdwyertech/newrelic-management/issues/1)
      ENV['TZ'] = 'UTC' if OS.windows? && !ENV['TZ']

      scheduler = Rufus::Scheduler.new
      Notifier.msg('Daemonizing Process')

      # => Alerts Management
      alerts_interval = Config.alert_management_interval
      scheduler.every alerts_interval, overlap: false do
        Manager.manage_alerts
      end

      # => Cleanup Stale Servers
      if Config.cleanup
        cleanup_interval = Config.cleanup_interval
        cleanup_age = Config.cleanup_age

        scheduler.every cleanup_interval, overlap: false do
          Manager.remove_nonreporting_servers(cleanup_age)
        end
      end

      # => Join the Current Thread to the Scheduler Thread
      scheduler.join
    end

def
daemon
ENV
[
'TZ'
]
=
'UTC'
if
OS
.
windows?
&&
!
ENV
[
'TZ'
]
scheduler
=
Rufus
::
Scheduler
.
new
Notifier
.
msg
(
'Daemonizing Process'
)
alerts_interval
=
Config
.
alert_management_interval
scheduler
.
every
alerts_interval
,
overlap
:
false
do
Manager
.
manage_alerts
end
if
Config
.
cleanup
cleanup_interval
=
Config
.
cleanup_interval
cleanup_age
=
Config
.
cleanup_age
scheduler
.
every
cleanup_interval
,
overlap
:
false
do
Manager
.
remove_nonreporting_servers
(
cleanup_age
)
end
end
scheduler
.
join
end

=> Daemonization for Periodic Management

= > Daemonization for Periodic Management


def run
      daemon if Config.daemonize

      # => Manage Alerts
      Manager.manage_alerts

      # => Manage
      Manager.remove_nonreporting_servers(Config.cleanup_age) if Config.cleanup
    end

def run daemon if Config . daemonize Manager . manage_alerts Manager . remove_nonreporting_servers ( Config . cleanup_age ) if Config . cleanup end


def ensure_jekyll_dir
      @orig_dir = Dir.pwd
      start_path = Pathname.new(@orig_dir)

      ok = File.exists?('./_posts')
      new_path = nil

      # if it doesn't exist, check for a custom source dir in _config.yml
      if !ok
        check_custom_src_dir!
        ok = File.exists?('./_posts')
        new_path = Pathname.new(Dir.pwd)
      end

      if ok
        return (new_path ? new_path.relative_path_from(start_path) : '.')
      else
        puts 'ERROR: Cannot locate _posts directory. Double check to make sure'
        puts '       that you are in a jekyll directory.'
        exit
      end
    end

'       that you are in a jekyll directory.'

Check for a _posts directory in current directory. If there's not one,
 check for a _config.yml and look for a custom src directory.  If we
 don't find one, puke an error message and die. If we do, return the name
 of the directory

Check for a _posts directory in current directory . If there s not one check for a _config . yml and look for a custom src directory . If we don t find one puke an error message and die . If we do return the name of the directory


def get_layout(layout_path)

      if layout_path.nil?
        contents  = "---\n"
        contents << "title:\n"
        contents << "layout: post\n"
        contents << "date:\n"
        contents << "---\n"
        ext = nil
      else
        begin
          contents = File.open(layout_path, "r").read()
          ext = layout_path.match(/\.(.*?)$/)[1]
        rescue Errno::ENOENT
          bad_path(layout_path)
        end
      end

      return contents, ext
    end

def
get_layout
(
layout_path
)
if
layout_path
.
nil?
contents
=
"---\n"
contents
<<
"title:\n"
contents
<<
"layout: post\n"
contents
<<
"date:\n"
contents
<<
"---\n"
ext
=
else
begin
contents
=
File
.
open
(
layout_path
,
"r"
)
.
read
(
)
ext
=
layout_path
.
match
(
/
\.
/
)
[
1
]
rescue
Errno
::
ENOENT
bad_path
(
layout_path
)
end
end
return
contents
,
ext
end

Get a layout as a string. If layout_path is non-nil,  will open that
 file and read it, otherwise will return a default one, and a file
 extension to use

Get a layout as a string . If layout_path is non - nil will open that file and read it otherwise will return a default one and a file extension to use


def gen_usage
      puts 'Usage:'
      puts '  poole [ACTION] [ARG]'
      puts ''
      puts 'Actions:'
      puts '  draft      Create a new draft in _drafts with title SLUG'
      puts '  post       Create a new timestamped post in _posts with title SLUG'
      puts '  publish    Publish the draft with SLUG, timestamping appropriately'
      puts '  unpublish  Move a post to _drafts, untimestamping appropriately'
      exit
    end


def msg(message, subtitle = message, title = 'NewRelic Management')
      # => Stdout Messages
      terminal_notification(message, subtitle)

      return if Config.silent

      # => Pretty GUI Messages
      osx_notification(message, subtitle, title) if OS.x?
    end

def
msg
(
message
,
subtitle
=
message
,
title
=
'NewRelic Management'
)
terminal_notification
(
message
,
subtitle
)
return
if
Config
.
silent
osx_notification
(
message
,
subtitle
,
title
)
if
OS
.
x?
end

=> Primary Notification Message Controller

= > Primary Notification Message Controller


def osx_notification(message, subtitle, title)
      TerminalNotifier.notify(message, title: title, subtitle: subtitle)
    end

def osx_notification ( message , subtitle , title ) TerminalNotifier . notify ( message , title : title , subtitle : subtitle ) end


def analyze(string, options = {})
      x = []
      @fst._analyze(string) do |a|
        if options[:symbol_sequence]
          x << a.map { |s| s.match(/^<(.*)>$/) ? $1.to_sym : s }
        else
          x << a.join
        end
      end
      x
    end

def analyze ( string , options = { } ) x = [ ] @fst . _analyze ( string ) do | a | if options [ :symbol_sequence ] x << a . map { | s | s . match ( / / ) ? $1 . to_sym : s } else x << a . join end end x end

Analyses a string +string+. Returns an array of analysed
 strings if the string is accepted, or an empty array if not.

 ==== Options
 * +symbol_sequence+ - Return each analysis as a sequence of symbols.
   Multicharacter symbols will be strings on the form +<symbol>+.

Analyses a string + string + . Returns an array of analysed strings if the string is accepted or an empty array if not .


def generate(string)
      x = []
      @fst._generate(string) { |a| x << a.join }
      x
    end

def generate ( string ) x = [ ] @fst . _generate ( string ) { | a | x << a . join } x end

Generates a string +string+. Returns an array of generated
 strings if the string is accepted or an empty array if not.

Generates a string + string + . Returns an array of generated strings if the string is accepted or an empty array if not .


def configure(argv = ARGV)
      # => Parse CLI Configuration
      cli = Options.new
      cli.parse_options(argv)

      # => Parse JSON Config File (If Specified and Exists)
      json_config = Util.parse_json(cli.config[:config_file] || Config.config_file)

      # => Merge Configuration (CLI Wins)
      config = [json_config, cli.config].compact.reduce(:merge)

      # => Apply Configuration
      config.each { |k, v| Config.send("#{k}=", v) }
    end

def configure ( argv = ARGV ) cli = Options . new cli . parse_options ( argv ) json_config = Util . parse_json ( cli . config [ :config_file ] || Config . config_file ) config = [ json_config , cli . config ] . compact . reduce ( :merge ) config . each { | k , v | Config . send ( "#{k}=" , v ) } end


def missing_specs_from_file(path)
      Spectro::Spec::Parser.parse(path).select do |spec|
        index_spec = Spectro::Database.index[path] && Spectro::Database.index[path][spec.signature.name]
        index_spec.nil? || index_spec['spec_md5'] != spec.md5
      end
    end

def missing_specs_from_file ( path ) Spectro :: Spec :: Parser . parse ( path ) . select do | spec | index_spec = Spectro :: Database . index [ path ] && Spectro :: Database . index [ path ] [ spec . signature . name ] index_spec . nil? || index_spec [ 'spec_md5' ] != spec . md5 end end

Parse the specs on the given file path and return those
 that have not been fulfilled or need to be updated.

 @param [String] path target file path
 @return [<Spectro::Spec>] collection of specs not fulfilled or out of date

Parse the specs on the given file path and return those that have not been fulfilled or need to be updated .


def remove_nonreporting_servers(keeptime = nil)
      list_nonreporting_servers.each do |server|
        next if keeptime && Time.parse(server[:last_reported_at]) >= Time.now - ChronicDuration.parse(keeptime)
        Notifier.msg(server[:name], 'Removing Stale, Non-Reporting Server')
        Client.delete_server(server[:id])
      end
    end

def
remove_nonreporting_servers
(
keeptime
=
)
list_nonreporting_servers
.
each
do
|
server
|
next
if
keeptime
&&
Time
.
parse
(
server
[
:last_reported_at
]
)
>=
Time
.
now
ChronicDuration
.
parse
(
keeptime
)
Notifier
.
msg
(
server
[
:name
]
,
'Removing Stale, Non-Reporting Server'
)
Client
.
delete_server
(
server
[
:id
]
)
end
end


def find_excluded(excluded)
      result = []
      Array(excluded).each do |exclude|
        if exclude.include?(':')
          find_labeled(exclude).each { |x| result << x }
          next
        end
        res = Client.get_server(exclude)
        result << res['id'] if res
      end
      result
    end

def find_excluded ( excluded ) result = [ ] Array ( excluded ) . each do | exclude | if exclude . include? ( ':' ) find_labeled ( exclude ) . each { | x | result << x } next end res = Client . get_server ( exclude ) result << res [ 'id' ] if res end result end

=> Find Servers which should be excluded from Management

= > Find Servers which should be excluded from Management


def send_sms_confirmation!
      return false if _TC_sms_blocked
      return true  if sms_confirmed?
      return false if _TC_phone_number.blank?

      format_phone_number
      confirmation_code = TwilioContactable.confirmation_code(self, :sms)

      # Use this class' confirmation_message method if it
      # exists, otherwise use the generic message
      message = (self.class.respond_to?(:confirmation_message) ?
                   self.class :
                   TwilioContactable).confirmation_message(confirmation_code)

      if message.to_s.size > 160
        raise ArgumentError, "SMS Confirmation Message is too long. Limit it to 160 characters of unescaped text."
      end

      response = TwilioContactable::Gateway.deliver_sms(message, _TC_formatted_phone_number)

      if response.success?
        update_twilio_contactable_sms_confirmation confirmation_code
      end

      response
    end

Sends an SMS validation request through the gateway


def send_voice_confirmation!
      return false if _TC_voice_blocked
      return true  if voice_confirmed?
      return false if _TC_phone_number.blank?

      format_phone_number
      confirmation_code = TwilioContactable.confirmation_code(self, :voice)

      response = TwilioContactable::Gateway.initiate_voice_call(self, _TC_formatted_phone_number)

      if response.success?
        update_twilio_contactable_voice_confirmation confirmation_code
      end

      response
    end

Begins a phone call to the user where they'll need to type
 their confirmation code

Begins a phone call to the user where they ll need to type their confirmation code


def validate(value, format, raise_error=false)
      unless FORMATS.key?(format)
        raise FormatError, "Invalid data format: #{format}"
      end
      result = value =~ FORMATS[format] ? true : false
      if raise_error && !result
        raise ValidationError, "Invalid value \"#{value}\" for #{format}"
      end
      result
    end


def call(url, settings, &block)
      uri = URI.parse(url)
      klass = adapter_class(uri.scheme)
      session = klass.new(uri, settings.deep_dup)
      session.start(&block)
    end

def call ( url , settings , & block ) uri = URI . parse ( url ) klass = adapter_class ( uri . scheme ) session = klass . new ( uri , settings . deep_dup ) session . start ( & block ) end

Initiates a new session
 @see XFTP::Validator::Settings


def all_fields(table, field_name, value)
      body = savon.
             call(:get_all_fields_records_given_a_field_name_and_value,
                  message: {
                    'strFeatureClassOrTableName' => table,
                    'strFieldNameToSearchOn' => field_name,
                    'strValueOfFieldToSearchOn' => value
                  }).
             body

      resp = body[:get_all_fields_records_given_a_field_name_and_value_response]
      rslt = resp[:get_all_fields_records_given_a_field_name_and_value_result]
      polys = rslt[:diffgram][:document_element][:municipality_poly]

      poly = if polys.is_a? Array
               polys.first
             elsif polys.is_a? Hash
               polys
             else
               fail "Unexpected polys #{polys.class.name}, wanted Array or Hash"
             end
    end

Performs a search for geo-attributes.

 @param [String] table the table to search
 @param [String] field_name the field/column to search in the given table
 @param [String] value string value to search in the given field and table
 @return [Hash] search results

Performs a search for geo - attributes .


def WHEN(condition, adjuster)
      unless Validation.conditionable? condition
        raise TypeError, 'wrong object for condition'
      end

      unless Validation.adjustable? adjuster
        raise TypeError, 'wrong object for adjuster'
      end

      ->v{_valid?(condition, v) ? adjuster.call(v) : v}
    end

def
WHEN
(
condition
,
adjuster
)
unless
Validation
.
conditionable?
condition
raise
TypeError
,
'wrong object for condition'
end
unless
Validation
.
adjustable?
adjuster
raise
TypeError
,
'wrong object for adjuster'
end
->
v
{
_valid?
(
condition
,
v
)
adjuster
.
call
(
v
)
:
v
}
end

Adjuster Builders
 Apply adjuster when passed condition.
 @param condition [Proc, Method, #===]
 @param adjuster [Proc, #to_proc]
 @return [lambda]

Adjuster Builders Apply adjuster when passed condition .


def INJECT(adjuster1, adjuster2, *adjusters)
      adjusters = [adjuster1, adjuster2, *adjusters]

      unless adjusters.all?{|f|adjustable? f}
        raise TypeError, 'wrong object for adjuster'
      end

      ->v{
        adjusters.reduce(v){|ret, adjuster|adjuster.call ret}
      }
    end

def
INJECT
(
adjuster1
,
adjuster2
,
*
adjusters
)
adjusters
=
[
adjuster1
,
adjuster2
,
*
adjusters
]
unless
adjusters
.
all?
{
|
f
|
adjustable?
f
}
raise
TypeError
,
'wrong object for adjuster'
end
->
v
{
adjusters
.
reduce
(
v
)
{
|
ret
,
adjuster
|
adjuster
.
call
ret
}
}
end

Sequencial apply all adjusters.
 @param adjuster1 [Proc, #to_proc]
 @param adjuster2 [Proc, #to_proc]
 @param adjusters [Proc, #to_proc]
 @return [lambda]


def PARSE(parser)
      if !::Integer.equal?(parser) and !parser.respond_to?(:parse)
        raise TypeError, 'wrong object for parser'
      end

      ->v{
        if ::Integer.equal? parser
          ::Kernel.Integer v
        else
          parser.parse(
            case v
            when String
              v
            when ->_{v.respond_to? :to_str}
              v.to_str
            when ->_{v.respond_to? :read}
              v.read
            else
              raise TypeError, 'wrong object for parsing source'
            end
          )
        end
      }
    end

def
PARSE
(
parser
)
if
!
::
Integer
.
equal?
(
parser
)
and
!
parser
.
respond_to?
(
:parse
)
raise
TypeError
,
'wrong object for parser'
end
->
v
{
if
::
Integer
.
equal?
parser
::
Kernel
.
Integer
v
else
parser
.
parse
(
case
v
when
String
v
when
->
_
{
v
.
respond_to?
:to_str
}
v
.
to_str
when
->
_
{
v
.
respond_to?
:read
}
v
.
read
else
raise
TypeError
,
'wrong object for parsing source'
end
)
end
}
end

Accept any parser when that resopond to parse method.
 @param parser [#parse]
 @return [lambda]

Accept any parser when that resopond to parse method .


def get_class(name)
      @class_list = get_class_list
      @class_list.select {|x| x.include?(name)}.collect{|y| y.strip}
    end

def get_class ( name ) @class_list = get_class_list @class_list . select { | x | x . include? ( name ) } . collect { | y | y . strip } end


def connection
      @options[:path] =API_REST + @options[:path]
      @options[:headers] = HEADERS.merge({
        'X-Megam-Date' =>  Time.now.strftime("%Y-%m-%d %H:%M")
      }).merge(@options[:headers])

      text.info("HTTP Request Data:")
      text.msg("> HTTP #{@options[:scheme]}://#{@options[:host]}")
      @options.each do |key, value|
        text.msg("> #{key}: #{value}")
      end
      text.info("End HTTP Request Data.")
      http = Net::HTTP.new(@options[:host], @options[:port])
      http
    end


def wake_deadline(start_time, timeout)
      timeout = process_timeout(timeout)
      deadline = start_time + timeout if timeout
    end

def wake_deadline ( start_time , timeout ) timeout = process_timeout ( timeout ) deadline = start_time + timeout if timeout end

Calculate the desired time to wake up.
 @api private
 @param start_time [nil,Time] time when the thread is put to sleep
 @param timeout [Numeric] desired time to sleep in seconds, +nil+ for forever
 @raise [TypeError] if +start_time+ is not +nil+ or a +Numeric+
 @raise [ArgumentError] if +start_time+ is negative
 @return [Time]


def crud_links(model, instance_name, actions, args={})
      _html = ""
      _options = args.keys.empty? ? '' : ", #{args.map{|k,v| ":#{k} => #{v}"}}"

      if use_crud_icons
        if actions.include?(:show)
          _html << eval("link_to image_tag('/images/icons/view.png', :class => 'crud_icon'), model, :title => 'View'#{_options}")
        end
        if actions.include?(:edit)
          _html << eval("link_to image_tag('/images/icons/edit.png', :class => 'crud_icon'), edit_#{instance_name}_path(model), :title => 'Edit'#{_options}")
        end
        if actions.include?(:delete)
          _html << eval("link_to image_tag('/images/icons/delete.png', :class => 'crud_icon'), model, :confirm => 'Are you sure? This action cannot be undone.', :method => :delete, :title => 'Delete'#{_options}")
        end
      else
        if actions.include?(:show)
          _html << eval("link_to 'View', model, :title => 'View', :class => 'crud_link'#{_options}")
        end
        if actions.include?(:edit)
          _html << eval("link_to 'Edit', edit_#{instance_name}_path(model), :title => 'Edit', :class => 'crud_link'#{_options}")
        end
        if actions.include?(:delete)
          _html << eval("link_to 'Delete', model, :confirm => 'Are you sure? This action cannot be undone.', :method => :delete, :title => 'Delete', :class => 'crud_link'#{_options}")
        end
      end
      _html
    end

Display CRUD icons or links, according to setting in use_crud_icons method.

 In application_helper.rb:

   def use_crud_icons
     true
   end

 Then use in index views like this:

 <%= crud_links(my_model, 'my_model', [:show, :edit, :delete]) -%>

Display CRUD icons or links according to setting in use_crud_icons method .


def obfuscated_link_to(path, image, label, args={})
      _html = %{<form action="#{path}" method="get" class="obfuscated_link">}
      _html << %{ <fieldset><input alt="#{label}" src="#{image}" type="image" /></fieldset>}
      args.each{ |k,v| _html << %{
<input id="#{k.to_s}" name="#{k}" type="hidden" value="#{v}" />
} }
      _html << %{</form>}
      _html
    end

Create a link that is opaque to search engine spiders.

Create a link that is opaque to search engine spiders .


def required_field_helper( model, element, html )
      if model && ! model.errors.empty? && element.is_required
        return content_tag( :div, html, :class => 'fieldWithErrors' )
      else
        return html
      end
    end

def required_field_helper ( model , element , html ) if model && ! model . errors . empty? && element . is_required return content_tag ( :div , html , :class => 'fieldWithErrors' ) else return html end end

Wraps the given HTML in Rails' default style to highlight validation errors, if any.

Wraps the given HTML in Rails default style to highlight validation errors if any .


def select_tag_for_filter(model, nvpairs, params)
      return unless model && nvpairs && ! nvpairs.empty?
      options = { :query => params[:query] }
      _url = url_for(eval("#{model}_url(options)"))
      _html = %{Show:
}
      _html << %{<select name="show" id="show" onchange="window.location='#{_url}' + '?show=' + this.value">}
      nvpairs.each do |pair|
        _html << %{<option value="#{pair[:scope]}"}
        if params[:show] == pair[:scope] || ((params[:show].nil? || params[:show].empty?) && pair[:scope] == "all")
          _html << %{ selected="selected"}
        end
        _html << %{>#{pair[:label]}}
        _html << %{</option>}
      end
      _html << %{</select>}
    end

Use on index pages to create dropdown list of filtering criteria.
 Populate the filter list using a constant in the model corresponding to named scopes.

 Usage:

 - item.rb:

     scope :active,   :conditions => { :is_active => true }
     scope :inactive, :conditions => { :is_active => false }

     FILTERS = [
       {:scope => "all",       :label => "All"},
       {:scope => "active",    :label => "Active Only"},
       {:scope => "inactive",  :label => "Inactive Only"}
     ]

 - items/index.html.erb:

     <%= select_tag_for_filter("items", @filters, params) -%>

 - items_controller.rb:

     def index
       @filters = Item::FILTERS
       if params[:show] && params[:show] != "all" && @filters.collect{|f| f[:scope]}.include?(params[:show])
         @items = eval("@items.#{params[:show]}.order_by(params[:by], params[:dir])")
       else
         @items = @items.order_by(params[:by], params[:dir])
       end
       ...
     end

Use on index pages to create dropdown list of filtering criteria . Populate the filter list using a constant in the model corresponding to named scopes .


def sort_link(model, field, params, html_options={})
      if (field.to_sym == params[:by] || field == params[:by]) && params[:dir] == "ASC"
        classname = "arrow-asc"
        dir = "DESC"
      elsif (field.to_sym == params[:by] || field == params[:by])
        classname = "arrow-desc"
        dir = "ASC"
      else
        dir = "ASC"
      end

      options = {
        :anchor => html_options[:anchor] || nil,
        :by => field,
        :dir => dir,
        :query => params[:query],
        :show => params[:show]
      }

      options[:show] = params[:show] unless params[:show].blank? || params[:show] == 'all'

      html_options = {
        :class => "#{classname} #{html_options[:class]}",
        :style => "color: white; font-weight: #{params[:by] == field ? "bold" : "normal"}; #{html_options[:style]}",
        :title => "Sort by this field"
      }

      field_name = params[:labels] && params[:labels][field] ? params[:labels][field] : field.titleize

      _link = model.is_a?(Symbol) ? eval("#{model}_url(options)") : "/#{model}?#{options.to_params}"
      link_to(field_name, _link, html_options)
    end

Returns a link_to tag with sorting parameters that can be used with ActiveRecord.order_by.

 To use standard resources, specify the resources as a plural symbol:
   sort_link(:users, 'email', params)

 To use resources aliased with :as (in routes.rb), specify the aliased route as a string.
   sort_link('users_admin', 'email', params)

 You can override the link's label by adding a labels hash to your params in the controller:
   params[:labels] = {'user_id' => 'User'}

Returns a link_to tag with sorting parameters that can be used with ActiveRecord . order_by .


def tag_for_label_with_inline_help( label_text, field_id, help_text )
      _html = ""
      _html << %{#{label_text}}
      _html << %{}
      _html << %{
}
      _html << %{
}
      _html << %{
#{help_text}
}
      _html << %{
}
      _html
    end

%{<img src="/images/icons/help_icon.png" onclick="$('#{field_id}_help').toggle();" class='inline_icon' />}

%{<div class="inline_help" id="#{field_id}_help" style="display: none;">}

Create a set of tags for displaying a field label with inline help.
 Field label text is appended with a ? icon, which responds to a click
 by showing or hiding the provided help text.

 Sample usage:

   <%= tag_for_label_with_inline_help 'Relative Frequency', 'rel_frequency', 'Relative frequency of search traffic for this keyword across multiple search engines, as measured by WordTracker.' %>

 Yields:

   Relative Frequency: <%= image_tag "/images/help_icon.png", :onclick => "$('rel_frequency_help').toggle();", :class => 'inline_icon' %>

Relative frequency of search traffic for this keyword across multiple search engines, as measured by WordTracker.

Create a set of tags for displaying a field label with inline help . Field label text is appended with a ? icon which responds to a click by showing or hiding the provided help text .


def key_press(keys)
      dump_caller_stack

      if keys =~ /^Ctrl\+([A-Z])$/
        filtered_keys = "^+#{$1}"
      elsif keys =~ /^Ctrl\+Shift\+([A-Z])$/
        filtered_keys = "^+#{$1.downcase}"
      elsif keys =~ /^Alt+([A-Z])$/
        filtered_keys = "!+#{$1}"
      elsif keys =~ /^Alt\+Shift\+([a-z])$/
        filtered_keys = "!+#{$1.downcase}"
      else
        filtered_keys = keys
      end
      filtered_keys = keys.gsub("Alt+", "!+").gsub("Ctrl+", "^+")
      RFormSpec::Keyboard.press(filtered_keys)
      sleep 0.5
    end

def key_press ( keys ) dump_caller_stack if keys =~ / \+ / filtered_keys = "^+#{$1}" elsif keys =~ / \+ \+ / filtered_keys = "^+#{$1.downcase}" elsif keys =~ / / filtered_keys = "!+#{$1}" elsif keys =~ / \+ \+ / filtered_keys = "!+#{$1.downcase}" else filtered_keys = keys end filtered_keys = keys . gsub ( "Alt+" , "!+" ) . gsub ( "Ctrl+" , "^+" ) RFormSpec :: Keyboard . press ( filtered_keys ) sleep 0.5 end


def open_file_dialog(title, filepath, text="")
      wait_and_focus_window(title)
      dialog = RFormSpec::OpenFileDialog.new(title, text)
      dialog.enter_filepath(filepath)
      sleep 1
      dialog.click_open
    end

def open_file_dialog ( title , filepath , text = "" ) wait_and_focus_window ( title ) dialog = RFormSpec :: OpenFileDialog . new ( title , text ) dialog . enter_filepath ( filepath ) sleep 1 dialog . click_open end


def parse(args)
      # The options specified on the command line will be collected in
      # *options*.
      @options = Options.new
      opt_parser = OptionParser.new do |parser|
        @options.define_options(parser)
      end
      opt_parser.parse!(args)

      @options
    end

def parse ( args ) @options = Options . new opt_parser = OptionParser . new do | parser | @options . define_options ( parser ) end opt_parser . parse! ( args ) @options end

Return a structure describing the options.

Return a structure describing the options .