Class: LL::Checklist

Inherits:
Object
  • Object
show all
Defined in:
lib/ll/checklist.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document: nil, name: nil, authority: nil) {|_self| ... } ⇒ Checklist

Maybe abstract the versioning here into VV?

Yields:

  • (_self)

Yield Parameters:

  • _self (LL::Checklist)

    the object that the method was called on



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

Instance Attribute Details

#action_atObject (readonly)

Returns the value of attribute action_at.



3
4
5
# File 'lib/ll/checklist.rb', line 3

def action_at
  @action_at
end

#authorityObject (readonly)

Returns the value of attribute authority.



3
4
5
# File 'lib/ll/checklist.rb', line 3

def authority
  @authority
end

#descriptionObject (readonly)

Returns the value of attribute description.



3
4
5
# File 'lib/ll/checklist.rb', line 3

def description
  @description
end

#document_versionObject (readonly)

Returns the value of attribute document_version.



3
4
5
# File 'lib/ll/checklist.rb', line 3

def document_version
  @document_version
end

#identifierObject (readonly)

Returns the value of attribute identifier.



3
4
5
# File 'lib/ll/checklist.rb', line 3

def identifier
  @identifier
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/ll/checklist.rb', line 3

def name
  @name
end

#serialization_identifierObject (readonly)

Returns the value of attribute serialization_identifier.



3
4
5
# File 'lib/ll/checklist.rb', line 3

def serialization_identifier
  @serialization_identifier
end

#stepsObject

Returns the value of attribute steps.



3
4
5
# File 'lib/ll/checklist.rb', line 3

def steps
  @steps
end

#titleObject (readonly)

Returns the value of attribute title.



3
4
5
# File 'lib/ll/checklist.rb', line 3

def title
  @title
end

Class Method Details

.load(dir:) ⇒ Object



13
14
15
16
17
18
# 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

Instance Method Details

#current_versionObject



78
79
80
# File 'lib/ll/checklist.rb', line 78

def current_version
  LL::VERSION
end

#document_current?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/ll/checklist.rb', line 51

def document_current?
  return nil if @document_version.nil?

  @document_version == self.current_version
end

#kindObject



88
89
90
# File 'lib/ll/checklist.rb', line 88

def kind
  "checklist"
end

#meta=(meta) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 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) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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!Object



39
40
41
42
43
44
45
46
47
48
49
# 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

#supported_versionsObject



82
83
84
85
86
# File 'lib/ll/checklist.rb', line 82

def supported_versions
  %w[ 0.0.2
      0.0.3
      0.0.4 ]
end

#to_hObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# 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_sObject



128
129
130
# File 'lib/ll/checklist.rb', line 128

def to_s
  "Checklist: #{self.name}"
end

#vv_jsonObject



152
153
154
155
156
157
# File 'lib/ll/checklist.rb', line 152

def vv_json
  format = "json"
  response = self.to_h
  response[:meta][:format] = format
  response.vv_json
end