Class: Omen::Combination

Inherits:
Object
  • Object
show all
Defined in:
app/models/omen/combination.rb

Overview

One column a reply asks to be drawn as its parts joined, where the database could not join them.

Constant Summary collapse

SCHEMA =

All a reply may declare: a name, the headers to join, and what goes between them.

{
  type: 'array',
  description: 'Columns to draw as one, where the database could not build the value itself ' \
               'because a part of it is encrypted. Empty where nothing needs joining.',
  items: {
    type: 'object', additionalProperties: false, required: %w[ name parts separator ],
    properties: {
      name: { type: 'string', description: 'The header the joined column is drawn under.' },
      parts: { type: 'array', items: { type: 'string' },
               description: 'Headers of your own query to join, in the order they read.', },
      separator: { type: 'string', description: 'What goes between them, such as ", ".' },
    },
  },
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(declared) ⇒ Combination

Returns a new instance of Combination.

Parameters:

  • declared (Hash)

    one entry of a reply's combine.



27
28
29
# File 'app/models/omen/combination.rb', line 27

def initialize(declared)
  @declared = declared
end

Class Method Details

.all(declared, headers) ⇒ Array<Omen::Combination>

Returns the ones a row can be drawn through.

Parameters:

  • declared (Array<Hash>)

    what the reply asked for.

  • headers (Array<String>)

    the headers the rows really have.

Returns:



22
23
24
# File 'app/models/omen/combination.rb', line 22

def self.all(declared, headers)
  declared.map { |one| new one }.select { |combination| combination.over? headers }
end

Instance Method Details

#applied(row) ⇒ Hash

Joins what Omen::Column#read handed back, less the blanks: HIDDEN is a value, NULL is not.

Parameters:

  • row (Hash)

    one row of the answer, decrypted.

Returns:

  • (Hash)

    the row with the parts replaced, where the first of them stood.



39
40
41
42
43
44
45
46
47
# File 'app/models/omen/combination.rb', line 39

def applied(row)
  row.each_with_object({}) do |(header, value), into|
    if header == parts.first
      into[name] = parts.filter_map { |part| row[part].presence }.join separator
    elsif parts.exclude? header
      into[header] = value
    end
  end
end

#over?(headers) ⇒ Boolean

Answered rather than raised on: the rows are right, and only the presentation was wrong.

Parameters:

  • headers (Array<String>)

    the headers the rows really have.

Returns:

  • (Boolean)

    whether every part of this names one of them.



34
# File 'app/models/omen/combination.rb', line 34

def over?(headers) = parts.any? && parts.all? { |part| headers.include? part }