Class: AssetDB::Database
- Inherits:
-
Object
- Object
- AssetDB::Database
- Defined in:
- lib/asset_db/database.rb
Constant Summary collapse
- CONFIG_RESERVED =
%w[types basepath folders].freeze
- G_FOLDER_DEFAULT =
DSL helpers
Group::FOLDER_DEFAULT
Instance Attribute Summary collapse
-
#asset_types ⇒ Object
readonly
Returns the value of attribute asset_types.
-
#basepath ⇒ Object
readonly
Returns the value of attribute basepath.
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
-
#resolver ⇒ Object
readonly
Returns the value of attribute resolver.
-
#separator ⇒ Object
Returns the value of attribute separator.
Class Method Summary collapse
-
.from_config(cfg) ⇒ Object
————— Config loader —————.
Instance Method Summary collapse
-
#build_url(asset) ⇒ Object
————— URL expansion —————.
- #ensure_root_slash(pth) ⇒ Object
- #group(id, folder: G_FOLDER_DEFAULT) {|g| ... } ⇒ Object
-
#group!(id) ⇒ Object
Strict fetch for resolver.
-
#initialize(asset_types: nil, basepath: nil) ⇒ Database
constructor
A new instance of Database.
- #package!(g_id, p_id) ⇒ Object
-
#unify(*items) ⇒ Object
Unify any number (≥2) of Package or PackageCollection into one collection.
- #validate_identifier!(name) ⇒ Object
Constructor Details
#initialize(asset_types: nil, basepath: nil) ⇒ Database
Returns a new instance of Database.
12 13 14 15 16 17 18 |
# File 'lib/asset_db/database.rb', line 12 def initialize(asset_types: nil, basepath: nil) @asset_types = (asset_types&.map(&:to_sym) || %i[css js]).freeze @basepath = (basepath || '').dup @separator = nil @groups = {} # {id ⇒ Group} @resolver = Resolver.new(self) end |
Instance Attribute Details
#asset_types ⇒ Object (readonly)
Returns the value of attribute asset_types.
9 10 11 |
# File 'lib/asset_db/database.rb', line 9 def asset_types @asset_types end |
#basepath ⇒ Object (readonly)
Returns the value of attribute basepath.
9 10 11 |
# File 'lib/asset_db/database.rb', line 9 def basepath @basepath end |
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
9 10 11 |
# File 'lib/asset_db/database.rb', line 9 def groups @groups end |
#resolver ⇒ Object (readonly)
Returns the value of attribute resolver.
9 10 11 |
# File 'lib/asset_db/database.rb', line 9 def resolver @resolver end |
#separator ⇒ Object
Returns the value of attribute separator.
10 11 12 |
# File 'lib/asset_db/database.rb', line 10 def separator @separator end |
Class Method Details
.from_config(cfg) ⇒ Object
————— Config loader —————
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/asset_db/database.rb', line 67 def self.from_config(cfg) cfg = cfg.transform_keys(&:to_s) db = new(asset_types: cfg['types'], basepath: cfg['basepath']) folders_map = (cfg['folders'] || {}).transform_keys(&:to_s).dup if sep = folders_map.delete('separator') db.separator = sep.to_s end cfg.each do |g_id, g_spec| next if CONFIG_RESERVED.include?(g_id) raise Errors::InvalidIdentifierError, "group name ‘#{g_id}’ conflicts with asset type" \ if db.asset_types.include?(g_id.to_sym) # ---------- GROUP ---------- # if (folders_map.key? g_id) g = db.group(g_id, folder: folders_map[g_id]) # explicit override (can be nil/false/empty) else g = db.group(g_id) # default ⇒ id end gid = g_id.to_s # ---------- PACKAGES ---------- # g_spec.each do |p_id, p_spec| p_key = "#{gid}#{db.separator}#{p_id}" # composite key for folder overrides if (folders_map.key? p_key) pkg = g.package(p_id, folder: folders_map[p_key]) # explicit override else pkg = g.package(p_id) # default ⇒ id end p_spec.each do |k, v| key = k.to_s if db.asset_types.include?(key.to_sym) # ASSETS Array(v).each { |url| pkg.asset(key, url == true ? p_id + '.' + key : url) } else # DEPENDENCIES Array(v).each { |target_pkg| pkg.depends_on(target_pkg, group_id: key) } end end end end db end |
Instance Method Details
#build_url(asset) ⇒ Object
————— URL expansion —————
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/asset_db/database.rb', line 46 def build_url(asset) return asset.url if asset.protocol_url? return ensure_root_slash(asset.url) if asset.url.start_with?('/') group = asset.group package = asset.package path = @basepath.dup unless path.empty? path.gsub!(':type', asset.type.to_s) path.gsub!(':group', group.folder_segment.to_s) path.gsub!(':package', package.folder_segment.to_s) end ensure_root_slash(File.join(path, asset.url)) end |
#ensure_root_slash(pth) ⇒ Object
62 63 64 |
# File 'lib/asset_db/database.rb', line 62 def ensure_root_slash(pth) ('/' + pth.gsub(%r{^/+}, '')).gsub(%r{/{2,}}, '/') end |
#group(id, folder: G_FOLDER_DEFAULT) {|g| ... } ⇒ Object
22 23 24 25 26 |
# File 'lib/asset_db/database.rb', line 22 def group(id, folder: G_FOLDER_DEFAULT, &block) g = (@groups[id.to_s] ||= Group.new(self, id, folder: folder)) yield g if block g end |
#group!(id) ⇒ Object
Strict fetch for resolver
36 37 38 |
# File 'lib/asset_db/database.rb', line 36 def group!(id) @groups[id.to_s] or raise Errors::UnknownGroupError, id end |
#package!(g_id, p_id) ⇒ Object
40 41 42 43 |
# File 'lib/asset_db/database.rb', line 40 def package!(g_id, p_id) group!(g_id).instance_variable_get(:@packages_hash)[p_id.to_s] or raise Errors::UnknownPackageError, "#{g_id}/#{p_id}" end |
#unify(*items) ⇒ Object
Unify any number (≥2) of Package or PackageCollection into one collection
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/asset_db/database.rb', line 116 def unify(*items) # Simple returns if items.empty? return nil elsif items.size == 1 return Resolver::PackageCollection.new(self, items.first) end # Combine packages merged = items.flat_map do |i| case i when Resolver::PackageCollection i.instance_variable_get(:@packages) when Package [i] else raise ArgumentError, "Cannot unify #{i.inspect}" end end Resolver::PackageCollection.new(self, merged) end |
#validate_identifier!(name) ⇒ Object
138 139 140 141 142 |
# File 'lib/asset_db/database.rb', line 138 def validate_identifier!(name) if separator && name.to_s.include?(separator) raise Errors::InvalidIdentifierError, "'#{separator}' forbidden in identifier #{name.inspect}" end end |