Class: Apropos::Set

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#pathObject (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_reObject



45
46
47
# File 'lib/apropos/set.rb', line 45

def basedir_re
  @basedir_re ||= Regexp.new("^.*#{Regexp.escape(@basedir.to_s)}/")
end

#basenameObject



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

#dirnameObject



58
59
60
# File 'lib/apropos/set.rb', line 58

def dirname
  @dirname ||= File.dirname(@path)
end

#extnameObject



66
67
68
# File 'lib/apropos/set.rb', line 66

def extname
  @extname ||= File.extname(@path)
end

#invalid_variantsObject



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_rulesObject



28
29
30
# File 'lib/apropos/set.rb', line 28

def valid_variant_rules
  valid_variants.map(&:rule)
end

#valid_variantsObject



20
21
22
# File 'lib/apropos/set.rb', line 20

def valid_variants
  variants.select(&:valid?)
end

#variant_path_globObject



54
55
56
# File 'lib/apropos/set.rb', line 54

def variant_path_glob
  Pathname.new(dirname).join("#{basename}#{SEPARATOR}*#{extname}")
end

#variant_pathsObject



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

#variantsObject



14
15
16
17
18
# File 'lib/apropos/set.rb', line 14

def variants
  variant_paths.map do |code_fragment, path|
    Variant.new(code_fragment, path)
  end.sort
end