Class: CSVMapper

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/csvmapper.rb,
lib/csvmapper/version.rb

Constant Summary collapse

VERSION =
"0.0.4"
@@schema =
[]
@@ignore_header_line =
false
@@on_error_go_to_next_line =
false
@@has_header =
nil
@@delimiter =
','
@@before_filters =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records = []) ⇒ CSVMapper

Returns a new instance of CSVMapper.



157
158
159
160
161
# File 'lib/csvmapper.rb', line 157

def initialize(records=[])
  @records = records.map{ |hash|
    Hashie::Mash.new(hash)
  }
end

Class Method Details

.before_filter(&block) ⇒ Object



153
154
155
# File 'lib/csvmapper.rb', line 153

def self.before_filter(&block)
  @@before_filters << block
end

.column(field, *args, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/csvmapper.rb', line 19

def self.column(field, *args, &block)
  value = args.shift
  record = {
    field => value,
    :options => args,
    :filter => block
  }
  @@schema << record
end

.columns(*fields) ⇒ Object



29
30
31
32
33
# File 'lib/csvmapper.rb', line 29

def self.columns(*fields)
  fields.each_with_index{ |field, index|
    self.column(field, index, :string)
  }
end

.delimiter(val) ⇒ Object



149
150
151
# File 'lib/csvmapper.rb', line 149

def self.delimiter(val)
  @@delimiter = val
end

.has_header(at = 0) ⇒ Object



35
36
37
# File 'lib/csvmapper.rb', line 35

def self.has_header(at=0)
  @@has_header = at
end

.ignore_header_line(line_count = 1) ⇒ Object



140
141
142
143
# File 'lib/csvmapper.rb', line 140

def self.ignore_header_line(line_count=1)
  @@ignore_header_line = true
  @@ignore_header_line_count = line_count
end

.load(data) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
132
133
134
135
136
137
138
# File 'lib/csvmapper.rb', line 45

def self.load(data)
  buf = CSV.parse(data, :col_sep => @@delimiter)
  if @@ignore_header_line
    @@ignore_header_line_count.times{
      buf.shift()
    }
  end

  records = []
  buf.each_with_index{ |line, index|
    if line.empty?
      next
    end

    if @@has_header == index
      line.each_with_index{ |name, i|
        self.column(name.gsub(" ", "_").downcase, i, :string)
      }
      next
    end

    line = line.map{ |item|
      @@before_filters.each{ |filter|
        item = filter.call(item)
      }
      item
    }

    hash = {}
    @@schema.each{ |opt|
      key = opt.keys.first
      options = opt[:options]

      if options.empty?
        index = opt[key]
        hash[key] = line[index]
      else
        klass = options.first
        index = opt[key]

        if line[index].nil?
          hash[key] = nil
          next
        end

        begin
          case klass
          when :string
            hash[key] = line[index].to_s
          when :numeric
            hash[key] = line[index].to_i
          when :pathname
            hash[key] = Pathname.new(line[index])
          when :time
            hash[key] = Time.parse(line[index])
          when :unixtime
            hash[key] = Time.at(line[index].to_i)
          when :date
            hash[key] = Date.parse(line[index])
          when :boolean
            hash[key] = line[index].to_s == "true" ? true : false
          when :ipaddr
            hash[key] = IPAddr.new(line[index])
          when :uri
            hash[key] = URI.parse(line[index])
          when :regexp
            hash[key] = Regexp.compile(line[index])
          else
            raise "not implemented yet"
          end
        rescue => ex
          puts ex
          puts ex.backtrace.join($/)

          if @@on_error_go_to_next_line
            puts "========"
            puts "Ignore error!!"
            puts "========"
            next
          else
            raise ex
          end
        end

        ## filterは最後に適用
        if opt[:filter]
          hash[key] = opt[:filter].call(hash[key])
        end
      end
    }
    records << hash
  }
  self.new(records)
end

.load_file(path, encoding = nil) ⇒ Object



39
40
41
42
43
# File 'lib/csvmapper.rb', line 39

def self.load_file(path, encoding = nil)
  data = open(path).read
  data.force_encoding(encoding) if encoding
  self.load(data)
end

.on_error_go_to_next_lineObject



145
146
147
# File 'lib/csvmapper.rb', line 145

def self.on_error_go_to_next_line
  @@on_error_go_to_next_line = true
end

Instance Method Details

#[](index) ⇒ Object



180
181
182
# File 'lib/csvmapper.rb', line 180

def [](index)
  row(index)
end

#colsObject



172
173
174
175
176
177
178
# File 'lib/csvmapper.rb', line 172

def cols
  if @records.nil? or @records.empty?
    return 0
  else
    return @records.first.size
  end
end

#each(&block) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/csvmapper.rb', line 185

def each(&block)
  matched = []
  if @where
    @records.each{ |record|
      @where.each{ |key, value|
        if record.send(key) == value
          matched << record
        end
      }
    }
  else
    matched = @records
  end

  matched.each{ |e|
    block.call(e)
  }
end

#grouped_by(field) ⇒ Object



227
228
229
230
231
232
233
234
235
# File 'lib/csvmapper.rb', line 227

def grouped_by(field)
  hash = {}
  each{|e|
    k = e[field]
    hash[k] ||= []
    hash[k] << e
  }
  hash
end

#order(options = {}) ⇒ Object



211
212
213
214
215
# File 'lib/csvmapper.rb', line 211

def order(options={})
  @order = Hashie::Clash.new(options) if @order.nil?
  @order.merge! options
  self
end

#row(index) ⇒ Object



163
164
165
# File 'lib/csvmapper.rb', line 163

def row(index)
  @records[index]
end

#rowsObject

size



168
169
170
# File 'lib/csvmapper.rb', line 168

def rows
  @records.size
end

#to_hashObject



217
218
219
220
221
# File 'lib/csvmapper.rb', line 217

def to_hash
  @records.map{ |record|
    record.to_hash
  }
end

#to_jsonObject



223
224
225
# File 'lib/csvmapper.rb', line 223

def to_json
  @records.to_json
end

#where(options = {}) ⇒ Object

arel style finder



205
206
207
208
209
# File 'lib/csvmapper.rb', line 205

def where(options={})
  @where = Hashie::Clash.new(options) if @where.nil?
  @where.merge! options
  self
end