Class Gruff::Layer
In: lib/gruff/scene.rb
Parent: Object
StackedMixin StackedBar SideStackedBar StackedArea AccumulatorBar Base Scene Pie Area PhotoBar Spider SideBar Net Bar Line Pie Observable Group SideBar StandardError IncorrectNumberOfDatasetsException Magick Bar Layer BarConversion lib/gruff/stacked_area.rb lib/gruff/scene.rb lib/gruff/spider.rb lib/gruff/pie.rb lib/gruff/area.rb lib/gruff/net.rb lib/gruff/bar_conversion.rb lib/gruff/bar.rb lib/gruff/side_bar.rb lib/gruff/line.rb lib/gruff/stacked_bar.rb lib/gruff/side_stacked_bar.rb lib/gruff/photo_bar.rb lib/gruff/base.rb lib/gruff/accumulator_bar.rb lib/gruff/mini/bar.rb lib/gruff/mini/side_bar.rb lib/gruff/mini/pie.rb Legend Mini Deprecated Gruff dot/m_10_0.png

Methods

Attributes

name  [R] 

Public Class methods

[Source]

     # 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

Public Instance methods

Register this layer so it receives updates from the group

[Source]

     # File lib/gruff/scene.rb, line 141
141:   def observe(obj)
142:     obj.add_observer self
143:   end

Returns the full path to the selected image, or a blank string

[Source]

     # File lib/gruff/scene.rb, line 164
164:   def path
165:     unless @selected_filename.nil? || @selected_filename.empty?
166:       return File.join(@base_dir, @name, @selected_filename)
167:     end
168:     ''
169:   end

Choose the appropriate filename for this layer, based on the input

[Source]

     # 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

Private Instance methods

Returns the string "#{filename}.png", if it exists.

Failing that, it returns default.png, or ’’ if that doesn‘t exist.

[Source]

     # 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"

[Source]

     # File lib/gruff/scene.rb, line 174
174:   def select_boolean(value)
175:     file_exists_or_blank value.to_s
176:   end

[Source]

     # 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

[Source]

     # 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"

[Source]

     # File lib/gruff/scene.rb, line 194
194:   def select_string(value)
195:     file_exists_or_blank value.to_s.gsub(' ', '_')
196:   end

[Source]

     # 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

[Validate]