Class: Apropos::Set
- Inherits:
-
Object
- Object
- Apropos::Set
- Defined in:
- lib/apropos/set.rb
Overview
A Set generates a list of Variants from a base image path. Any file in the same directory as the base image with the pattern “basename.*.extension” is considered to be a potential Variant, though not all generated Variants are guaranteed to be valid.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
-
.glob(path) ⇒ Object
Wrapper for Dir.glob to make test stubbing cleaner.
Instance Method Summary collapse
- #basedir_re ⇒ Object
- #basename ⇒ Object
- #code_fragment(path) ⇒ Object
- #dirname ⇒ Object
- #extname ⇒ Object
-
#initialize(path, basedir) ⇒ Set
constructor
A new instance of Set.
- #invalid_variants ⇒ Object
- #remove_basedir(path) ⇒ Object
- #valid_variant_rules ⇒ Object
- #valid_variants ⇒ Object
- #variant_path_glob ⇒ Object
- #variant_paths ⇒ Object
- #variants ⇒ Object
Constructor Details
#initialize(path, basedir) ⇒ Set
Returns a new instance of Set.
9 10 11 12 |
# File 'lib/apropos/set.rb', line 9 def initialize(path, basedir) @path = path @basedir = Pathname.new(basedir) end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/apropos/set.rb', line 7 def path @path end |
Class Method Details
.glob(path) ⇒ Object
Wrapper for Dir.glob to make test stubbing cleaner
71 72 73 |
# File 'lib/apropos/set.rb', line 71 def self.glob(path) Dir.glob(path) end |
Instance Method Details
#basedir_re ⇒ Object
45 46 47 |
# File 'lib/apropos/set.rb', line 45 def basedir_re @basedir_re ||= Regexp.new("^.*#{Regexp.escape(@basedir.to_s)}/") end |
#basename ⇒ Object
62 63 64 |
# File 'lib/apropos/set.rb', line 62 def basename @basename ||= File.basename(@path, extname) end |
#code_fragment(path) ⇒ Object
49 50 51 52 |
# File 'lib/apropos/set.rb', line 49 def code_fragment(path) start = File.join(File.dirname(path), basename) path[(start.length + 1)...(path.length - extname.length)] end |
#dirname ⇒ Object
58 59 60 |
# File 'lib/apropos/set.rb', line 58 def dirname @dirname ||= File.dirname(@path) end |
#extname ⇒ Object
66 67 68 |
# File 'lib/apropos/set.rb', line 66 def extname @extname ||= File.extname(@path) end |
#invalid_variants ⇒ Object
24 25 26 |
# File 'lib/apropos/set.rb', line 24 def invalid_variants variants.reject(&:valid?) end |
#remove_basedir(path) ⇒ Object
41 42 43 |
# File 'lib/apropos/set.rb', line 41 def remove_basedir(path) path.sub(basedir_re, '') end |
#valid_variant_rules ⇒ Object
28 29 30 |
# File 'lib/apropos/set.rb', line 28 def valid_variant_rules valid_variants.map(&:rule) end |
#valid_variants ⇒ Object
20 21 22 |
# File 'lib/apropos/set.rb', line 20 def valid_variants variants.select(&:valid?) end |
#variant_path_glob ⇒ Object
54 55 56 |
# File 'lib/apropos/set.rb', line 54 def variant_path_glob Pathname.new(dirname).join("#{basename}#{SEPARATOR}*#{extname}") end |
#variant_paths ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/apropos/set.rb', line 32 def variant_paths paths = {} self.class.glob(@basedir.join(variant_path_glob)).each do |path| key = code_fragment(path) paths[key] = remove_basedir(path) end paths end |