Class | Gruff::Layer |
In: |
lib/gruff/scene.rb
|
Parent: | Object |
name | [R] |
# File lib/gruff/scene.rb, line 133 133: def initialize(base_dir, folder_name) 134: @base_dir = base_dir.to_s 135: @name = folder_name.to_s 136: @filenames = Dir.open(File.join(base_dir, folder_name)).entries.select { |file| file =~ /^[^.]+\.png$/ } 137: @selected_filename = select_default 138: end
Register this layer so it receives updates from the group
# File lib/gruff/scene.rb, line 141 141: def observe(obj) 142: obj.add_observer self 143: end
Choose the appropriate filename for this layer, based on the input
# File lib/gruff/scene.rb, line 146 146: def update(value) 147: @selected_filename = case value.to_s 148: when /^(true|false)$/ 149: select_boolean value 150: when /^(\w|\s)+$/ 151: select_string value 152: when /^-?(\d+\.)?\d+$/ 153: select_numeric value 154: when /(\d\d):(\d\d):\d\d/ 155: select_time "#{$1}#{$2}" 156: else 157: select_default 158: end 159: # Finally, try to use 'default' if we're still blank 160: @selected_filename ||= select_default 161: 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 205: def file_exists_or_blank(filename) 206: @filenames.include?("#{filename}.png") ? "#{filename}.png" : select_default 207: end
Match "true.png" or "false.png"
# File lib/gruff/scene.rb, line 174 174: def select_boolean(value) 175: file_exists_or_blank value.to_s 176: end
# File lib/gruff/scene.rb, line 198 198: def select_default 199: @filenames.include?("default.png") ? "default.png" : '' 200: end
Match -5 to _5.png
# File lib/gruff/scene.rb, line 179 179: def select_numeric(value) 180: file_exists_or_blank value.to_s.gsub('-', '_') 181: end
Match "partly cloudy" to "partly_cloudy.png"
# File lib/gruff/scene.rb, line 194 194: def select_string(value) 195: file_exists_or_blank value.to_s.gsub(' ', '_') 196: end
# File lib/gruff/scene.rb, line 183 183: def select_time(value) 184: times = @filenames.map { |filename| filename.gsub('.png', '') } 185: times.each_with_index do |time, index| 186: if (time > value) && (index > 0) 187: return "#{times[index - 1]}.png" 188: end 189: end 190: return "#{times.last}.png" 191: end