Class: Collmex::Api::Line
- Inherits:
-
Object
- Object
- Collmex::Api::Line
show all
- Defined in:
- lib/collmex/api.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(arg = nil) ⇒ Line
Returns a new instance of Line.
134
135
136
137
138
139
140
141
|
# File 'lib/collmex/api.rb', line 134
def initialize(arg = nil)
@hash = self.class.default_hash
@hash = @hash.merge(self.class.hashify(arg)) if !arg.nil?
if self.class.specification.empty? && self.class.name.to_s != "Collmex::Api::Line"
raise "#{self.class.name} has no specification"
end
end
|
Class Method Details
.default_hash ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/collmex/api.rb', line 93
def self.default_hash
hash = {}
self.specification.each_with_index do |field_spec, index|
if field_spec.has_key? :fix
hash[field_spec[:name]] = field_spec[:fix]
elsif field_spec.has_key? :default
hash[field_spec[:name]] = field_spec[:default]
else
hash[field_spec[:name]] = Collmex::Api.parse_field(nil, field_spec[:type])
end
end
hash
end
|
.hashify(data) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/collmex/api.rb', line 107
def self.hashify(data)
hash = self.default_hash
fields_spec = self.specification
if data.is_a? Array
fields_spec.each_with_index do |field_spec, index|
if !data[index].nil? && !field_spec.has_key?(:fix)
hash[field_spec[:name]] = Collmex::Api.parse_field(data[index], field_spec[:type])
end
end
elsif data.is_a? Hash
fields_spec.each_with_index do |field_spec, index|
if data.key?(field_spec[:name]) && !field_spec.has_key?(:fix)
hash[field_spec[:name]] = Collmex::Api.parse_field(data[field_spec[:name]], field_spec[:type])
end
end
elsif data.is_a?(String) && parsed = CSV.parse_line(data,Collmex.csv_opts)
fields_spec.each_with_index do |field_spec, index|
if !data[index].nil? && !field_spec.has_key?(:fix)
hash[field_spec[:name]] = Collmex::Api.parse_field(parsed[index], field_spec[:type])
end
end
end
hash
end
|
.specification ⇒ Object
89
90
91
|
# File 'lib/collmex/api.rb', line 89
def self.specification
{}
end
|
Instance Method Details
#to_a ⇒ Object
144
145
146
147
148
149
150
|
# File 'lib/collmex/api.rb', line 144
def to_a
array = []
self.class.specification.each do |spec|
array << @hash[spec[:name]]
end
array
end
|
#to_csv ⇒ Object
161
162
163
164
165
166
167
|
# File 'lib/collmex/api.rb', line 161
def to_csv
array = []
self.class.specification.each do |spec|
array << Collmex::Api.stringify(@hash[spec[:name]], spec[:type])
end
CSV.generate_line(array, Collmex.csv_opts)
end
|
#to_h ⇒ Object
169
170
171
|
# File 'lib/collmex/api.rb', line 169
def to_h
@hash
end
|
#to_stringified_array ⇒ Object
152
153
154
155
156
157
158
|
# File 'lib/collmex/api.rb', line 152
def to_stringified_array
array = []
self.class.specification.each do |spec|
array << Collmex::Api.stringify(@hash[spec[:name]], spec[:type])
end
array
end
|