class LL::Checklist

Attributes

action_at[R]
authority[R]
description[R]
document_version[R]
identifier[R]
name[R]
serialization_identifier[R]
steps[R]
title[R]

Public Class Methods

load( dir: ) click to toggle source
# File lib/ll/checklist.rb, line 13
def self.load( dir: )
  filepaths = Dir.glob(File.join dir, "checklists/*")
  filepaths.each do | filepath |
    self.new File.read filepath
  end
end
new(document: nil, name: nil, authority: nil) { |self| ... } click to toggle source

Maybe abstract the versioning here into VV?

# File lib/ll/checklist.rb, line 21
def initialize document: nil,
               name: nil,
               authority: nil
  @name   = name
  @name ||= "Prototypical"

  @authority   = authority
  @authority ||= LL.default_authority

  @document_version = nil

  self.parse_document! document if document

  yield self if block_given?

  self.reformat! unless self.document_current?
end

Public Instance Methods

current_version() click to toggle source
# File lib/ll/checklist.rb, line 78
def current_version
  LL::VERSION
end
document_current?() click to toggle source
# File lib/ll/checklist.rb, line 51
def document_current?
  return nil if @document_version.nil?

  @document_version == self.current_version
end
kind() click to toggle source
# File lib/ll/checklist.rb, line 88
def kind
  "checklist"
end
meta=(meta) click to toggle source
# File lib/ll/checklist.rb, line 92
def meta= meta
  meta.symbolize_keys!
  meta_authority = meta.fetch :authority
  meta_version   = meta.fetch :version
  meta_kind      = meta.fetch :kind
  meta_format    = meta.fetch :format

  authority_ok = meta_authority == self.authority
  message = "Document authority `#{meta_authority}` unknown."
  fail message unless authority_ok

  format_ok = meta_format == "json"
  message = \
  "Document format `#{meta_format}` not supported, must be `json`."
  fail message unless format_ok

  version_ok = meta_version.one_of?( *supported_versions )
  message = "Document version `#{meta_version}` not supported."
  fail message unless version_ok

  kind_ok = meta_kind == "checklist"
  message = \
  "Document kind `#{meta_kind}` not supported, must be `checklist`."
  fail message unless kind_ok

  @document_version = meta_version
end
parse_document!(document) click to toggle source
# File lib/ll/checklist.rb, line 57
def parse_document! document
  if document.is_a? String
    document = document.parse_json
  elsif document.is_a? Hash
  else
    fail TypeError, "Checklist expects string or hash document"
  end

  document.symbolize_keys!

  attrs = %i[ meta
              title
              description
              identifier
              serialization_identifier
              action_at
              steps ]

  self.set_attrs_via attrs, document: document
end
reformat!() click to toggle source
# File lib/ll/checklist.rb, line 39
def reformat!
  @document_version = self.current_version

  # Because the same checklist may be serialized multiple times,
  # by different clients it will get tricky tracing problems, so
  # we include an identifier that clients should generate, but never
  # manipulate.
  @serialization_identifier = Random.identifier

  self.steps
end
steps=(steps) click to toggle source
# File lib/ll/checklist.rb, line 120
def steps= steps
  fail "Expecting steps to be an array." unless steps.is_a? Array
  @steps = steps.map do |step|
    fail "Expecting step to be a hash." unless step.is_a? Hash
    LL::Step.new step, document_version: @document_version
  end
end
supported_versions() click to toggle source
# File lib/ll/checklist.rb, line 82
def supported_versions
  %w[ 0.0.2
      0.0.3
      0.0.4 ]
end
to_h() click to toggle source
# File lib/ll/checklist.rb, line 132
def to_h
  message = \
  "Checklist in indeterminate state. This should never happen."
  fail message unless self.document_current?

  format = "hash"
  meta = { version: self.current_version,
           kind: self.kind,
           authority: self.authority,
           format: format }

  { meta: meta,
    title: self.title,
    description: self.description,
    identifier: self.identifier,
    serialization_identifier: self.serialization_identifier,
    action_at: self.action_at,
    steps: self.steps }
end
to_s() click to toggle source
# File lib/ll/checklist.rb, line 128
def to_s
  "Checklist: #{self.name}"
end
vv_json() click to toggle source
# File lib/ll/checklist.rb, line 152
def vv_json
  format = "json"
  response = self.to_h
  response[:meta][:format] = format
  response.vv_json
end