Class: Slidict::PresentationMethod
- Inherits:
-
Object
- Object
- Slidict::PresentationMethod
- 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
-
#ai_instructions ⇒ Object
readonly
Returns the value of attribute ai_instructions.
-
#category ⇒ Object
readonly
Returns the value of attribute category.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#locale ⇒ Object
readonly
Returns the value of attribute locale.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#references ⇒ Object
readonly
Returns the value of attribute references.
-
#review_checklist ⇒ Object
readonly
Returns the value of attribute review_checklist.
-
#slides ⇒ Object
readonly
Returns the value of attribute slides.
-
#source_path ⇒ Object
readonly
Returns the value of attribute source_path.
-
#suitable_for ⇒ Object
readonly
Returns the value of attribute suitable_for.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(attributes, source_path: nil) ⇒ PresentationMethod
constructor
A new instance of PresentationMethod.
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 || MethodSlide.new( title: .fetch("title"), role: .fetch("role"), instructions: .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_instructions ⇒ Object (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 |
#category ⇒ Object (readonly)
Returns the value of attribute category.
12 13 14 |
# File 'lib/slidict/presentation_method.rb', line 12 def category @category end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
12 13 14 |
# File 'lib/slidict/presentation_method.rb', line 12 def description @description end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
12 13 14 |
# File 'lib/slidict/presentation_method.rb', line 12 def id @id end |
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
12 13 14 |
# File 'lib/slidict/presentation_method.rb', line 12 def locale @locale end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/slidict/presentation_method.rb', line 12 def name @name end |
#references ⇒ Object (readonly)
Returns the value of attribute references.
12 13 14 |
# File 'lib/slidict/presentation_method.rb', line 12 def references @references end |
#review_checklist ⇒ Object (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 |
#slides ⇒ Object (readonly)
Returns the value of attribute slides.
12 13 14 |
# File 'lib/slidict/presentation_method.rb', line 12 def @slides end |
#source_path ⇒ Object (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_for ⇒ Object (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.}" end |
.present?(value) ⇒ 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
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 |, index| unless .is_a?(Hash) raise ArgumentError, "#{path} slide #{index + 1} must be a mapping" end = REQUIRED_SLIDE_FIELDS.reject { |field| present?([field]) } next if .empty? raise ArgumentError, "#{path} slide #{index + 1} is missing required fields: #{.join(', ')}" end end |