Class: Cardio::Mod::Loader::SetTemplate

Inherits:
ModuleTemplate show all
Defined in:
lib/cardio/mod/loader/set_template.rb

Overview

Template for set modules

Instance Method Summary collapse

Methods inherited from ModuleTemplate

#build, #simple_load?, #to_s

Constructor Details

#initialize(modules, content_path, strategy) ⇒ SetTemplate

Returns a new instance of SetTemplate.



6
7
8
9
# File 'lib/cardio/mod/loader/set_template.rb', line 6

def initialize modules, content_path, strategy
  super
  @modules.pop if helper_module?
end

Instance Method Details

#add_explicit_format_modulesObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/cardio/mod/loader/set_template.rb', line 26

def add_explicit_format_modules
  @content.gsub!(/^ *format +:?(\w+)? *do *$/) do
    format_name = Regexp.last_match(1)
    format_name = format_name.blank? ? nil : format_name.to_sym
    "module #{module_name format_name}; " \
    "module_parent.send :register_set_format, "\
    "#{format_class format_name}, self; "\
    "extend Card::Set::AbstractFormat"
  end
end

#format_class(format_name) ⇒ Object



41
42
43
44
45
# File 'lib/cardio/mod/loader/set_template.rb', line 41

def format_class format_name
  klass = ["Card::Format"]
  klass << module_name(format_name) if format_name
  klass.join "::"
end

#helper_module?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/cardio/mod/loader/set_template.rb', line 47

def helper_module?
  if @is_helper_module.nil?
    @is_helper_module = @content =~ /\A#!\s?not? set module/
  else
    @is_helper_module
  end
end

#module_name(format_name) ⇒ Object



37
38
39
# File 'lib/cardio/mod/loader/set_template.rb', line 37

def module_name format_name
  Card::Format.format_class_name format_name
end

#offsetObject

correct line number for error messages



56
57
58
# File 'lib/cardio/mod/loader/set_template.rb', line 56

def offset
  -5
end

#processed_contentObject



21
22
23
24
# File 'lib/cardio/mod/loader/set_template.rb', line 21

def processed_content
  add_explicit_format_modules if @strategy.clean_comments?
  super
end

#to_constObject



11
12
13
14
15
16
17
18
19
# File 'lib/cardio/mod/loader/set_template.rb', line 11

def to_const
  return Object if simple_load?

  @modules.inject(pattern_class) do |const, name_part|
    const.const_get_or_set name_part do
      Module.new
    end
  end
end