Class: Rant::Generators::DirectedRule

Inherits:
Object
  • Object
show all
Defined in:
lib/rant/import/directedrule.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rac, ch, target_dir, source_dirs, target, source, &block) ⇒ DirectedRule

Returns a new instance of DirectedRule.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rant/import/directedrule.rb', line 62

def initialize(rac, ch, target_dir, source_dirs,
 target, source, &block)
	@rac = rac
	@ch = ch
	@source_dirs = source_dirs
	@target_dir = target_dir
	# target should be a string (file extension)
	@target = target.sub(/^\./, '')
	@target_rx = /#{Regexp.escape(target)}$/
	# source should be a string (file extension)
	@source = source.sub(/^\./, '')
	@esc_target_dir = Regexp.escape(target_dir)
	@block = block
end

Class Method Details

.rant_gen(rac, ch, args, &block) ⇒ Object



9
10
11
12
13
14
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
# File 'lib/rant/import/directedrule.rb', line 9

def self.rant_gen(rac, ch, args, &block)
	unless args.size == 1
 rac.abort_at(ch, "DirectedRule takes one arguments.")
	end
	h = args.first
	if h.respond_to? :to_hash
 h = h.to_hash
	else
 rac.abort_at(ch, "Argument has to be a hash.")
	end
	ts_h, dir_h = nil, nil
	h.each { |k, v| v.respond_to?(:to_ary) ?
 dir_h = { k => v } :
 ts_h = { k => v }
	}
	unless dir_h
 rac.abort_at(ch,
		"Source directory argument has to be a list.")
	end
	target, source = nil, nil
	ts_h.each { |target, source| }
	target_dir, source_dirs = nil, nil
	dir_h.each { |target_dir, source_dirs| }
	if target_dir.respond_to? :to_str
 target_dir = target_dir.to_str
	else
 rac.abort_at(ch, "String required as target directory.")
	end
	if source_dirs.respond_to? :to_ary
 source_dirs = source_dirs.to_ary
	elsif source_dirs.respond_to? :to_str
 source_dirs = [source_dirs.to_str]
	else
 rac.abort_at(ch,
		"List of strings or string required for source directories.")
	end
	target = ".#{target}" if Symbol === target
	source = ".#{source}" if Symbol === source
	if target.respond_to? :to_str
 target = target.to_str
	else
 rac.abort_at(ch, "target has to be a string")
	end
	if source.respond_to? :to_str
 source = source.to_str
	else
 rac.abort_at(ch, "source has to be a string or symbol")
	end
	blk = self.new(rac, ch, target_dir, source_dirs,
 target, source, &block)
	blk.define_hook
	blk
end

Instance Method Details

#[](name, rel_project_dir) ⇒ Object



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
# File 'lib/rant/import/directedrule.rb', line 76

def [](name, rel_project_dir)
	#puts "rule (#@target) for #{name} ?"
	if name =~ /^#@esc_target_dir\// && name =~ @target_rx
 #puts "  matches"
 fn = File.basename(name)
 src_fn = fn.sub_ext(@source)
 #puts "  source filename #{src_fn}"
 src = nil
 @source_dirs.each { |d|
		path = File.join(d, src_fn)
		#puts "  #{path} exist?"
		(src = path) && break if test(?e, path)
 }
 if src
		# pre 0.3.7
		#[@rac.file(:__caller__ => @ch, name => src, &@block)]
		[@rac.prepare_task({name => src}, @block, @ch) { |name,pre,blk|
  @rac.node_factory.new_auto_subfile(@rac, name, pre, blk)
		}]
 else
		nil
 end
	else
 nil
	end
end

#candidatesObject



110
111
112
113
114
# File 'lib/rant/import/directedrule.rb', line 110

def candidates
	sources.map { |src|
 File.join(@target_dir, File.basename(src).sub_ext(@target))
	}
end

#define_hookObject



102
103
104
# File 'lib/rant/import/directedrule.rb', line 102

def define_hook
	@rac.resolve_hooks << self
end

#each_target(&block) ⇒ Object



105
106
107
108
109
# File 'lib/rant/import/directedrule.rb', line 105

def each_target(&block)
	@rac.cx.sys["#@target_dir/*"].each { |entry|
 yield entry if entry =~ @target_rx
	}
end

#sourcesObject



115
116
117
118
119
120
121
122
# File 'lib/rant/import/directedrule.rb', line 115

def sources
	# TODO: returning a file list would be more efficient
	cl = []
	@source_dirs.each { |dir|
 cl.concat(@rac.cx.sys["#{dir}/*.#@source"])
	}
	cl
end