Class: Slidict::PresentationMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/slidict/presentation_method.rb

Constant Summary collapse

REQUIRED_FIELDS =
%w[id name category description suitable_for slides ai_instructions review_checklist].freeze
REQUIRED_SLIDE_FIELDS =
%w[title role instructions].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, source_path: nil) ⇒ PresentationMethod

Returns a new instance of PresentationMethod.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/slidict/presentation_method.rb', line 15

def initialize(attributes, source_path: nil)
  @source_path = source_path
  @id = attributes.fetch("id")
  @name = attributes.fetch("name")
  @category = attributes.fetch("category")
  @description = attributes.fetch("description")
  @suitable_for = Array(attributes.fetch("suitable_for"))
  @slides = Array(attributes.fetch("slides")).map do |slide|
    MethodSlide.new(
      title: slide.fetch("title"),
      role: slide.fetch("role"),
      instructions: slide.fetch("instructions")
    )
  end
  @ai_instructions = Array(attributes.fetch("ai_instructions"))
  @review_checklist = Array(attributes.fetch("review_checklist"))
  @references = Array(attributes["references"])
  @locale = attributes.fetch("locale", "en")
end

Instance Attribute Details

#ai_instructionsObject (readonly)

Returns the value of attribute ai_instructions.



12
13
14
# File 'lib/slidict/presentation_method.rb', line 12

def ai_instructions
  @ai_instructions
end

#categoryObject (readonly)

Returns the value of attribute category.



12
13
14
# File 'lib/slidict/presentation_method.rb', line 12

def category
  @category
end

#descriptionObject (readonly)

Returns the value of attribute description.



12
13
14
# File 'lib/slidict/presentation_method.rb', line 12

def description
  @description
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/slidict/presentation_method.rb', line 12

def id
  @id
end

#localeObject (readonly)

Returns the value of attribute locale.



12
13
14
# File 'lib/slidict/presentation_method.rb', line 12

def locale
  @locale
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/slidict/presentation_method.rb', line 12

def name
  @name
end

#referencesObject (readonly)

Returns the value of attribute references.



12
13
14
# File 'lib/slidict/presentation_method.rb', line 12

def references
  @references
end

#review_checklistObject (readonly)

Returns the value of attribute review_checklist.



12
13
14
# File 'lib/slidict/presentation_method.rb', line 12

def review_checklist
  @review_checklist
end

#slidesObject (readonly)

Returns the value of attribute slides.



12
13
14
# File 'lib/slidict/presentation_method.rb', line 12

def slides
  @slides
end

#source_pathObject (readonly)

Returns the value of attribute source_path.



12
13
14
# File 'lib/slidict/presentation_method.rb', line 12

def source_path
  @source_path
end

#suitable_forObject (readonly)

Returns the value of attribute suitable_for.



12
13
14
# File 'lib/slidict/presentation_method.rb', line 12

def suitable_for
  @suitable_for
end

Class Method Details

.load_file(path) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/slidict/presentation_method.rb', line 35

def self.load_file(path)
  attributes = YAML.safe_load_file(path, permitted_classes: [], aliases: false)
  validate!(attributes, path)
  new(attributes, source_path: path)
rescue Psych::Exception, KeyError, TypeError => e
  raise ArgumentError, "invalid presentation method #{path}: #{e.message}"
end

.present?(value) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
# File 'lib/slidict/presentation_method.rb', line 66

def self.present?(value)
  case value
  when String then !value.strip.empty?
  when Array then !value.empty?
  else !value.nil?
  end
end

.validate!(attributes, path) ⇒ Object

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/slidict/presentation_method.rb', line 43

def self.validate!(attributes, path)
  raise ArgumentError, "#{path} must contain a YAML mapping" unless attributes.is_a?(Hash)

  missing = REQUIRED_FIELDS.reject { |field| present?(attributes[field]) }
  raise ArgumentError, "#{path} is missing required fields: #{missing.join(', ')}" unless missing.empty?

  unless attributes["id"].is_a?(String) && attributes["id"].match?(/\A[a-z0-9-]+\z/)
    raise ArgumentError, "#{path} id must be a string using lowercase letters, numbers, and hyphens"
  end
  raise ArgumentError, "#{path} slides must be a non-empty array" unless attributes["slides"].is_a?(Array) && !attributes["slides"].empty?

  attributes["slides"].each_with_index do |slide, index|
    unless slide.is_a?(Hash)
      raise ArgumentError, "#{path} slide #{index + 1} must be a mapping"
    end

    missing_slide = REQUIRED_SLIDE_FIELDS.reject { |field| present?(slide[field]) }
    next if missing_slide.empty?

    raise ArgumentError, "#{path} slide #{index + 1} is missing required fields: #{missing_slide.join(', ')}"
  end
end