ascii-data-tools 0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. data/.gitignore +3 -0
  2. data/.rvmrc +1 -0
  3. data/.travis.yml +4 -0
  4. data/Gemfile +3 -0
  5. data/Gemfile.lock +40 -0
  6. data/LICENSE.GPL2 +339 -0
  7. data/README.rdoc +52 -0
  8. data/Rakefile +42 -0
  9. data/TODO +4 -0
  10. data/ascii-data-tools.gemspec +30 -0
  11. data/bin/ascii-data-cat +13 -0
  12. data/bin/ascii-data-edit +13 -0
  13. data/bin/ascii-data-norm +13 -0
  14. data/bin/ascii-data-qdiff +13 -0
  15. data/bin/ascii-data-tools-config +9 -0
  16. data/examples/big +10000 -0
  17. data/examples/built_in_records.gz +0 -0
  18. data/examples/slightly_modified_built_in_records.gz +0 -0
  19. data/features/ascii-data-cat.feature +110 -0
  20. data/features/ascii-data-edit.feature +91 -0
  21. data/features/ascii-data-qdiff.feature +54 -0
  22. data/features/encoding_decoding.feature +68 -0
  23. data/features/normaliser.feature +27 -0
  24. data/features/plugins.feature +73 -0
  25. data/features/record_recognition.feature +61 -0
  26. data/features/step_definitions/ascii-data-cat_steps.rb +48 -0
  27. data/features/step_definitions/ascii-data-edit_steps.rb +38 -0
  28. data/features/step_definitions/ascii-data-norm_steps.rb +7 -0
  29. data/features/step_definitions/ascii-data-qdiff_steps.rb +43 -0
  30. data/features/step_definitions/encoding_decoding_steps.rb +23 -0
  31. data/features/step_definitions/plugins_steps.rb +11 -0
  32. data/features/step_definitions/record_recognition_steps.rb +10 -0
  33. data/features/support/env.rb +5 -0
  34. data/lib/ascii-data-tools.rb +8 -0
  35. data/lib/ascii-data-tools/configuration.rb +169 -0
  36. data/lib/ascii-data-tools/configuration_printer.rb +38 -0
  37. data/lib/ascii-data-tools/controller.rb +123 -0
  38. data/lib/ascii-data-tools/discover.rb +19 -0
  39. data/lib/ascii-data-tools/external_programs.rb +23 -0
  40. data/lib/ascii-data-tools/filter.rb +148 -0
  41. data/lib/ascii-data-tools/filter/diffing.rb +139 -0
  42. data/lib/ascii-data-tools/formatting.rb +109 -0
  43. data/lib/ascii-data-tools/global_autodiscovery.rb +21 -0
  44. data/lib/ascii-data-tools/record.rb +50 -0
  45. data/lib/ascii-data-tools/record_type.rb +139 -0
  46. data/lib/ascii-data-tools/record_type/builder.rb +50 -0
  47. data/lib/ascii-data-tools/record_type/decoder.rb +77 -0
  48. data/lib/ascii-data-tools/record_type/encoder.rb +17 -0
  49. data/lib/ascii-data-tools/record_type/field.rb +168 -0
  50. data/lib/ascii-data-tools/record_type/normaliser.rb +38 -0
  51. data/lib/ascii-data-tools/ruby_extensions.rb +7 -0
  52. data/lib/ascii-data-tools/version.rb +3 -0
  53. data/spec/ascii-data-tools/configuration_printer_spec.rb +51 -0
  54. data/spec/ascii-data-tools/configuration_spec.rb +153 -0
  55. data/spec/ascii-data-tools/discover_spec.rb +8 -0
  56. data/spec/ascii-data-tools/filter/diffing_spec.rb +82 -0
  57. data/spec/ascii-data-tools/filter_spec.rb +107 -0
  58. data/spec/ascii-data-tools/formatting_spec.rb +106 -0
  59. data/spec/ascii-data-tools/record_spec.rb +49 -0
  60. data/spec/ascii-data-tools/record_type/builder_spec.rb +69 -0
  61. data/spec/ascii-data-tools/record_type/decoder_spec.rb +73 -0
  62. data/spec/ascii-data-tools/record_type/encoder_spec.rb +32 -0
  63. data/spec/ascii-data-tools/record_type/field_spec.rb +160 -0
  64. data/spec/ascii-data-tools/record_type/normaliser_spec.rb +25 -0
  65. data/spec/ascii-data-tools/record_type_spec.rb +175 -0
  66. data/spec/filter_helper.rb +24 -0
  67. data/spec/record_type_helpers.rb +8 -0
  68. data/spec/spec.opts +2 -0
  69. data/spec/spec_helper.rb +5 -0
  70. metadata +196 -0
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ module AsciiDataTools
4
+ module RecordType
5
+ module Normaliser
6
+ describe Normaliser do
7
+ include RecordTypeHelpers
8
+ before do
9
+ @fields = fields do
10
+ field "field1", :length => 3
11
+ field "field2", :length => 5, :normalised => true
12
+ field "field3", :length => 3, :normalised => true
13
+ field "field4", :length => 1
14
+ end
15
+ @type = Struct.new(:fields).new(@fields)
16
+ @type.extend(Normaliser)
17
+ end
18
+
19
+ it "should X out the characters in fields configured for normalisation" do
20
+ @type.normalise("123ABCDEXYZ\n").should == "123XXXXXXXX\n"
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,175 @@
1
+ require 'spec_helper'
2
+
3
+ module AsciiDataTools
4
+ module RecordType
5
+ describe Type do
6
+ include RecordTypeHelpers
7
+
8
+ before do
9
+ @type = type("ABC") do
10
+ field "field100"
11
+ field "field1"
12
+ field "field10"
13
+ end
14
+ end
15
+
16
+ it "should have a name" do
17
+ @type.name.should == "ABC"
18
+ end
19
+
20
+ it "should provide the field names" do
21
+ @type.field_names.should == ["field100", "field1", "field10"]
22
+ end
23
+
24
+ it "should provide the number of content fields" do
25
+ @type.number_of_content_fields.should == 3
26
+ end
27
+
28
+ it "should provide the length of the field with the longest name" do
29
+ @type.length_of_longest_field_name.should == 8
30
+ end
31
+
32
+ it "should provide an empty constraints description when there are no constraints" do
33
+ @type.constraints_description.should be_empty
34
+ end
35
+
36
+ it "should provide a list of comma-delimited field constraints as the constraints description" do
37
+ @type.field_with_name("field100").should_be_constrained_to("ABC")
38
+ @type.constraints_description.should == "field100 = ABC"
39
+
40
+ @type.field_with_name("field10").should_be_constrained_to("DEF")
41
+ @type.constraints_description.should == "field100 = ABC, field10 = DEF"
42
+ end
43
+
44
+ it "should have an inbuilt filename meta field" do
45
+ @type.field_with_name(:filename).should_not be_nil
46
+ end
47
+
48
+ it "should provide a shortcut for constraining the filename" do
49
+ @type.filename_should_match /abc/
50
+ @type.field_with_name(:filename).constraint_description.should == "filename =~ /abc/"
51
+ end
52
+ end
53
+
54
+ describe FixedLengthType do
55
+ include RecordTypeHelpers
56
+
57
+ before do
58
+ @type = type("ABC") do
59
+ field "field100", :length => 3
60
+ field "field1", :length => 5
61
+ field "field10", :length => 1
62
+ end
63
+ end
64
+
65
+ it "should provide the total length of the fields" do
66
+ @type.total_length_of_fields.should == 9
67
+ end
68
+ end
69
+
70
+ describe UnknownType do
71
+ it "should provide the name" do
72
+ UnknownType.new.name.should == UnknownType::UNKNOWN_RECORD_TYPE_NAME
73
+ end
74
+
75
+ it "should have one field (UNKNOWN)" do
76
+ UnknownType.new.field_names.should == ["UNKNOWN"]
77
+ end
78
+
79
+ it "should decode the entire ascii string into the UNKNOWN field" do
80
+ record = UnknownType.new.decode(:ascii_string => "any string\n")
81
+ record["UNKNOWN"].should == "any string\n"
82
+ end
83
+ end
84
+
85
+ describe TypeDeterminer do
86
+ it "should determine the type that matches" do
87
+ all_types = mock(RecordTypeRepository, :identify_type_for => mock(Type, :name => "ABC"))
88
+ TypeDeterminer.new(all_types).determine_type_for(:ascii_string => "any encoded record").name.should == "ABC"
89
+ end
90
+
91
+ it "should accept the context filename as an optional parameter" do
92
+ all_types = mock(RecordTypeRepository, :identify_type_for => mock(Type, :name => "ABC"))
93
+ TypeDeterminer.new(all_types).determine_type_for(:ascii_string => "any encoded record", :filename => "context filename").name.should == "ABC"
94
+ end
95
+
96
+ it "should determine the type to be unknown when no matching real type can be found" do
97
+ TypeDeterminer.new(mock(RecordTypeRepository, :identify_type_for => nil)).determine_type_for(:ascii_string => "any encoded record").should be_an(UnknownType)
98
+ end
99
+
100
+ it "should cache previously matched types and try to match them first" do
101
+ first_type = mock("type 1")
102
+ second_type = mock("type 2")
103
+
104
+ all_types = mock(RecordTypeRepository)
105
+ all_types.should_receive(:identify_type_for).with(:ascii_string => "record 1").ordered.and_return(first_type)
106
+
107
+ determiner = TypeDeterminer.new(all_types)
108
+ determiner.determine_type_for(:ascii_string => "record 1").should == first_type
109
+
110
+ first_type.should_receive(:able_to_decode?).with(:ascii_string => "record 2").once.ordered.and_return(false)
111
+
112
+ all_types.should_receive(:identify_type_for).with(:ascii_string => "record 2").ordered.and_return(second_type)
113
+
114
+ determiner.determine_type_for(:ascii_string => "record 2").should == second_type
115
+ end
116
+ end
117
+
118
+ describe RecordTypeRepository do
119
+ it "should be enumerable" do
120
+ should be_a_kind_of(Enumerable)
121
+ end
122
+
123
+ it "should have a reset switch" do
124
+ repo = RecordTypeRepository.new
125
+ repo << mock(Type, :name => "ABC") << mock(Type, :name => "DEF")
126
+ repo.clear
127
+ repo.type("ABC").should be_nil
128
+ repo.type("DEF").should be_nil
129
+ end
130
+
131
+ it "should find record types by name" do
132
+ repo = RecordTypeRepository.new
133
+ repo << mock(Type, :name => "ABC") << mock(Type, :name => "DEF")
134
+ repo.find_by_name("ABC").name.should == "ABC"
135
+ repo.type("ABC").name.should == "ABC"
136
+ repo.find_by_name("XYZ").should be_nil
137
+ end
138
+
139
+ it "should find record types which match a certain record" do
140
+ repo = RecordTypeRepository.new
141
+ repo << mock(Type, :name => "ABC", :able_to_decode? => false) << mock(Type, :name => "DEF", :able_to_decode? => :true)
142
+ repo.identify_type_for(:ascii_string => "some record", :filename => "context").name.should == "DEF"
143
+ end
144
+
145
+ it "should store each type only once" do
146
+ type = mock(Type)
147
+ repo = RecordTypeRepository.new([type, type])
148
+ repo.map {|type| type}.should have(1).item
149
+ end
150
+
151
+ it "should find types by name" do
152
+ repo = RecordTypeRepository.new
153
+ repo << mock(Type, :name => "ABC") << mock(Type, :name => "DEF") << mock(Type, :name => "XYZ")
154
+ found_type_names = []
155
+ repo.for_names_matching(/ABC|DEF/) {|type| found_type_names << type.name}
156
+ found_type_names.sort.should == ["ABC", "DEF"]
157
+ end
158
+
159
+ it "should find types by name using a block" do
160
+ repo = RecordTypeRepository.new
161
+ repo << mock(Type, :name => "ABC") << mock(Type, :name => "DEF") << mock(Type, :name => "XYZ")
162
+ found_type_names = []
163
+ matcher = lambda {|name| ["ABC", "DEF"].include?(name) }
164
+ repo.for_names_matching(matcher) {|type| found_type_names << type.name}
165
+ found_type_names.sort.should == ["ABC", "DEF"]
166
+ end
167
+
168
+ it "should include the builder for new types for convenience" do
169
+ repo = RecordTypeRepository.new
170
+ repo.record_type("ABC") { field "xyz", :length => 3 }
171
+ repo.type("ABC").number_of_content_fields.should == 1
172
+ end
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,24 @@
1
+ Spec::Matchers.define :output do |expected_output|
2
+ chain :from_upstream do |*filters|
3
+ @filters = filters.map {|filter| filter.is_a?(String) ? input_source_containing(filter) : filter }
4
+ end
5
+
6
+ match do |filter|
7
+ # connect upstream and downstream
8
+ ([filter] + @filters).each_cons(2) {|downstream, upstream| downstream << upstream }
9
+ output = StringIO.new
10
+
11
+ filter.write(output)
12
+
13
+ @actual_string = output.string
14
+ output.string == expected_output
15
+ end
16
+
17
+ failure_message_for_should do |filter|
18
+ "filter should output #{expected_output.inspect} but instead outputs #{@actual_string.inspect}"
19
+ end
20
+ end
21
+
22
+ def input_source_containing(content)
23
+ AsciiDataTools::InputSource.new("some file", StringIO.new(content))
24
+ end
@@ -0,0 +1,8 @@
1
+ module RecordTypeHelpers
2
+ include AsciiDataTools::RecordType::Builder::TypeBuilder
3
+
4
+ alias :type :build_type
5
+ alias :fields :build_fields
6
+ alias :make_field :build_field
7
+ end
8
+
@@ -0,0 +1,2 @@
1
+ --diff
2
+ --colour
@@ -0,0 +1,5 @@
1
+ lib_path = File.expand_path("#{File.dirname(__FILE__)}/../lib")
2
+ $LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
3
+
4
+ require 'ascii-data-tools'
5
+ require File.join(File.dirname(__FILE__), 'record_type_helpers')
metadata ADDED
@@ -0,0 +1,196 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ascii-data-tools
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.9'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jake Benilov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-07-31 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: terminal-table
16
+ requirement: &2160999340 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.4.2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2160999340
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &2160998940 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2160998940
36
+ - !ruby/object:Gem::Dependency
37
+ name: cucumber
38
+ requirement: &2160998480 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2160998480
47
+ - !ruby/object:Gem::Dependency
48
+ name: ruby-prof
49
+ requirement: &2160998060 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2160998060
58
+ description: A tool for decoding and modifying ASCII CDRs.
59
+ email:
60
+ - benilov@gmail.com
61
+ executables:
62
+ - ascii-data-cat
63
+ - ascii-data-edit
64
+ - ascii-data-norm
65
+ - ascii-data-qdiff
66
+ - ascii-data-tools-config
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - .rvmrc
72
+ - .travis.yml
73
+ - Gemfile
74
+ - Gemfile.lock
75
+ - LICENSE.GPL2
76
+ - README.rdoc
77
+ - Rakefile
78
+ - TODO
79
+ - ascii-data-tools.gemspec
80
+ - bin/ascii-data-cat
81
+ - bin/ascii-data-edit
82
+ - bin/ascii-data-norm
83
+ - bin/ascii-data-qdiff
84
+ - bin/ascii-data-tools-config
85
+ - examples/big
86
+ - examples/built_in_records.gz
87
+ - examples/slightly_modified_built_in_records.gz
88
+ - features/ascii-data-cat.feature
89
+ - features/ascii-data-edit.feature
90
+ - features/ascii-data-qdiff.feature
91
+ - features/encoding_decoding.feature
92
+ - features/normaliser.feature
93
+ - features/plugins.feature
94
+ - features/record_recognition.feature
95
+ - features/step_definitions/ascii-data-cat_steps.rb
96
+ - features/step_definitions/ascii-data-edit_steps.rb
97
+ - features/step_definitions/ascii-data-norm_steps.rb
98
+ - features/step_definitions/ascii-data-qdiff_steps.rb
99
+ - features/step_definitions/encoding_decoding_steps.rb
100
+ - features/step_definitions/plugins_steps.rb
101
+ - features/step_definitions/record_recognition_steps.rb
102
+ - features/support/env.rb
103
+ - lib/ascii-data-tools.rb
104
+ - lib/ascii-data-tools/configuration.rb
105
+ - lib/ascii-data-tools/configuration_printer.rb
106
+ - lib/ascii-data-tools/controller.rb
107
+ - lib/ascii-data-tools/discover.rb
108
+ - lib/ascii-data-tools/external_programs.rb
109
+ - lib/ascii-data-tools/filter.rb
110
+ - lib/ascii-data-tools/filter/diffing.rb
111
+ - lib/ascii-data-tools/formatting.rb
112
+ - lib/ascii-data-tools/global_autodiscovery.rb
113
+ - lib/ascii-data-tools/record.rb
114
+ - lib/ascii-data-tools/record_type.rb
115
+ - lib/ascii-data-tools/record_type/builder.rb
116
+ - lib/ascii-data-tools/record_type/decoder.rb
117
+ - lib/ascii-data-tools/record_type/encoder.rb
118
+ - lib/ascii-data-tools/record_type/field.rb
119
+ - lib/ascii-data-tools/record_type/normaliser.rb
120
+ - lib/ascii-data-tools/ruby_extensions.rb
121
+ - lib/ascii-data-tools/version.rb
122
+ - spec/ascii-data-tools/configuration_printer_spec.rb
123
+ - spec/ascii-data-tools/configuration_spec.rb
124
+ - spec/ascii-data-tools/discover_spec.rb
125
+ - spec/ascii-data-tools/filter/diffing_spec.rb
126
+ - spec/ascii-data-tools/filter_spec.rb
127
+ - spec/ascii-data-tools/formatting_spec.rb
128
+ - spec/ascii-data-tools/record_spec.rb
129
+ - spec/ascii-data-tools/record_type/builder_spec.rb
130
+ - spec/ascii-data-tools/record_type/decoder_spec.rb
131
+ - spec/ascii-data-tools/record_type/encoder_spec.rb
132
+ - spec/ascii-data-tools/record_type/field_spec.rb
133
+ - spec/ascii-data-tools/record_type/normaliser_spec.rb
134
+ - spec/ascii-data-tools/record_type_spec.rb
135
+ - spec/filter_helper.rb
136
+ - spec/record_type_helpers.rb
137
+ - spec/spec.opts
138
+ - spec/spec_helper.rb
139
+ homepage: http://github.com/benilovj/ascii-data-tools
140
+ licenses: []
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ! '>='
149
+ - !ruby/object:Gem::Version
150
+ version: 1.8.7
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ none: false
153
+ requirements:
154
+ - - ! '>='
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements:
158
+ - none
159
+ rubyforge_project:
160
+ rubygems_version: 1.8.5
161
+ signing_key:
162
+ specification_version: 3
163
+ summary: A tool for decoding and modifying ASCII CDRs.
164
+ test_files:
165
+ - features/ascii-data-cat.feature
166
+ - features/ascii-data-edit.feature
167
+ - features/ascii-data-qdiff.feature
168
+ - features/encoding_decoding.feature
169
+ - features/normaliser.feature
170
+ - features/plugins.feature
171
+ - features/record_recognition.feature
172
+ - features/step_definitions/ascii-data-cat_steps.rb
173
+ - features/step_definitions/ascii-data-edit_steps.rb
174
+ - features/step_definitions/ascii-data-norm_steps.rb
175
+ - features/step_definitions/ascii-data-qdiff_steps.rb
176
+ - features/step_definitions/encoding_decoding_steps.rb
177
+ - features/step_definitions/plugins_steps.rb
178
+ - features/step_definitions/record_recognition_steps.rb
179
+ - features/support/env.rb
180
+ - spec/ascii-data-tools/configuration_printer_spec.rb
181
+ - spec/ascii-data-tools/configuration_spec.rb
182
+ - spec/ascii-data-tools/discover_spec.rb
183
+ - spec/ascii-data-tools/filter/diffing_spec.rb
184
+ - spec/ascii-data-tools/filter_spec.rb
185
+ - spec/ascii-data-tools/formatting_spec.rb
186
+ - spec/ascii-data-tools/record_spec.rb
187
+ - spec/ascii-data-tools/record_type/builder_spec.rb
188
+ - spec/ascii-data-tools/record_type/decoder_spec.rb
189
+ - spec/ascii-data-tools/record_type/encoder_spec.rb
190
+ - spec/ascii-data-tools/record_type/field_spec.rb
191
+ - spec/ascii-data-tools/record_type/normaliser_spec.rb
192
+ - spec/ascii-data-tools/record_type_spec.rb
193
+ - spec/filter_helper.rb
194
+ - spec/record_type_helpers.rb
195
+ - spec/spec.opts
196
+ - spec/spec_helper.rb