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



76
77
78
79
80
81
# File 'lib/anixe_csv/formatter.rb', line 76

def [](arg)
	return data[arg.to_sym] if arg.kind_of? String
	return data.values[arg.to_i] if arg.kind_of? Fixnum

	nil
end

#[]=(key, value) ⇒ Object



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

def []=(key, value)
  data[key.to_sym] = value
end

#clear!Object

Reset all fields to nil or their default value



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

def clear!
	set_field_values([])
	self
end

#dataObject Also known as: to_hash, inspect



103
104
105
# File 'lib/anixe_csv/formatter.rb', line 103

def data
  @data ||= set_field_values([])
end

#delimiterObject



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

def delimiter
	singleton.get_delimiter
end

#fieldsObject



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

def fields
	singleton.fields
end

#kernel_to_sObject

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



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

alias kernel_to_s to_s

#to_sObject

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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/anixe_csv/formatter.rb', line 111

def to_s
  values = fields.values.collect do |field|
    value = field.to_s(data[field.name]).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



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

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

#validate?Boolean

Returns:

  • (Boolean)


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

def validate?
  @validate
end

#valuesObject



139
140
141
# File 'lib/anixe_csv/formatter.rb', line 139

def values
  data.values
end

#values=(ary) ⇒ Object



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

def values=(ary)
  set_field_values(ary)
end