| Conditions | 5 |
| Total Lines | 74 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | |||
| 4 | def self.parse(*addresses) |
||
| 5 | lines = addresses.grep(String) |
||
| 6 | line = lines.join('').strip |
||
| 7 | |||
| 8 | # empty or <> or < or > |
||
| 9 | if line.empty? || line.match(/\A[<>;, ]+\z/) |
||
| 10 | return [ MailAddress::Address.new(line, nil, line) ] |
||
| 11 | end |
||
| 12 | |||
| 13 | # undisclosed-recipient |
||
| 14 | if line.match(/undisclosed[ \-]recipients?: ?;?/i) |
||
| 15 | return [ MailAddress::Address.new(line, nil, line) ] |
||
| 16 | end |
||
| 17 | |||
| 18 | phrase, address, objs = [], [], [] |
||
| 19 | original = '' |
||
| 20 | depth, idx, end_paren_idx = 0, 0, 0 |
||
| 21 | |||
| 22 | tokens = _tokenize lines |
||
| 23 | len = tokens.length |
||
| 24 | _next = _find_next idx, tokens, len |
||
| 25 | |||
| 26 | for idx in 0 ... len do |
||
| 27 | |||
| 28 | token = tokens[idx] |
||
| 29 | substr = token[0, 1] |
||
| 30 | original << token |
||
| 31 | |||
| 32 | if (end_paren_idx > 0 && end_paren_idx >= idx) |
||
| 33 | next |
||
| 34 | end |
||
| 35 | |||
| 36 | if (substr == '(' && !address.empty?) then |
||
| 37 | end_paren_idx = _find_next_paren(idx, tokens, len) |
||
| 38 | if end_paren_idx == -1 |
||
| 39 | # end paren doesn't exist |
||
| 40 | # but nothing to do |
||
| 41 | end |
||
| 42 | rem = tokens[idx .. end_paren_idx] |
||
| 43 | phrase.push(rem.join('')) |
||
| 44 | elsif (substr == '<') then |
||
| 45 | depth += 1 |
||
| 46 | elsif (substr == '>') then |
||
| 47 | depth -= 1 if depth > 0 |
||
| 48 | elsif (substr == ',' || substr == ';') then |
||
| 49 | original.sub!(/[,;]\s*\z/, '') |
||
| 50 | |||
| 51 | if depth > 0 |
||
| 52 | # raise "Unmatched '<>' in line" |
||
| 53 | o = MailAddress::Address.new(original, nil, original) |
||
| 54 | phrase.clear; address.clear |
||
| 55 | else |
||
| 56 | o = _complete(phrase, address, original) |
||
| 57 | end |
||
| 58 | |||
| 59 | objs.push(o) if o |
||
| 60 | depth = 0 |
||
| 61 | end_paren_idx = 0 |
||
| 62 | original = '' |
||
| 63 | _next = _find_next idx+1, tokens, len |
||
| 64 | elsif (depth > 0) then |
||
| 65 | token.strip! |
||
| 66 | address.push(token) |
||
| 67 | elsif (_next == '<') then |
||
| 68 | phrase.push(token) |
||
| 69 | elsif ( token.match(/^[.\@:;]/) || address.empty? || address[-1].match(/^[.\@:;]/) ) then |
||
| 70 | token.strip! |
||
| 71 | address.push(token) |
||
| 72 | else |
||
| 73 | phrase.push(token) |
||
| 74 | end |
||
| 75 | end |
||
| 76 | objs |
||
| 77 | end |
||
| 78 | |||
| 144 |