Class: Reporter::DataStructure

Inherits:
Object
  • Object
show all
Defined in:
lib/reporter/data_structure.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_set, *args) ⇒ DataStructure

Returns a new instance of DataStructure.



3
4
5
6
# File 'lib/reporter/data_structure.rb', line 3

def initialize data_set, *args
	@data_set = data_set
	@fields = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/reporter/data_structure.rb', line 25

def method_missing(method_name, *args, &block)
	if method_name.to_s =~ /^add_(.*)_field$/
		return send :add, "#{$1}_field", *args, &block
	end
	return send :add, :field, *args, &block if method_name.to_s == "add_field"
	super
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



8
9
10
# File 'lib/reporter/data_structure.rb', line 8

def fields
  @fields
end

Instance Method Details

#<<(column_type, column_alias, *args, &block) ⇒ Object Also known as: add



10
11
12
13
14
15
16
17
# File 'lib/reporter/data_structure.rb', line 10

def << column_type, column_alias, *args, &block
	klass = "reporter/field/#{column_type}".classify.constantize
	column = klass.new self, column_alias, *args, &block
	#TODO: Validate column

	@fields[column_alias] = column
	column
end

#field_value_of(field, options) ⇒ Object



20
21
22
23
# File 'lib/reporter/data_structure.rb', line 20

def field_value_of field, options
	raise "No such field defined: #{field}" unless @fields.has_key? field
	@fields[field].calculate_value(data_set.data_source, options)
end

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/reporter/data_structure.rb', line 33

def respond_to?(method_name)
	return true if method_name.to_s.starts_with? "add_" and method_name.to_s.ends_with? "_field"
	super
end