Class: FileSystemProject

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_dir, file_system) ⇒ FileSystemProject

Returns a new instance of FileSystemProject.



10
11
12
13
14
15
16
17
# File 'lib/file_system_project.rb', line 10

def initialize(project_dir, file_system)
  @root = project_dir
  fail(ArgumentError, "Project directory does not exist.") unless File.exist?(@root)
  @file_system = file_system
  @data_map = data_file_exists? ? make_data_accessors(file_system) : nil
  add_data_map_methods if @data_map
  @error_log = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/file_system_project.rb', line 44

def method_missing(method, *args)
  if dir = directory_accessor(method)
    get_files(dir)
  elsif dir = directory_adder(method)
    add_file(dir, *args)
  else
    super
  end
end

Instance Attribute Details

#data_mapObject (readonly)

Returns the value of attribute data_map.



6
7
8
# File 'lib/file_system_project.rb', line 6

def data_map
  @data_map
end

#error_logObject

Returns the value of attribute error_log.



7
8
9
# File 'lib/file_system_project.rb', line 7

def error_log
  @error_log
end

#file_systemObject (readonly)

Returns the value of attribute file_system.



6
7
8
# File 'lib/file_system_project.rb', line 6

def file_system
  @file_system
end

#rootObject (readonly) Also known as: path

Returns the value of attribute root.



6
7
8
# File 'lib/file_system_project.rb', line 6

def root
  @root
end

Instance Method Details

#add_data_map_methodsObject



36
37
38
39
40
41
42
# File 'lib/file_system_project.rb', line 36

def add_data_map_methods
  @data_map.mapper.singleton_methods.each do |meth|
    self.class.send(:define_method, meth) do
      data_map.mapper.send(meth)
    end
  end
end

#add_file(dir, *args) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/file_system_project.rb', line 106

def add_file(dir, *args)
  fail ArgumentError, "Wrong number or type of file arguments." unless valid_adder_args?(args)
  outdir = File.join(@root, dir)
  ensure_dir_exists(outdir)
  content = args[0]
  options = args[1]
  outfile = File.join(outdir, options[:name])
  write_and_sync(outfile, content)
end

#add_to_error_log(name, error) ⇒ Object



19
20
21
# File 'lib/file_system_project.rb', line 19

def add_to_error_log(name, error)
  error_log[name] = error
end

#data_file_exists?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/file_system_project.rb', line 23

def data_file_exists?
  file_system[:data] and File.exists?(data_file_path)
end

#data_file_pathObject



27
28
29
# File 'lib/file_system_project.rb', line 27

def data_file_path
  File.join(root, file_system[:data][:loc], 'data.xml')
end

#data_for(method) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/file_system_project.rb', line 58

def data_for(method)
  begin
    data_map.mapper.send(method)
  rescue
    false
  end
end

#data_has?(method) ⇒ Boolean

Returns:

  • (Boolean)


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

def data_has?(method)
  data and data_for(method)
end

#dir_path(dir) ⇒ Object



89
90
91
# File 'lib/file_system_project.rb', line 89

def dir_path(dir)
  File.join(@root, dir)
end

#directory_accessor(method_name) ⇒ Object



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

def directory_accessor(method_name)
  @file_system[:dirs].keys.find { |d| method_name.match(/^#{d}_files$/) }
end

#directory_adder(method_name) ⇒ Object



70
71
72
# File 'lib/file_system_project.rb', line 70

def directory_adder(method_name)
  @file_system[:dirs].keys.find { |d| method_name.match(/^add_#{d}_file$/) }
end

#ensure_dir_exists(dir) ⇒ Object



122
123
124
# File 'lib/file_system_project.rb', line 122

def ensure_dir_exists(dir)
  Dir.mkdir(dir) unless File.exist?(dir)
end

#file_struct(type) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/file_system_project.rb', line 74

def file_struct(type)
  case type
  when 'xml'
    XMLFileStruct
  when 'yaml'
    YAMLFileStruct
  when 'txt'
    FileStruct
  when 'bin'
    FileStruct
  else
    FileStruct
  end
end

#get_files(dir) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/file_system_project.rb', line 93

def get_files(dir)
  type = @file_system[:dirs][dir][:type]
  locations = Dir.glob(File.join(dir_path(dir), '/**/*')).reject { |d| File.directory?(d) }.sort
  locations.map do |d|
    opts = @file_system[:dirs][dir][:versions] ? {version: get_version(d)} : {}
    file_struct(type).new(d, opts)
  end
end

#get_version(f) ⇒ Object



102
103
104
# File 'lib/file_system_project.rb', line 102

def get_version(f)
  File.dirname(f)[/(?<=\/)[^\/]+$/]
end

#make_data_accessors(file_system) ⇒ Object



31
32
33
34
# File 'lib/file_system_project.rb', line 31

def make_data_accessors(file_system)
  raw_data = File.read(data_file_path)
  DataMapper.new(raw_data)
end

#valid_adder_args?(args) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/file_system_project.rb', line 126

def valid_adder_args?(args)
  args.size == 2 and args[1].is_a?(Hash)
end

#write_and_sync(file, content) ⇒ Object



116
117
118
119
120
# File 'lib/file_system_project.rb', line 116

def write_and_sync(file, content)
  out = File.open(file, 'w')
  out.write(content)
  out.fsync
end