Class: Card::Mod::Dirs

Inherits:
Array
  • Object
show all
Defined in:
lib/card/mod/dirs.rb

Overview

Dirs objects are used to manage the load paths for card mods. Mods can be loaded as gems and by using directories with mod subdirectories.

  1. Gemfile A mod gem needs a metadata attribute with { "card-mod" => "the_mod_name" } or the name has to start with "card-mod-". Then you can just add it to your Gemfile. Otherwise it won't be recognized as mod.

  2. mod directory Give a path to a directory with mods. The mods will be loaded in alphabetical order. To change the load order you can add number prefixes to the mod names (like "01_this_first") or add a Modfile. In the Modfile you list all the mods you want to be loaded from that directory in load order with a preceding "mod" command (similar to a Gemfile). The mods are expected in subdirectories with the mod names.

Mods in Modfiles are always loaded before mods in the Gemfile. If you have to change the order add gem mods to your Modfile using the mod_gem command. You can omit the 'card-mod' prefix.

Example for a mod directory: # my_mod/Modfile mod "twitter" gem_mod "logger" mod "cache"

# directory structure my_mods/ Modfile cache/ set/ all/ my_cache.rb twitter/ set/ type/ basic.rb set_pattern/ my_pattern.rb

Dir checks always for gems. You can initialize an Dirs object with an additional array of paths to card mod directories.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod_paths = []) ⇒ Dirs

Returns a new instance of Dirs.

Parameters:

  • mod_paths (String, Array<String>) (defaults to: [])

    paths to directories that contain mods



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/card/mod/dirs.rb', line 59

def initialize mod_paths=[]
  @mods = []
  @loaded_gem_mods = ::Set.new
  @paths = {}
  mod_paths = Array(mod_paths)
  mod_paths.each do |mp|
    @current_path = mp
    load_from_modfile || load_from_dir
  end
  load_from_gemfile
  super()
  @mods.each do |mod_name|
    self << @paths[mod_name]
  end
end

Instance Attribute Details

#modsObject (readonly)

Returns the value of attribute mods.



56
57
58
# File 'lib/card/mod/dirs.rb', line 56

def mods
  @mods
end

Instance Method Details

#add_gem_mod(mod_name, mod_path) ⇒ Object



95
96
97
98
99
100
# File 'lib/card/mod/dirs.rb', line 95

def add_gem_mod mod_name, mod_path
  return if @loaded_gem_mods.include?(mod_name)

  @loaded_gem_mods << mod_name
  add_path mod_name, mod_path
end

#add_path(mod_name, path = nil) ⇒ Object Also known as: mod

Add a mod to mod load paths



76
77
78
79
80
81
82
83
84
# File 'lib/card/mod/dirs.rb', line 76

def add_path mod_name, path=nil
  if @mods.include? mod_name
    raise Error,
          "name conflict: mod with name \"#{mod_name}\" already loaded"
  end
  @mods << mod_name
  path ||= File.join @current_path, mod_name
  @paths[mod_name] = path
end

#each(type = nil) ⇒ Object

Iterate over each mod directory

Parameters:

  • type (Symbol) (defaults to: nil)

    the type of modification like set, set_pattern, or format. It is attached as subdirectory.



113
114
115
116
117
118
119
120
# File 'lib/card/mod/dirs.rb', line 113

def each type=nil
  super() do |path|
    dirname = type ? File.join(path, type.to_s) : path
    next unless Dir.exist? dirname

    yield dirname
  end
end

#each_assets_pathObject



140
141
142
143
144
145
146
147
# File 'lib/card/mod/dirs.rb', line 140

def each_assets_path
  @mods.each do |mod|
    path = File.join(@paths[mod], "public", "assets")
    next unless Dir.exist? path

    yield mod, path
  end
end

#each_tmp(type) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/card/mod/dirs.rb', line 122

def each_tmp type
  @mods.each do |mod|
    path = tmp_dir mod, type
    next unless Dir.exist? path

    yield path
  end
end

#each_with_tmp(type = nil) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/card/mod/dirs.rb', line 131

def each_with_tmp type=nil
  @mods.each do |mod|
    dirname = type ? File.join(@paths[mod], type.to_s) : @paths[mod]
    next unless Dir.exist? dirname

    yield dirname, tmp_dir(mod, type)
  end
end

#gem_mod(mod_name, path = nil) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/card/mod/dirs.rb', line 86

def gem_mod mod_name, path=nil
  path ||= Cardio.gem_mod_paths[mod_name]
  unless path
    raise Error, "Unknown gem mod \"#{mod_name}\". Make sure it is in your Gemfile."
  end

  add_gem_mod mod_name, path
end

#path(mod_name) ⇒ Object

Returns the path to mod mod_name.

Parameters:

  • mod_name (String)

    the name of a mod

Returns:

  • the path to mod mod_name



106
107
108
# File 'lib/card/mod/dirs.rb', line 106

def path mod_name
  @paths[mod_name]
end