Class: FunWith::Templates::FilenameVarData

Inherits:
Object
  • Object
show all
Defined in:
lib/fun_with/templates/filename_var_data.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(var_name, method_to_call = nil, num_format = nil) ⇒ FilenameVarData

Returns a new instance of FilenameVarData.



6
7
8
9
10
# File 'lib/fun_with/templates/filename_var_data.rb', line 6

def initialize( var_name, method_to_call = nil, num_format = nil )
  @num_format = num_format
  @var_name = var_name.to_sym
  @method_to_call = method_to_call.to_sym if method_to_call
end

Instance Attribute Details

#method_to_callObject

Returns the value of attribute method_to_call.



4
5
6
# File 'lib/fun_with/templates/filename_var_data.rb', line 4

def method_to_call
  @method_to_call
end

#num_formatObject

Returns the value of attribute num_format.



4
5
6
# File 'lib/fun_with/templates/filename_var_data.rb', line 4

def num_format
  @num_format
end

#var_nameObject Also known as: name

Returns the value of attribute var_name.



4
5
6
# File 'lib/fun_with/templates/filename_var_data.rb', line 4

def var_name
  @var_name
end

Class Method Details

.fill_in_path(template_path, var_data, vars) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/fun_with/templates/filename_var_data.rb', line 33

def self.fill_in_path( template_path, var_data, vars )
  var_data.each do |var_dat|
    template_path = var_dat.fill_in_path( template_path, vars[var_dat.name] )
  end
  
  template_path
end

Instance Method Details

#call_method_on(obj) ⇒ Object

If necessary. If



27
28
29
30
31
# File 'lib/fun_with/templates/filename_var_data.rb', line 27

def call_method_on( obj )
  return obj if self.method_to_call.nil?
  return obj[self.method_to_call] if obj.is_a?(Hash) && obj.has_key?(self.method_to_call)
  return obj.send( self.method_to_call )
end

#fill_in_path(template_path, substitution) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/fun_with/templates/filename_var_data.rb', line 18

def fill_in_path( template_path, substitution )
  return nil if substitution.nil?
  substitution = call_method_on( substitution )
  substitution = self.num_format ? sprintf("%0#{self.num_format.length}i", substitution ) : substitution
  
  template_path.gsub( original_string, substitution.to_s )
end

#original_stringObject



14
15
16
# File 'lib/fun_with/templates/filename_var_data.rb', line 14

def original_string
  "%#{self.num_format}#{self.name}" + (self.method_to_call ? ".#{method_to_call}" : "") + "%"
end