Class: PathEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/naksh/path_entry.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location, type = :guess, naming_convention = false, include_subfolders = false) ⇒ PathEntry

location is a string representing a file path (like /home/user/Documents) type is either :nkc_dir,:nkc,:external_dir,:external, or :mixed_dir

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/naksh/path_entry.rb', line 74

def initialize(location,type=:guess,naming_convention=false,include_subfolders=false)
  raise ArgumentError unless location.kind_of? String
  @location=Pathname.new(location)
  if type==:guess
    begin
      @type=PathEntry.guess_type(@location)
    rescue
      raise NoGuessError
    end
  else
    raise ArgumentError unless type==:mixed_dir or type==:nkc_dir or type==:nkc or type==:external or type==:external_dir
    @type=type
  end
  raise ArgumentError if naming_convention and not (naming_convention.kind_of? String and (@type==:external or @type==:nkc))
  @naming_convention=naming_convention
  @include_subfolders=include_subfolders
  if not @naming_convention and @type==:nkc
    @name=/\.nkc\.rb\z/.match(@location.basename).pre_match
  end
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



95
96
97
# File 'lib/naksh/path_entry.rb', line 95

def location
  @location
end

#nameObject (readonly)

Returns the value of attribute name.



95
96
97
# File 'lib/naksh/path_entry.rb', line 95

def name
  @name
end

#naming_conventionObject (readonly)

Returns the value of attribute naming_convention.



95
96
97
# File 'lib/naksh/path_entry.rb', line 95

def naming_convention
  @naming_convention
end

#typeObject (readonly)

Returns the value of attribute type.



95
96
97
# File 'lib/naksh/path_entry.rb', line 95

def type
  @type
end

Class Method Details

.guess_type(location, include_subfolders = false) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/naksh/path_entry.rb', line 35

def PathEntry.guess_type(location,include_subfolders=false)
  raise ArgumentError unless location.kind_of? Pathname and location.exist?
  if location.directory?
    types=Set.new
    if include_subfolders
      test_files=location.descendants
    else
      test_files=location.children
    end
    test_files.each do |child|
      if Pathname.new(child).is_nkc?
        types<<:nkc
      else
        types<<:external
      end
    end
    if types.length==2
      return :mixed_dir
    elsif types.include? :nkc
      return :nkc_dir
    elsif types.include? :external
      return :external_dir
    else
      raise RuntimeError
    end
  elsif location.file?
    if location.is_nkc?
      return :nkc
    else
      return :external
    end
  else
    raise ArgumentError,'I can\'t handle anything but files and directories'
  end
  raise RuntimeError,'I should have returned a value by now'
end

Instance Method Details

#include_subfolders?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/naksh/path_entry.rb', line 96

def include_subfolders?
  @include_subfolders
end