Class: Cardio::Mod::Loader::SetLoader::Template

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

Instance Method Summary collapse

Methods inherited from ModuleTemplate

#build, #simple_load?, #to_s

Constructor Details

#initialize(modules, content_path, strategy) ⇒ Template

Returns a new instance of Template.



34
35
36
37
# File 'lib/cardio/mod/loader/set_loader.rb', line 34

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

Instance Method Details

#add_explicit_format_modulesObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/cardio/mod/loader/set_loader.rb', line 54

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



69
70
71
72
73
# File 'lib/cardio/mod/loader/set_loader.rb', line 69

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

#helper_module?Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
# File 'lib/cardio/mod/loader/set_loader.rb', line 75

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



65
66
67
# File 'lib/cardio/mod/loader/set_loader.rb', line 65

def module_name format_name
  Card::Format.format_class_name format_name
end

#offsetObject

correct line number for error messages



84
85
86
# File 'lib/cardio/mod/loader/set_loader.rb', line 84

def offset
  -5
end

#processed_contentObject



49
50
51
52
# File 'lib/cardio/mod/loader/set_loader.rb', line 49

def processed_content
  add_explicit_format_modules if @strategy.clean_comments?
  super
end

#to_constObject



39
40
41
42
43
44
45
46
47
# File 'lib/cardio/mod/loader/set_loader.rb', line 39

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