Module: Captive::Base

Included in:
SRT, VTT
Defined in:
lib/captive/base.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/captive/base.rb', line 57

def method_missing(method_name, *_args)
  super unless (match = /^as_([a-z]+)$/.match(method_name))

  if valid_format?(match.captures.first.upcase)
    define_format_conversion(method_name, match.captures.first.upcase)
    send(method_name)
  elsif valid_format?(match.captures.first.capitalize)
    define_format_conversion(method_name, match.captures.first.capitalize)
    send(method_name)
  else
    super
  end
end

Instance Attribute Details

#cuesObject

Returns the value of attribute cues.



33
34
35
# File 'lib/captive/base.rb', line 33

def cues
  @cues
end

Class Method Details

.included(base) ⇒ Object



28
29
30
31
# File 'lib/captive/base.rb', line 28

def self.included(base)
  base.extend(ClassMethods)
  base.include(Util)
end

Instance Method Details

#as_json(**args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/captive/base.rb', line 45

def as_json(**args)
  results = {
    'version' => VERSION,
    'cues' => cues.map(&:as_json),
  }
  if results.respond_to?(:as_json)
    results.as_json(**args)
  else
    results
  end
end

#initialize(cue_list: nil) ⇒ Object



35
36
37
# File 'lib/captive/base.rb', line 35

def initialize(cue_list: nil)
  @cues = cue_list || []
end

#respond_to_missing?(method_name, _) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
# File 'lib/captive/base.rb', line 71

def respond_to_missing?(method_name, _)
  super unless (match = /^as_([a-z]+)$/.match(method_name))

  return true if valid_format?(match.captures.first.upcase) || valid_format?(match.captures.first.capitalize)

  super
end

#save_as(filename:) ⇒ Object



39
40
41
42
43
# File 'lib/captive/base.rb', line 39

def save_as(filename:)
  File.open(filename, 'w') do |file|
    file.write(to_s)
  end
end