# File lib/gruff/scene.rb, line 133 def initialize(base_dir, folder_name) @base_dir = base_dir.to_s @name = folder_name.to_s @filenames = Dir.open(File.join(base_dir, folder_name)).entries.select { |file| file =~ /^[^.]+\.png$/ } @selected_filename = select_default end
Register this layer so it receives updates from the group
# File lib/gruff/scene.rb, line 141 def observe(obj) obj.add_observer self end
Returns the full path to the selected image, or a blank string
# File lib/gruff/scene.rb, line 164 def path unless @selected_filename.nil? || @selected_filename.empty? return File.join(@base_dir, @name, @selected_filename) end '' end
Choose the appropriate filename for this layer, based on the input
# File lib/gruff/scene.rb, line 146 def update(value) @selected_filename = case value.to_s when /^(true|false)$/ select_boolean value when /^(\w|\s)+$/ select_string value when /^-?(\d+\.)?\d+$/ select_numeric value when /(\d\d):(\d\d):\d\d/ select_time "#{$1}#{$2}" else select_default end # Finally, try to use 'default' if we're still blank @selected_filename ||= select_default end
Returns the string “#{filename}.png”, if it exists.
Failing that, it returns default.png, or '' if that doesn't exist.
# File lib/gruff/scene.rb, line 205 def file_exists_or_blank(filename) @filenames.include?("#{filename}.png") ? "#{filename}.png" : select_default end
Match “true.png” or “false.png”
# File lib/gruff/scene.rb, line 174 def select_boolean(value) file_exists_or_blank value.to_s end
# File lib/gruff/scene.rb, line 198 def select_default @filenames.include?("default.png") ? "default.png" : '' end
Match -5 to _5.png
# File lib/gruff/scene.rb, line 179 def select_numeric(value) file_exists_or_blank value.to_s.gsub('-', '_') end
Match “partly cloudy” to “partly_cloudy.png”
# File lib/gruff/scene.rb, line 194 def select_string(value) file_exists_or_blank value.to_s.gsub(' ', '_') end
# File lib/gruff/scene.rb, line 183 def select_time(value) times = @filenames.map { |filename| filename.gsub('.png', '') } times.each_with_index do |time, index| if (time > value) && (index > 0) return "#{times[index - 1]}.png" end end return "#{times.last}.png" end