Module: SolrMakr::Utility

Constant Summary collapse

REQUIRED_SOLR_NODES =

These are the minimum files required to configure a collection.

%w[schema.xml solrconfig.xml]

Class Method Summary collapse

Class Method Details

.default_table(collection:, if_blank: 'n/a', **table_options) {|t, item| ... } ⇒ Terminal::Table

Parameters:

  • collection (#each)
  • if_blank (String) (defaults to: 'n/a')
  • table_options (Hash)

    option that get passed to the ‘Terminal::Table` constructor

Yield Parameters:

  • t (Terminal::Table)
  • item (Array, #to_a, Object)

Returns:

  • (Terminal::Table)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/solr_makr/meta/utility.rb', line 26

def default_table(collection:, if_blank: 'n/a', **table_options, &formatter)
  table_options = default_table_options!(table_options)

  return if_blank if collection.blank?

  Terminal::Table.new table_options do |t|
    collection.each do |item|
      if block_given?
        if formatter.arity == 2
          formatter.call(t, item)
        else
          t << yield(item)
        end
      elsif item.respond_to? :to_table_row
        t << item.to_table_row
      else
        t << Array(item)
      end
    end
  end
end

.default_table_options!(**table_options) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/solr_makr/meta/utility.rb', line 8

def default_table_options!(**table_options)
  table_options[:style] ||= {}

  width = table_options.delete(:width)

  unless width == false
    table_options[:style].reverse_merge! width: width || 80
  end

  return table_options
end

.hash_to_table(hsh, if_blank: 'n/a', **table_options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/solr_makr/meta/utility.rb', line 48

def hash_to_table(hsh, if_blank: 'n/a', **table_options)
  hsh = hsh.to_h

  table_options = default_table_options!(table_options)

  if hsh.present?
    Terminal::Table.new table_options do |t|
      hsh.each do |key, value|
        t << [key.inspect, value.inspect]
      end
    end
  else
    if_blank
  end
end

.looks_like_a_valid_configset?(conf_path) {|name| ... } ⇒ Boolean

Parameters:

  • conf_path (Pathname)

Yields:

  • called once for each missing node

Yield Parameters:

  • name (String)

Yield Returns:

  • (void)

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/solr_makr/meta/utility.rb', line 83

def looks_like_a_valid_configset?(conf_path)
  if conf_path.present? && conf_path.try(:exist?)
    missing = REQUIRED_SOLR_NODES.reject do |file|
      conf_path.join(file).exist?
    end

    missing.each do |name|
      yield name if block_given?
    end

    return missing.none?
  else
    false
  end
end

.path_to_configset(root) ⇒ Pathname?

It’s possible the root directory is not what actually contains the nodes for configuring solr (e.g. files are in a ‘conf` directory). For now let’s use ‘Pathname#find` to figure it out.

Parameters:

  • root (Pathname)

Returns:

  • (Pathname)
  • (nil)


71
72
73
74
75
76
77
# File 'lib/solr_makr/meta/utility.rb', line 71

def path_to_configset(root)
  root.find do |path|
    return path.dirname if path.basename.to_s.in?(REQUIRED_SOLR_NODES)
  end

  return nil
end