posifile 0.2.4 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,5 @@
1
- class FieldsNotSpecified < Exception
2
- end
3
- class InvalidFieldName < Exception
4
- end
5
- class InvalidAttrName < Exception
6
- end
1
+ class FieldsNotSpecified < StandardError; end
2
+ class InvalidFieldName < StandardError; end
3
+ class InvalidAttrName < StandardError; end
4
+ class InvalidPositionFileFormat < StandardError; end
5
+
@@ -10,8 +10,14 @@ class Posifile
10
10
 
11
11
  attr_accessor :data_file, :raw_content
12
12
 
13
- def initialize(data_file_name)
14
- @data_file = data_file_name
13
+ def initialize(data_file_name_or_text)
14
+ if data_file_name_or_text.class == String
15
+ @data_file = data_file_name_or_text
16
+ elsif data_file_name_or_text .class == Hash
17
+ @file_content = [data_file_name_or_text[:text]]
18
+ else
19
+ raise InvalidPositionFileFormat, "Pass a file path or text."
20
+ end
15
21
 
16
22
  check_specification_hash
17
23
  file_content.each do |line|
@@ -167,11 +173,15 @@ class Posifile
167
173
  end
168
174
 
169
175
  def file_content
170
- if raw_content.nil?
171
- file = File.open(@data_file, "r")
172
- @raw_content = file.readlines
176
+ if @data_file
177
+ if raw_content.nil?
178
+ file = File.open(@data_file, "r")
179
+ @raw_content = file.readlines
180
+ end
181
+ return @raw_content
182
+ else
183
+ @file_content
173
184
  end
174
- @raw_content
175
185
  end
176
186
 
177
187
  # Get the value of a specifc position declared in the specification hash
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'posifile'
3
- s.version = '0.2.4'
3
+ s.version = '0.2.5'
4
4
  s.summary = 'Ruby library to make it easier to read position files. '
5
5
  s.description = 'Ruby library to make it easier to read position files.'
6
6
  s.files = `git ls-files`.split("\n")
@@ -8,7 +8,7 @@ class TestsChangeName < Test::Unit::TestCase
8
8
 
9
9
  def test_um
10
10
  c = Client.new("samples/sample.txt")
11
- assert_equal "parangaricotirimirru_aro",c.change_name("parangaricotirIMi*rr\&u a-r.o")
11
+ assert_equal "parangaricotirimirru_aro", c.change_name("parangaricotirIMi*rr\&u a-r.o")
12
12
  end
13
13
 
14
14
  end
@@ -11,7 +11,6 @@ class OtherClass < MyClass
11
11
  end
12
12
 
13
13
  class TestInheritance < Test::Unit::TestCase
14
- # Here we test how deep in subclasses the library is gonna work (beacase there are self and self.class references in the code)
15
14
 
16
15
  include TestHelpers
17
16
 
@@ -7,7 +7,7 @@ class MultiLinesWithTwoSpecs3 < Posifile
7
7
 
8
8
  # two specifications, for a multi-line file
9
9
  lines_where 0..2, "001" do
10
- set_specification( "color"=>13..22,"brand"=>3..12)
10
+ set_specification( "color" => 13..22, "brand" => 3..12)
11
11
  set_attr_name :brand
12
12
  end
13
13
  end
@@ -7,14 +7,12 @@ class NoSpec < Posifile
7
7
  end
8
8
 
9
9
  class TestPosifile < Test::Unit::TestCase
10
- # Here are general tests
11
10
 
12
11
  include TestHelpers
13
12
 
14
13
  def setup
15
14
  create_sample_file
16
15
  create_sample_file2
17
- create_sample_invalid
18
16
  end
19
17
 
20
18
  def test_posifile
@@ -36,7 +34,7 @@ class TestPosifile < Test::Unit::TestCase
36
34
 
37
35
  def test_field_value
38
36
  c = Client.new("samples/sample.txt")
39
- assert_equal "jose", c.field_value("name",{"name"=>0..10, "city"=>11..31, "country"=>32..42}, "jose new york brazil ")
37
+ assert_equal "jose", c.field_value("name", {"name" => 0..10, "city" => 11..31, "country" => 32..42}, "jose new york brazil ")
40
38
  end
41
39
 
42
40
  def test_higher
@@ -48,5 +46,12 @@ class TestPosifile < Test::Unit::TestCase
48
46
  no_spec = NoSpec.new("samples/sample.txt")
49
47
  end
50
48
  end
49
+
50
+ def test_passing_position_file_as_text
51
+ client = Client.new(:text => File.open("samples/sample.txt").read)
52
+
53
+ assert_equal "jose", client.field_value("name", {"name" => 0..10, "city" => 11..31, "country" => 32..42}, "jose new york brazil ")
54
+ assert_equal "jose", client.name
55
+ end
51
56
 
52
57
  end
@@ -11,7 +11,7 @@ class TestPosifile < Test::Unit::TestCase
11
11
 
12
12
  def test_set_spec_with_uppercase
13
13
  assert_raise(InvalidFieldName) do
14
- SpecWithUppercase.set_specification("Name"=>0..10, "CITY"=>11..31,"country"=>32..42)
14
+ SpecWithUppercase.set_specification("Name" => 0..10, "CITY" => 11..31, "country" => 32..42)
15
15
  end
16
16
  end
17
17
 
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: posifile
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
5
- prerelease:
4
+ prerelease:
5
+ version: 0.2.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Marco Antonio Fogaça Nogueira
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-25 00:00:00.000000000Z
12
+ date: 2012-02-26 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: Ruby library to make it easier to read position files.
15
15
  email:
@@ -42,26 +42,27 @@ files:
42
42
  - tests/validations/tests_validations.rb
43
43
  homepage: http://github.com/marcofognog/posifile
44
44
  licenses: []
45
- post_install_message:
45
+ post_install_message:
46
46
  rdoc_options: []
47
47
  require_paths:
48
48
  - lib
49
49
  required_ruby_version: !ruby/object:Gem::Requirement
50
- none: false
51
50
  requirements:
52
51
  - - ! '>='
53
52
  - !ruby/object:Gem::Version
54
53
  version: '0'
55
- required_rubygems_version: !ruby/object:Gem::Requirement
56
54
  none: false
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
56
  requirements:
58
57
  - - ! '>='
59
58
  - !ruby/object:Gem::Version
60
59
  version: 1.3.7
60
+ none: false
61
61
  requirements: []
62
- rubyforge_project:
63
- rubygems_version: 1.8.6
64
- signing_key:
62
+ rubyforge_project:
63
+ rubygems_version: 1.8.15
64
+ signing_key:
65
65
  specification_version: 3
66
66
  summary: Ruby library to make it easier to read position files.
67
67
  test_files: []
68
+ ...