Class: Gruff::Layer

Inherits:
Object
  • Object
show all
Defined in:
lib/gruff/scene.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_dir, folder_name) ⇒ Layer

Returns a new instance of Layer.



133
134
135
136
137
138
# 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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



131
132
133
# File 'lib/gruff/scene.rb', line 131

def name
  @name
end

Instance Method Details

#observe(obj) ⇒ Object

Register this layer so it receives updates from the group



141
142
143
# File 'lib/gruff/scene.rb', line 141

def observe(obj)
  obj.add_observer self
end

#pathObject

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



164
165
166
167
168
169
# 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

#update(value) ⇒ Object

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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# 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