Module: AnixeCsv::Formatter::InstanceMethods

Defined in:
lib/anixe_csv/formatter.rb

Overview

visible in the including class

Instance Method Summary collapse

Instance Method Details

#[](arg) ⇒ Object

returns the value of the field by name or index



74
75
76
77
78
79
# File 'lib/anixe_csv/formatter.rb', line 74

def [](arg)
	return self.send(arg) if arg.kind_of? String
	return fields.values[arg.to_i] if arg.kind_of? Fixnum

	nil
end

#clear!Object

Reset all fields to nil or their default value



82
83
84
85
# File 'lib/anixe_csv/formatter.rb', line 82

def clear!
	set_field_values([])
	self
end

#delimiterObject



87
88
89
# File 'lib/anixe_csv/formatter.rb', line 87

def delimiter
	singleton.get_delimiter
end

#fieldsObject



91
92
93
# File 'lib/anixe_csv/formatter.rb', line 91

def fields
	singleton.fields
end

#inspectObject



96
97
98
# File 'lib/anixe_csv/formatter.rb', line 96

def inspect
	to_hash
end

#kernel_to_sObject

candy - otherwise formatting definition in field blocks would be called e.g. in irb



95
# File 'lib/anixe_csv/formatter.rb', line 95

alias kernel_to_s to_s

#to_hashObject



100
101
102
103
# File 'lib/anixe_csv/formatter.rb', line 100

def to_hash
	ary = fields.values.collect {|field| [field.name, field.value]}
	Hash[ary]
end

#to_sObject

outputs all fields in one line and formats each field with a block if given



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/anixe_csv/formatter.rb', line 106

def to_s
	values = fields.values.collect do |field|
		value = field.to_s.to_s
		value = value.tr(*singleton.get_escape)
		validate_field(field, value) if validate?

		value
	end

	singleton.rules.each do |rule|
		rule.call(self)
	end

	values.join(delimiter)
end

#validate=(value = true) ⇒ Object



126
127
128
# File 'lib/anixe_csv/formatter.rb', line 126

def validate=(value=true)
	@validate = value
end

#validate?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/anixe_csv/formatter.rb', line 122

def validate?
	@validate
end

#valuesObject



134
135
136
# File 'lib/anixe_csv/formatter.rb', line 134

def values
	fields.values.collect(&:value)
end

#values=(ary) ⇒ Object



130
131
132
# File 'lib/anixe_csv/formatter.rb', line 130

def values=(ary)
	set_field_values(ary)
end