paf 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 36b4513b6be17e49ed9962e9aafcdbbb4039ee3d
4
- data.tar.gz: 1f2d2a352533a1fcfb6e6d8ccd321f3de40b2c75
3
+ metadata.gz: b233ff56fedadfea9e1f21508ea7b6fb85f852c1
4
+ data.tar.gz: 59dbbde56e750c387774368cad006e4d3e4a0a7c
5
5
  SHA512:
6
- metadata.gz: 409df603f878fafd0b169d39fc65b26e3419b880d3a3bf8bf857bfff53625fedda83c62d64a629de03117fd22f984cbf13954eefe6414e5d67cfd2ff8e1fb0df
7
- data.tar.gz: 00d5ae8fef08d1324f7998b1899451fecb1dc52a9e893f2210020085c12c415905acdd0612f862fed5b2f97079d55dca4e34ca5322498a27d89aac760c3e97b4
6
+ metadata.gz: c379a989578aa2c9a0398f26dddf50d0cce40d15f0ddc8394e54e24bd7c13c016b30734ff3f2375ff985d63caf707d9b024c8720edade1ac3cccbf7a67b78779
7
+ data.tar.gz: 7414a214e0a027bffa8c774f595844a208e75db227af5f2338e9e2f80804088f0e50a0ff39c69f2a5e01412582060d39faa36badd96365ebf630abc8a4a18579
data/README.md CHANGED
@@ -23,7 +23,7 @@ Or install it yourself as:
23
23
  May be used to format the PAF elements as an array of strings:
24
24
 
25
25
  ```ruby
26
- Paf.format(
26
+ Paf.to_a(
27
27
  building_name: '1-2',
28
28
  thoroughfare_name: 'NURSERY',
29
29
  thoroughfare_descriptor: 'LANE',
@@ -35,6 +35,25 @@ Paf.format(
35
35
  ['1-2 NURSERY LANE', 'PENN', 'HIGH WYCOMBE', 'HP10 8LS']
36
36
  ```
37
37
 
38
+ Or as a hash of strings:
39
+
40
+ ```ruby
41
+ Paf.to_h(
42
+ building_name: '1-2',
43
+ thoroughfare_name: 'NURSERY',
44
+ thoroughfare_descriptor: 'LANE',
45
+ dependent_locality: 'PENN',
46
+ post_town: 'HIGH WYCOMBE',
47
+ postcode: 'HP10 8LS'
48
+ )
49
+
50
+ {
51
+ lines: ['1-2 NURSERY LANE', 'PENN'],
52
+ post_town: 'HIGH WYCOMBE',
53
+ postcode: 'HP10 8LS'
54
+ }
55
+ ```
56
+
38
57
  Or as a single string:
39
58
 
40
59
  ```ruby
@@ -61,10 +80,26 @@ paf = Paf.new(
61
80
  post_town: 'HIGH WYCOMBE',
62
81
  postcode: 'HP10 8LS'
63
82
  )
64
- paf.format
83
+ paf.to_a
65
84
 
66
85
  ['1-2 NURSERY LANE', 'PENN', 'HIGH WYCOMBE', 'HP10 8LS']
67
86
 
87
+ paf = Paf.new(
88
+ building_name: '1-2',
89
+ thoroughfare_name: 'NURSERY',
90
+ thoroughfare_descriptor: 'LANE',
91
+ dependent_locality: 'PENN',
92
+ post_town: 'HIGH WYCOMBE',
93
+ postcode: 'HP10 8LS'
94
+ )
95
+ paf.to_h
96
+
97
+ {
98
+ lines: ['1-2 NURSERY LANE', 'PENN'],
99
+ post_town: 'HIGH WYCOMBE',
100
+ postcode: 'HP10 8LS'
101
+ }
102
+
68
103
  paf = Paf.new(
69
104
  building_name: '1-2',
70
105
  thoroughfare_name: 'NURSERY',
data/lib/paf.rb CHANGED
@@ -35,7 +35,7 @@ class Paf
35
35
  private
36
36
 
37
37
  def concatenated(attrs)
38
- value = attrs.map { |attr| send(attr) }.condense.join(' ')
38
+ value = attrs.map { |attr| send(attr) }.condense(' ')
39
39
  value unless value.vacant?
40
40
  end
41
41
  end
@@ -0,0 +1,15 @@
1
+ require 'paf/formatter'
2
+
3
+ class Paf
4
+ # Processing to format a PAF entry as an array
5
+ class ArrayFormatter < Formatter
6
+ def format(paf)
7
+ super(paf)
8
+ lines.clone.tap do |array|
9
+ post_attrs.each do |attr|
10
+ array << send(attr) unless send(attr).vacant?
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -4,8 +4,8 @@ class Paf
4
4
  module CoreExt
5
5
  # Extend the core Array class with PAF specific processing
6
6
  module Array
7
- def condense
8
- reject(&:vacant?)
7
+ def condense(separator)
8
+ reject(&:vacant?).join(separator)
9
9
  end
10
10
  end
11
11
  end
@@ -2,12 +2,20 @@ class Paf
2
2
  module CoreExt
3
3
  # Extend the core String class with PAF specific processing
4
4
  module String
5
- def paf_exception?
5
+ def exception?
6
6
  !/^(.|[\d][[:alpha:]]|[\d].*?[\d][[:alpha:]]?)$/.match(self).nil?
7
7
  end
8
8
 
9
- def paf_split_exception?
10
- paf_exception? && /^\d+$/.match(self).nil?
9
+ def split_exception?
10
+ last_word.exception? && /^\d+$/.match(last_word).nil?
11
+ end
12
+
13
+ def last_word
14
+ split.last
15
+ end
16
+
17
+ def but_last_word
18
+ self[0...rindex(' ')]
11
19
  end
12
20
  end
13
21
  end
@@ -1,47 +1,49 @@
1
- require 'paf/lineable'
2
- require 'paf/premises'
3
- require 'paf/thoroughfare_locality'
1
+ require 'paf/array_formatter'
2
+ require 'paf/hash_formatter'
3
+ require 'paf/string_formatter'
4
4
 
5
5
  class Paf
6
6
  # Processing to format a PAF entry
7
7
  module Formattable
8
+ attr_accessor :formatter, :concatenation_indicator
9
+ private :concatenation_indicator=
10
+
8
11
  def self.included(base)
12
+ base.prepend Initializer
9
13
  base.extend ClassMethods
10
- base.class_eval do
11
- include Lineable
12
- include Premises
13
- include ThoroughfareLocality
14
- end
15
14
  end
16
15
  private_class_method :included
17
16
 
17
+ # initialize method to be prepended to the including class
18
+ module Initializer
19
+ def initialize(*)
20
+ super if defined? super
21
+ @formatter ||= Paf::ArrayFormatter.new
22
+ end
23
+ end
24
+
18
25
  # Methods to be added to the including class
19
26
  module ClassMethods
20
- # Formats a hash of PAF address elements into an array of strings
21
- def format(args)
22
- new(args).format
27
+ %i[to_a to_h format].each do |method|
28
+ define_method method do |args|
29
+ new(args).send(method)
30
+ end
23
31
  end
24
32
 
25
- # Formats a hash of PAF address elements into a string
26
33
  def to_s(*args)
27
34
  return super if args.empty?
28
35
  new(args[0]).to_s
29
36
  end
30
37
  end
31
38
 
32
- # Formats a Paf instance into an array of strings
33
- def format
34
- array = lines
35
- self.class.post_attrs.each do |attr|
36
- array << send(attr) unless send(attr).vacant?
39
+ { to_a: :Array, to_h: :Hash, to_s: :String }.each do |key, value|
40
+ define_method key do
41
+ Kernel.const_get("Paf::#{value}Formatter").format(self)
37
42
  end
38
- array
39
43
  end
40
44
 
41
- # Formats a Paf instance into a string
42
- def to_s
43
- string = (lines + [post_town]).condense.join(', ')
44
- ([string] + [postcode]).condense.join('. ')
45
+ def format
46
+ formatter.format(self)
45
47
  end
46
48
  end
47
49
  end
@@ -0,0 +1,25 @@
1
+ require 'paf/lineable'
2
+
3
+ class Paf
4
+ # Processing to format a PAF entry
5
+ class Formatter
6
+ def self.format(paf)
7
+ new.format(paf)
8
+ end
9
+
10
+ def format(paf)
11
+ @paf = paf.clone
12
+ @paf.extend Lineable
13
+ end
14
+
15
+ def method_missing(method, *args)
16
+ return @paf.send(method, *args) if @paf.respond_to?(method)
17
+ return @paf.class.send(method, *args) if @paf.class.respond_to?(method)
18
+ super
19
+ end
20
+
21
+ def respond_to_missing?(method_name, include_private = false)
22
+ @paf.respond_to?(method) || @paf.class.respond_to?(method) || super
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ require 'paf/formatter'
2
+
3
+ class Paf
4
+ # Processing to format a PAF entry as a hash
5
+ class HashFormatter < Formatter
6
+ def format(paf)
7
+ super(paf)
8
+ {}.tap do |hash|
9
+ ([:lines] + post_attrs).each do |attr|
10
+ hash[attr] = send(attr) unless send(attr).vacant?
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,27 +1,28 @@
1
+ require 'paf/premises'
2
+ require 'paf/thoroughfare_locality'
3
+
1
4
  class Paf
2
5
  # Processing to format PAF entry lines
3
6
  module Lineable
4
- def self.included(base)
5
- base.extend ClassMethods
7
+ def self.extended(base)
8
+ base.extend Premises
9
+ base.extend ThoroughfareLocality
6
10
  end
7
- private_class_method :included
8
11
 
9
- # Methods to be added to the including class
10
- module ClassMethods
11
- def lines_methods
12
- organisation_attrs + %i[po_box premises thoroughfares_and_localities]
12
+ def lines
13
+ [].tap do |lines|
14
+ lines_methods.each do |method|
15
+ value = send(method)
16
+ (lines << value).flatten! unless value.vacant?
17
+ end
13
18
  end
14
19
  end
15
20
 
16
21
  private
17
22
 
18
- def lines
19
- lines = []
20
- self.class.lines_methods.each do |method|
21
- value = send(method)
22
- (lines << value).flatten! unless value.vacant?
23
- end
24
- lines
23
+ def lines_methods
24
+ self.class.organisation_attrs +
25
+ %i[po_box premises thoroughfares_and_localities]
25
26
  end
26
27
  end
27
28
  end
@@ -5,26 +5,17 @@ class Paf
5
5
  module Premises
6
6
  include Common
7
7
 
8
- def self.included(base)
9
- base.prepend Initializer
8
+ def self.extended(base)
9
+ base.extend_premises_rule
10
10
  end
11
- private_class_method :included
12
-
13
- # initialize method to be prepended to the including class
14
- module Initializer
15
- def initialize(*)
16
- super if defined? super
17
- extend_premises_rule
18
- end
19
- end
20
-
21
- private
22
11
 
23
12
  def extend_premises_rule
24
13
  require premises_rule_filename
25
14
  extend premises_rule_module
26
15
  end
27
16
 
17
+ private
18
+
28
19
  def premises_rule_filename
29
20
  "paf/premises/rule#{rule_key}"
30
21
  end
@@ -2,9 +2,6 @@ class Paf
2
2
  module Premises
3
3
  # Common processing for premises elements of a PAF entry
4
4
  module Common
5
- attr_accessor :concatenation_indicator
6
- private :concatenation_indicator=
7
-
8
5
  private
9
6
 
10
7
  def concatenate?
@@ -2,12 +2,12 @@ class Paf
2
2
  module Premises
3
3
  # Processing for premises elements of a PAF entry under Rule 1
4
4
  module Rule000
5
- private
6
-
7
5
  def premises
8
6
  []
9
7
  end
10
8
 
9
+ private
10
+
11
11
  def premises_includes_first_thoroughfare_or_locality?
12
12
  false
13
13
  end
@@ -2,12 +2,12 @@ class Paf
2
2
  module Premises
3
3
  # Processing for premises elements of a PAF entry under Rule 2
4
4
  module Rule001
5
- private
6
-
7
5
  def premises
8
6
  [number_and_thoroughfare_or_locality]
9
7
  end
10
8
 
9
+ private
10
+
11
11
  def premises_includes_first_thoroughfare_or_locality?
12
12
  true
13
13
  end
@@ -2,35 +2,25 @@ class Paf
2
2
  module Premises
3
3
  # Processing for premises elements of a PAF entry under Rule 3
4
4
  module Rule010
5
- private
6
-
7
5
  def premises
8
- return [name_and_thoroughfare_or_locality] if
9
- building_name.paf_exception?
10
- if building_name_last_word.paf_split_exception?
6
+ return [name_and_thoroughfare_or_locality] if building_name.exception?
7
+ if building_name.split_exception?
11
8
  return [
12
- building_name_but_last_word,
9
+ building_name.but_last_word,
13
10
  name_last_word_and_thoroughfare_or_locality
14
11
  ]
15
12
  end
16
13
  [building_name]
17
14
  end
18
15
 
19
- def premises_includes_first_thoroughfare_or_locality?
20
- building_name.paf_exception? ||
21
- building_name_last_word.paf_split_exception?
22
- end
23
-
24
- def building_name_but_last_word
25
- building_name[0...building_name.rindex(' ')]
26
- end
16
+ private
27
17
 
28
- def building_name_last_word
29
- building_name.split.last
18
+ def premises_includes_first_thoroughfare_or_locality?
19
+ building_name.exception? || building_name.split_exception?
30
20
  end
31
21
 
32
22
  def name_last_word_and_thoroughfare_or_locality
33
- "#{building_name_last_word} #{first_thoroughfare_or_locality}"
23
+ "#{building_name.last_word} #{first_thoroughfare_or_locality}"
34
24
  end
35
25
  end
36
26
  end
@@ -2,12 +2,12 @@ class Paf
2
2
  module Premises
3
3
  # Processing for premises elements of a PAF entry under Rule 4
4
4
  module Rule011
5
- private
6
-
7
5
  def premises
8
6
  [building_name, number_and_thoroughfare_or_locality]
9
7
  end
10
8
 
9
+ private
10
+
11
11
  def premises_includes_first_thoroughfare_or_locality?
12
12
  true
13
13
  end
@@ -2,13 +2,13 @@ class Paf
2
2
  module Premises
3
3
  # Processing for premises elements of a PAF entry under Rule 5
4
4
  module Rule101
5
- private
6
-
7
5
  def premises
8
6
  return [number_sub_name_and_thoroughfare_or_locality] if concatenate?
9
7
  [sub_building_name, number_and_thoroughfare_or_locality]
10
8
  end
11
9
 
10
+ private
11
+
12
12
  def premises_includes_first_thoroughfare_or_locality?
13
13
  true
14
14
  end
@@ -2,17 +2,17 @@ class Paf
2
2
  module Premises
3
3
  # Processing for premises elements of a PAF entry under Rule 6
4
4
  module Rule110
5
- private
6
-
7
5
  def premises
8
- return [sub_name_and_name] if sub_building_name.paf_exception?
6
+ return [sub_name_and_name] if sub_building_name.exception?
9
7
  return [sub_building_name, name_and_thoroughfare_or_locality] if
10
- building_name.paf_exception?
8
+ building_name.exception?
11
9
  [sub_building_name, building_name]
12
10
  end
13
11
 
12
+ private
13
+
14
14
  def premises_includes_first_thoroughfare_or_locality?
15
- !sub_building_name.paf_exception? && building_name.paf_exception?
15
+ !sub_building_name.exception? && building_name.exception?
16
16
  end
17
17
  end
18
18
  end
@@ -2,14 +2,14 @@ class Paf
2
2
  module Premises
3
3
  # Processing for premises elements of a PAF entry under Rule 7
4
4
  module Rule111
5
- private
6
-
7
5
  def premises
8
6
  return [sub_name_and_name, number_and_thoroughfare_or_locality] if
9
- sub_building_name.paf_exception?
7
+ sub_building_name.exception?
10
8
  [sub_building_name, building_name, number_and_thoroughfare_or_locality]
11
9
  end
12
10
 
11
+ private
12
+
13
13
  def premises_includes_first_thoroughfare_or_locality?
14
14
  true
15
15
  end
@@ -0,0 +1,11 @@
1
+ require 'paf/formatter'
2
+
3
+ class Paf
4
+ # Processing to format a PAF entry as a string
5
+ class StringFormatter < Formatter
6
+ def format(paf)
7
+ super(paf)
8
+ ([(lines + [post_town]).condense(', ')] + [postcode]).condense('. ')
9
+ end
10
+ end
11
+ end
@@ -1,37 +1,29 @@
1
1
  class Paf
2
2
  # Processing for thoroughfare and locality elements of a PAF entry
3
3
  module ThoroughfareLocality
4
- def self.included(base)
5
- base.extend ClassMethods
4
+ def thoroughfare_and_locality_attrs
5
+ %i[dependent_thoroughfare thoroughfare] + self.class.locality_attrs
6
6
  end
7
- private_class_method :included
8
7
 
9
- # Methods to be added to the including class
10
- module ClassMethods
11
- def thoroughfare_and_locality_attrs
12
- %i[dependent_thoroughfare thoroughfare] + locality_attrs
8
+ def thoroughfares_and_localities
9
+ [].tap do |array|
10
+ thoroughfare_and_locality_attrs.each do |attr|
11
+ next if used?(attr)
12
+ value = send(attr)
13
+ array << value unless value.vacant?
14
+ end
13
15
  end
14
16
  end
15
17
 
16
18
  private
17
19
 
18
- def thoroughfares_and_localities
19
- array = []
20
- self.class.thoroughfare_and_locality_attrs.each do |attr|
21
- next if used?(attr)
22
- value = send(attr)
23
- array << value unless value.vacant?
24
- end
25
- array
26
- end
27
-
28
20
  def first_thoroughfare_or_locality
29
21
  send(first_thoroughfare_or_locality_attr) unless
30
22
  first_thoroughfare_or_locality_attr.nil?
31
23
  end
32
24
 
33
25
  def first_thoroughfare_or_locality_attr
34
- self.class.thoroughfare_and_locality_attrs.find do |attr|
26
+ thoroughfare_and_locality_attrs.find do |attr|
35
27
  !send(attr).vacant?
36
28
  end
37
29
  end
@@ -1,4 +1,4 @@
1
1
  # PAF gem version
2
2
  class Paf
3
- VERSION = '0.3.0'.freeze
3
+ VERSION = '0.4.0'.freeze
4
4
  end
@@ -9,6 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.version = Paf::VERSION
10
10
  spec.authors = ['John Bard']
11
11
  spec.email = ['johnbard@globalnet.co.uk']
12
+ spec.homepage = 'https://github.com/drabjay/paf'
12
13
 
13
14
  spec.summary = 'Royal Mail Postcode Address File (PAF) Formatter'
14
15
  spec.description = <<-EOF
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Bard
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-29 00:00:00.000000000 Z
11
+ date: 2017-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -103,11 +103,14 @@ files:
103
103
  - bin/console
104
104
  - bin/setup
105
105
  - lib/paf.rb
106
+ - lib/paf/array_formatter.rb
106
107
  - lib/paf/attribute.rb
107
108
  - lib/paf/core_ext/array.rb
108
109
  - lib/paf/core_ext/object.rb
109
110
  - lib/paf/core_ext/string.rb
110
111
  - lib/paf/formattable.rb
112
+ - lib/paf/formatter.rb
113
+ - lib/paf/hash_formatter.rb
111
114
  - lib/paf/lineable.rb
112
115
  - lib/paf/premises.rb
113
116
  - lib/paf/premises/common.rb
@@ -118,10 +121,11 @@ files:
118
121
  - lib/paf/premises/rule101.rb
119
122
  - lib/paf/premises/rule110.rb
120
123
  - lib/paf/premises/rule111.rb
124
+ - lib/paf/string_formatter.rb
121
125
  - lib/paf/thoroughfare_locality.rb
122
126
  - lib/paf/version.rb
123
127
  - paf.gemspec
124
- homepage:
128
+ homepage: https://github.com/drabjay/paf
125
129
  licenses:
126
130
  - MIT
127
131
  metadata: {}