Permalink
Browse files

First

  • Loading branch information...
0 parents commit c9d9c8cf5bb42ac6461d98b79e15155cf36b3606 @tatey committed Apr 25, 2011
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in conformist.gemspec
+gemspec
21 LICENSE
@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2011 Tate Johnson <tate@tatey.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,7 @@
+# Conformist
+
+Let multiple, different CSVs conform to a single interface without rewriting your parser. This project is a work-in-progress.
+
+## Copyright
+
+Copyright © 2011 Tate Johnson. Conformist is released under the MIT license. See LICENSE for details.
@@ -0,0 +1,10 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+require 'rake/testtask'
+Rake::TestTask.new(:test) do |test|
+ test.libs << 'test'
+ test.pattern = 'test/**/*_test.rb'
+end
+
+task :default => :test
@@ -0,0 +1,21 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "conformist/version"
+
+Gem::Specification.new do |s|
+ s.name = "conformist"
+ s.version = Conformist::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["TODO: Write your name"]
+ s.email = ["TODO: Write your email address"]
+ s.homepage = ""
+ s.summary = %q{TODO: Write a gem summary}
+ s.description = %q{TODO: Write a gem description}
+
+ s.rubyforge_project = "conformist"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+end
@@ -0,0 +1,5 @@
+require 'csv'
+
+require 'conformist/base'
+require 'conformist/column'
+require 'conformist/row'
@@ -0,0 +1,20 @@
+module Conformist
+ module Base
+ def column name, *indexes, &preprocessor
+ columns << Column.new(name, *indexes, &preprocessor)
+ end
+
+ def columns
+ @columns ||= []
+ end
+
+ def options value = nil
+ @options = value if value
+ @options ||= {}
+ end
+
+ def foreach path, &block
+ CSV.foreach(path, options) { |row| yield Row.new(columns, row).to_hash }
+ end
+ end
+end
@@ -0,0 +1,19 @@
+module Conformist
+ class Column
+ attr_reader :name, :indexes, :preprocessor
+
+ def initialize name, *indexes, &preprocessor
+ @name, @indexes, @preprocessor = name, indexes, preprocessor
+ end
+
+ def values_in row
+ values = indexes.map { |index| row[index].strip }
+ values = values.first if values.size == 1
+ if preprocessor.respond_to? :call
+ preprocessor.call values
+ else
+ values
+ end
+ end
+ end
+end
@@ -0,0 +1,17 @@
+module Conformist
+ class Row
+ attr_reader :columns, :row
+
+ def initialize columns, row
+ @columns, @row = columns, row
+ end
+
+ def to_hash
+ columns.inject({}) do |attributes, column|
+ values = column.values_in row
+ values = values.join if values.respond_to? :join
+ attributes.merge(column.name => values)
+ end
+ end
+ end
+end
@@ -0,0 +1,3 @@
+module Conformist
+ VERSION = '0.0.1'
+end
@@ -0,0 +1,29 @@
+require 'helper'
+
+class BaseTest < MiniTest::Unit::TestCase
+ class ACMA
+ extend Conformist::Base
+
+ options :col_sep => ',', :quote_char => '"'
+
+ column :name, 11 do |value|
+ value.match(/[A-Z\s\-]+$/)[0].upcase
+ end
+ column :callsign, 1
+ column :latitude, 15
+ end
+
+ class FCC
+ extend Conformist::Base
+
+ options :col_sep => '|'
+
+ column :name, 10, 11 do |values|
+ "#{values[0].titlecase}, #{values[-1]}"
+ end
+ column :callsign, 1
+ column :latitude, 20, 21, 22, 19 do |values|
+ values.join(' ')
+ end
+ end
+end
@@ -0,0 +1,33 @@
+require 'helper'
+
+class ColumnTest < MiniTest::Unit::TestCase
+ def test_name
+ column = Column.new :foo
+ assert_equal :foo, column.name
+ end
+
+ def test_one_index
+ column = Column.new :foo, 0
+ assert_equal 'a', column.values_in(stub_row)
+ end
+
+ def test_preprocess_with_one_index
+ column = Column.new(:foo, 0) { |value| value.upcase }
+ assert_equal 'A', column.values_in(stub_row)
+ end
+
+ def test_many_indexes
+ column = Column.new :foo, 1, 2, 3
+ assert_equal ['b', 'c', 'd'], column.values_in(stub_row)
+ end
+
+ def test_preprocess_with_many_indexes
+ column = Column.new(:foo, 1, 2, 3) { |values| values.reverse }
+ assert_equal ['d', 'c', 'b'], column.values_in(stub_row)
+ end
+
+ def test_strip_whitespace
+ column = Column.new(:foo, 0)
+ assert_equal 'a', column.values_in([' a '])
+ end
+end
@@ -0,0 +1,21 @@
+require 'helper'
+
+class RowTest < MiniTest::Unit::TestCase
+ def test_to_hash_should_join_array
+ column = Column.new(:foo, 0, 1, 2)
+ row = Row.new [column], stub_row
+ assert_equal({:foo => 'abc'}, row.to_hash)
+ end
+
+ def test_to_hash_should_not_join_string
+ column = Column.new(:foo, 0, 1, 2) { |values| values.join('-').upcase }
+ row = Row.new [column], stub_row
+ assert_equal({:foo => 'A-B-C'}, row.to_hash)
+ end
+
+ def test_to_hash_with_many_columns
+ columns = Column.new(:foo, 0), Column.new(:bar, 1, 2, 3)
+ row = Row.new columns, stub_row
+ assert_equal({:foo => 'a', :bar => 'bcd'}, row.to_hash)
+ end
+end
@@ -0,0 +1,10 @@
+Adelaide,ABS2,64.26,National,H,124,OD,200000,57001,1198821,23139,ntl Tower Summit Road CRAFERS,54,290707,6126723,34 58 49S,138 42 25E,SA,2,,,,Issued
+Adelaide,SAS7,182.26,Commercial,H,122,OD,200000,122001,1139429,23181,Channel 7/10 Site 115 Summit Road CRAFERS,54,290670,6126481,34 58 57S,138 42 24E,SA,7,122,ADELAIDE TV1,162,Issued
+Adelaide,NWS9,196.26,Commercial,H,144,OD,200000,121001,1384392,23132,NWS 9 site 109 Summit Road CRAFERS,54,290698,6126331,34 59 02S,138 42 25E,SA,9,121,ADELAIDE TV1,162,Issued
+Adelaide,ADS10,209.25,Commercial,H,144,OD,200000,120001,1904367,23132,NWS 9 site 109 Summit Road CRAFERS,54,290698,6126331,34 59 02S,138 42 25E,SA,10,120,ADELAIDE TV1,162,Issued
+Adelaide,ADS10,209.25,Commercial,H,122,OD,200000,120001,1384005,23181,Channel 7/10 Site 115 Summit Road CRAFERS,54,290670,6126481,34 58 57S,138 42 24E,SA,10,120,ADELAIDE TV1,162,Issued
+Adelaide,SBS28,527.224,National,H,155,OD,795000,2230001,1153139,23139,ntl Tower Summit Road CRAFERS,54,290707,6126723,34 58 49S,138 42 25E,SA,28,,,,Issued
+Adelaide,CTV31,548.224,Community TV Trial,H,155,OD,30000,10012838,1171360,23139,ntl Tower Summit Road CRAFERS,54,290707,6126723,34 58 49S,138 42 25E,SA,31,,,,Issued
+Adelaide Foothills,SBS43,632.224,National,H,145,DA,4000,2230002,1198817,22133,25 Grenfell Street ADELAIDE,54,280728,6132515,34 55 34S,138 35 58E,SA,43,,,,Issued
+Adelaide Foothills,ABS46,653.25,National,H,145,DA,4000,57002,1150414,22133,25 Grenfell Street ADELAIDE,54,280728,6132515,34 55 34S,138 35 58E,SA,46,,,,Issued
+Adelaide Foothills,SAS49,674.25,Commercial,H,100,DA,4000,122002,1139428,22133,25 Grenfell Street ADELAIDE,54,280728,6132515,34 55 34S,138 35 58E,SA,49,122,ADELAIDE TV1,162,Issued
@@ -0,0 +1,10 @@
+|KVTU-LP |- |LD |3 |DA | |- |- |APP |LOS ANGELES |CA |US |BDFCDVL-20100309ABS |0.25 kW |- |0.0 |- |130176 |N |34 |11 |13.90 |W |117 |42 |1.00 |MARK C. ALLEN | 0.00 km | 0.00 mi | 0.00 deg |1702. m|- |20780 |240. |1015693 |9. |1359585 |
+|NEW |- |LD |3 |ND | |- |- |APP |BAKERSFIELD |CA |US |BMJADVL-20100520ABV |0.3 kW |- |0.0 |- |130049 |N |35 |12 |6.00 |W |119 |5 |26.10 |JOHN R. POWLEY | 0.00 km | 0.00 mi | 0.00 deg |143.2 m|- |100298 |- |1015627 |50. |1368486 |
+|NEW |- |LD |3 |ND | |- |- |APP |YUCA VALLEY |CA |US |BMJADVL-20100520ADI |0.3 kW |- |0.0 |- |129831 |N |34 |9 |10.10 |W |116 |23 |19.10 |JOHN R. POWLEY | 0.00 km | 0.00 mi | 0.00 deg |1179. m|- |100305 |- |1028564 |20. |1370146 |
+|KCSO-LD |- |LD |3 |DA | |- |- |LIC |SACRAMENTO |CA |US |BLDVL -20100825AAQ |0.3 kW |- |0.0 |- |18998 |N |38 |7 |10.00 |W |120 |43 |27.00 |SAINTE 51, L.P. | 0.00 km | 0.00 mi | 0.00 deg |948.5 m|- |96076 |270. |1050375 |114. |1396970 |
+|KVTU-LP |- |TX |3 |DA |+ |- |- |CP MOD |LOS ANGELES |CA |US |BMPTVL -20100826AFT |0.498 kW |- |0.0 |- |130176 |N |34 |13 |38.00 |W |118 |4 |3.00 |MARK C. ALLEN | 0.00 km | 0.00 mi | 0.00 deg |1784. m|- |101585 |0. |1007719 |43. |1397199 |
+|KVTU-LP |- |LD |3 |DA | |- |- |APP |LOS ANGELES |CA |US |BDFCDVL-20100827ABD |0.085 kW |- |0.0 |- |130176 |N |34 |11 |14.00 |W |117 |42 |1.00 |MARK C. ALLEN | 0.00 km | 0.00 mi | 0.00 deg |1634. m|- |101606 |0. |1015693 |8. |1397204 |
+|KCSO-LD |- |LD |3 |DA | |- |- |APP |SACRAMENTO |CA |US |BPDVL -20101102ABA |1.2 kW |- |0.0 |- |18998 |N |38 |7 |10.00 |W |120 |43 |27.00 |SAINTE 51, L.P. | 0.00 km | 0.00 mi | 0.00 deg |948.5 m|- |102795 |270. |1050375 |114. |1405040 |
+|KVTU-LP |- |TX |3 |DA |+ |- |- |LIC |LOS ANGELES |CA |US |BLTVL -20110124AAJ |0.496 kW |- |0.0 |- |130176 |N |34 |14 |18.00 |W |117 |39 |10.00 |MARK C. ALLEN | 0.00 km | 0.00 mi | 0.00 deg |1337. m|- |103475 |2. |- |7. |1414874 |
+|KVTU-LP |- |TX |3 |ND |+ |- |- |APP |LOS ANGELES |CA |US |BPTVL -20110222AEE |0.499 kW |- |0.0 |- |130176 |N |34 |0 |56.00 |W |118 |1 |7.00 |MARK C. ALLEN | 0.00 km | 0.00 mi | 0.00 deg |320. m|- |104131 |- |- |8. |1418705 |
+|K14MW-D |- |LD |3 |ND | |- |- |APP |PETALUMA |CA |US |BMPDVL -20101208ADX |0.3 kW |- |0.0 |- |168824 |N |37 |45 |19.00 |W |122 |27 |6.00 |ONE MINISTRIES, INC | 0.00 km | 0.00 mi | 0.00 deg |370. m|- |104215 |- |1001289 |116. |1418791 |
@@ -0,0 +1,9 @@
+require 'conformist'
+require 'minitest/autorun'
+
+include Conformist
+
+def stub_row
+ ('a'..'d').to_a
+end
+

0 comments on commit c9d9c8c

Please sign in to comment.