Class: DirValidator::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/dir_validator/item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dirnameString (readonly)

The path to the parent directory of the Item, or ‘.’ if the parent directory is the root_dir of the Validator.

Returns:

  • (String)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
114
115
116
117
118
119
# File 'lib/dir_validator/item.rb', line 15

class DirValidator::Item

  attr_reader(:path, :dirname, :match_data)
  attr_reader(:pathname, :dirname2, :catalog_id, :matched, :target, :filetype) # @!visibility private

  # Returns a new Item, based on these arguments:
  #   - Validator.
  #   - String: path to a file or directory (omitting Validator.root_dir).
  #   - Integer: the Catalog ID assigned by the Catalog class.
  #
  # @!visibility private
  def initialize(validator, path, catalog_id)
    @validator  = validator
    @pathname   = Pathname.new(path).cleanpath
    @path       = @pathname.to_s
    @dirname    = @pathname.dirname.to_s
    @dirname2   = @dirname == '.' ? '' : @dirname
    @catalog_id = catalog_id
    @matched    = false
    @target     = nil
    @match_data = nil
    @filetype   = @pathname.file?      ? :file :
                  @pathname.directory? ? :dir  : nil
  end

  # Just a front-end for Ruby's File#basename.
  #
  # @param args          See File#basename.
  # @return     [String] The basename of the Item.
  def basename(*args)
    return @pathname.basename(*args).to_s
  end

  # @!visibility private
  def is_dir
    return @filetype == :dir
  end

  # @!visibility private
  def is_file
    return @filetype == :file
  end

  # @!visibility private
  def mark_as_matched
    @matched = true
  end

  # @!visibility private
  def set_target(t)
    @target = t
  end

  # Takes a Regexp. Trys to match Item.target against the Regexp.
  # Stores the resulting MatchData.
  #
  # @!visibility private
  def target_match(regex)
    @match_data = regex.match(@target)
    return @match_data
  end

  # Validation method, using a {DirValidator::Item} as the receiver.
  #
  # @see DirValidator::Validator#dir
  # @return  (see DirValidator::Validator#dir)
  def dir(vid, opts = {})
    return @validator.dir(vid, item_opts(opts))
  end

  # @see #dir
  # @return  (see DirValidator::Validator#file)
  def file(vid, opts = {})
    return @validator.file(vid, item_opts(opts))
  end

  # @see #dir
  # @return  (see DirValidator::Validator#dirs)
  def dirs(vid, opts = {})
    return @validator.dirs(vid, item_opts(opts))
  end

  # @see #dir
  # @return  (see DirValidator::Validator#files)
  def files(vid, opts = {})
    return @validator.files(vid, item_opts(opts))
  end

  # Takes a validation-method opts hash.
  # Returns a new hash of opts with the appropriate value
  # for :base_dir. That value depends on whether the current Item
  # is a dir or file, and whether the file has a parent dir.
  #
  # @!visibility private
  def item_opts(opts)
    if is_dir
      return opts.merge(:base_dir => @path)
    elsif @dirname == '.'
      return opts
    else
      return opts.merge(:base_dir => @dirname)
    end
  end

end

#match_dataMatchData (readonly)

The MatchData from the most recent regular expression test against the Item.

Returns:

  • (MatchData)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
114
115
116
117
118
119
# File 'lib/dir_validator/item.rb', line 15

class DirValidator::Item

  attr_reader(:path, :dirname, :match_data)
  attr_reader(:pathname, :dirname2, :catalog_id, :matched, :target, :filetype) # @!visibility private

  # Returns a new Item, based on these arguments:
  #   - Validator.
  #   - String: path to a file or directory (omitting Validator.root_dir).
  #   - Integer: the Catalog ID assigned by the Catalog class.
  #
  # @!visibility private
  def initialize(validator, path, catalog_id)
    @validator  = validator
    @pathname   = Pathname.new(path).cleanpath
    @path       = @pathname.to_s
    @dirname    = @pathname.dirname.to_s
    @dirname2   = @dirname == '.' ? '' : @dirname
    @catalog_id = catalog_id
    @matched    = false
    @target     = nil
    @match_data = nil
    @filetype   = @pathname.file?      ? :file :
                  @pathname.directory? ? :dir  : nil
  end

  # Just a front-end for Ruby's File#basename.
  #
  # @param args          See File#basename.
  # @return     [String] The basename of the Item.
  def basename(*args)
    return @pathname.basename(*args).to_s
  end

  # @!visibility private
  def is_dir
    return @filetype == :dir
  end

  # @!visibility private
  def is_file
    return @filetype == :file
  end

  # @!visibility private
  def mark_as_matched
    @matched = true
  end

  # @!visibility private
  def set_target(t)
    @target = t
  end

  # Takes a Regexp. Trys to match Item.target against the Regexp.
  # Stores the resulting MatchData.
  #
  # @!visibility private
  def target_match(regex)
    @match_data = regex.match(@target)
    return @match_data
  end

  # Validation method, using a {DirValidator::Item} as the receiver.
  #
  # @see DirValidator::Validator#dir
  # @return  (see DirValidator::Validator#dir)
  def dir(vid, opts = {})
    return @validator.dir(vid, item_opts(opts))
  end

  # @see #dir
  # @return  (see DirValidator::Validator#file)
  def file(vid, opts = {})
    return @validator.file(vid, item_opts(opts))
  end

  # @see #dir
  # @return  (see DirValidator::Validator#dirs)
  def dirs(vid, opts = {})
    return @validator.dirs(vid, item_opts(opts))
  end

  # @see #dir
  # @return  (see DirValidator::Validator#files)
  def files(vid, opts = {})
    return @validator.files(vid, item_opts(opts))
  end

  # Takes a validation-method opts hash.
  # Returns a new hash of opts with the appropriate value
  # for :base_dir. That value depends on whether the current Item
  # is a dir or file, and whether the file has a parent dir.
  #
  # @!visibility private
  def item_opts(opts)
    if is_dir
      return opts.merge(:base_dir => @path)
    elsif @dirname == '.'
      return opts
    else
      return opts.merge(:base_dir => @dirname)
    end
  end

end

#pathString (readonly)

The path to the Item, omitting the root_dir of the Validator.

Returns:

  • (String)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
114
115
116
117
118
119
# File 'lib/dir_validator/item.rb', line 15

class DirValidator::Item

  attr_reader(:path, :dirname, :match_data)
  attr_reader(:pathname, :dirname2, :catalog_id, :matched, :target, :filetype) # @!visibility private

  # Returns a new Item, based on these arguments:
  #   - Validator.
  #   - String: path to a file or directory (omitting Validator.root_dir).
  #   - Integer: the Catalog ID assigned by the Catalog class.
  #
  # @!visibility private
  def initialize(validator, path, catalog_id)
    @validator  = validator
    @pathname   = Pathname.new(path).cleanpath
    @path       = @pathname.to_s
    @dirname    = @pathname.dirname.to_s
    @dirname2   = @dirname == '.' ? '' : @dirname
    @catalog_id = catalog_id
    @matched    = false
    @target     = nil
    @match_data = nil
    @filetype   = @pathname.file?      ? :file :
                  @pathname.directory? ? :dir  : nil
  end

  # Just a front-end for Ruby's File#basename.
  #
  # @param args          See File#basename.
  # @return     [String] The basename of the Item.
  def basename(*args)
    return @pathname.basename(*args).to_s
  end

  # @!visibility private
  def is_dir
    return @filetype == :dir
  end

  # @!visibility private
  def is_file
    return @filetype == :file
  end

  # @!visibility private
  def mark_as_matched
    @matched = true
  end

  # @!visibility private
  def set_target(t)
    @target = t
  end

  # Takes a Regexp. Trys to match Item.target against the Regexp.
  # Stores the resulting MatchData.
  #
  # @!visibility private
  def target_match(regex)
    @match_data = regex.match(@target)
    return @match_data
  end

  # Validation method, using a {DirValidator::Item} as the receiver.
  #
  # @see DirValidator::Validator#dir
  # @return  (see DirValidator::Validator#dir)
  def dir(vid, opts = {})
    return @validator.dir(vid, item_opts(opts))
  end

  # @see #dir
  # @return  (see DirValidator::Validator#file)
  def file(vid, opts = {})
    return @validator.file(vid, item_opts(opts))
  end

  # @see #dir
  # @return  (see DirValidator::Validator#dirs)
  def dirs(vid, opts = {})
    return @validator.dirs(vid, item_opts(opts))
  end

  # @see #dir
  # @return  (see DirValidator::Validator#files)
  def files(vid, opts = {})
    return @validator.files(vid, item_opts(opts))
  end

  # Takes a validation-method opts hash.
  # Returns a new hash of opts with the appropriate value
  # for :base_dir. That value depends on whether the current Item
  # is a dir or file, and whether the file has a parent dir.
  #
  # @!visibility private
  def item_opts(opts)
    if is_dir
      return opts.merge(:base_dir => @path)
    elsif @dirname == '.'
      return opts
    else
      return opts.merge(:base_dir => @dirname)
    end
  end

end

Instance Method Details

#basename(*args) ⇒ String

Just a front-end for Ruby’s File#basename.

Parameters:

  • args

    See File#basename.

Returns:

  • (String)

    The basename of the Item.



44
45
46
# File 'lib/dir_validator/item.rb', line 44

def basename(*args)
  return @pathname.basename(*args).to_s
end

#dir(vid, opts = {}) ⇒ DirValidator::Item|nil

Validation method, using a DirValidator::Item as the receiver.

Returns:

See Also:



81
82
83
# File 'lib/dir_validator/item.rb', line 81

def dir(vid, opts = {})
  return @validator.dir(vid, item_opts(opts))
end

#dirs(vid, opts = {}) ⇒ Array

Returns:

  • (Array)

See Also:



93
94
95
# File 'lib/dir_validator/item.rb', line 93

def dirs(vid, opts = {})
  return @validator.dirs(vid, item_opts(opts))
end

#file(vid, opts = {}) ⇒ DirValidator::Item|nil

Returns:

See Also:



87
88
89
# File 'lib/dir_validator/item.rb', line 87

def file(vid, opts = {})
  return @validator.file(vid, item_opts(opts))
end

#files(vid, opts = {}) ⇒ Array

Returns:

  • (Array)

See Also:



99
100
101
# File 'lib/dir_validator/item.rb', line 99

def files(vid, opts = {})
  return @validator.files(vid, item_opts(opts))
end