Something like this might work, so you can define βcomponentsβ (or whatever) declaratively. This is an extra case for such an example, but you can use it when you need to define tens / hundreds of these things, or you put it as part of some structure (for example, rails).
The component -level method of the component class will usually work in some other module that is included in the class, rather than declaring it inline where it is used like that.
class Device class << self def component(component_name) define_method(component_name) do mode = @data_resource.send("get_#{component_name}_mode", @id) presets = @data_resource.send("get_#{component_name}_presets", @id) summary = "#{component_name} : #{mode} ($#{presets})" presets == "XTC909" ? "* #{summary}" : summary end end end component :display_device component :chip component :input_device def initialize(device_id, data_resource) @id = device_id @data_resource = data_resource end end
You can manage it with something like:
class DataResource def method_missing(method, *args) # puts "called #{method} with:#{args.inspect}" "#{method}-#{args.join(':')}" end end device = Device.new("ID123", DataResource.new) puts device.display_device puts device.chip puts device.input_device
source share