Class: Cardio::Mod::Dirs

Inherits:
Array
  • Object
show all
Defined in:
card/lib/cardio/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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'card/lib/cardio/mod/dirs.rb', line 49

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.



46
47
48
# File 'card/lib/cardio/mod/dirs.rb', line 46

def mods
  @mods
end

Instance Method Details

#add_gem_mod(mod_name, mod_path) ⇒ Object



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

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



66
67
68
69
70
71
72
73
74
# File 'card/lib/cardio/mod/dirs.rb', line 66

def add_path mod_name, path=nil
  if @mods.include? mod_name
    raise Card::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

#dirname(path, type) ⇒ Object



111
112
113
# File 'card/lib/cardio/mod/dirs.rb', line 111

def dirname path, type
  type ? File.join(path, type.to_s) : 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.



104
105
106
107
108
109
# File 'card/lib/cardio/mod/dirs.rb', line 104

def each type=nil
  super() do |path|
    dirname = dirname path, type
    yield dirname if Dir.exist? dirname
  end
end

#each_assets_pathObject



129
130
131
132
133
134
# File 'card/lib/cardio/mod/dirs.rb', line 129

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

#each_tmp(type) ⇒ Object



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

def each_tmp type
  @mods.each do |mod|
    path = tmp_dir mod, type
    yield path if Dir.exist? path
  end
end

#each_with_tmp(type = nil) ⇒ Object



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

def each_with_tmp type=nil
  @mods.each do |mod|
    dirname = dirname @paths[mod], type
    yield dirname, tmp_dir(mod, type) if Dir.exist? dirname
  end
end

#gem_mod(name) ⇒ Object



76
77
78
79
80
# File 'card/lib/cardio/mod/dirs.rb', line 76

def gem_mod name
  deps = Mod.dependencies name
  unknown_gem_mod!(name) if deps.blank?
  deps.each { |spec| add_gem_mod spec.name, spec.full_gem_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



97
98
99
# File 'card/lib/cardio/mod/dirs.rb', line 97

def path mod_name
  @paths[mod_name] || @paths["card-mod-#{mod_name}"]
end

#unknown_gem_mod!(name) ⇒ Object

Raises:



82
83
84
# File 'card/lib/cardio/mod/dirs.rb', line 82

def unknown_gem_mod! name
  raise Card::Error, %(Unknown gem "#{name}". Make sure it is in your Gemfile.)
end