Class: Effect

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/effect.rb

Overview

“./effects/Digital Economy Bill Table of Effects.csv”

Class Method Summary collapse

Class Method Details

.load(path) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/effect.rb', line 11

def load path
  
  first_line = File.open("#{path}").readline
  parts = first_line.split(',')
  if !(parts[0].strip == "Date" &&
     parts[1].strip == "Affected Legislation (Act)" &&
     parts[2].strip == "Affected Provision" &&
     parts[3].strip == "Type of Effect" &&
     parts[4].strip == "Affecting Legislation (Year and Chapter or Number)" &&
     parts[5].strip == "Affecting Provision" &&
     parts[6].strip == "Amendment applied to Database" &&
     parts[7].strip == "Checked (Y or leave Blank)" &&
     parts[8].strip == "Transferred to Final TOES Chart (Date)")
    raise "error: unexpected csv header format for effects"
  end
  
  FasterCSV.foreach("#{path}", {:headers => true}) do |row|
    bill = Bill.find_by_name(row["Affecting Legislation (Year and Chapter or Number)"])
    if bill
      reference_to_bill = row["Affecting Provision "]
      response = parse_bill_provision_reference(reference_to_bill)
      reference_parsed = response[1]
      html_ref = response[0]
      
      unless html_ref
        warn "unable to handle reference to bill: #{reference_to_bill} in effects file" unless RAILS_ENV == 'test'
      end
      
      while !reference_to_bill.blank? && html_ref
        unless Effect.find_by_bill_id_and_bill_provision_and_affected_act_and_affected_act_provision_and_type_of_effect(bill.id, html_ref, row["Affected Legislation (Act)"], row["Affected Provision"], row["Type of Effect"])
          Effect.create(
            :bill_id => bill.id,
            :bill_provision => html_ref,
            :affected_act => row["Affected Legislation (Act)"],
            :affected_act_provision => row["Affected Provision"],
            :type_of_effect => row["Type of Effect"]
          )
        end
        reference_to_bill.gsub!(reference_parsed, '').strip
        unless reference_to_bill.blank?
          response = parse_bill_provision_reference(reference_to_bill)
          reference_parsed = response[1]
          html_ref = response[0]
        end
      end
    end
  end    
end

.parse_bill_provision_reference(reference) ⇒ Object


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/effect.rb', line 60

def parse_bill_provision_reference reference
  parsed_ref = nil
  found_ref = ""
  
  if reference =~ /(^s. 0*(\d+)(?:\(0*(\d+)\))?(?:\((\w+)\))?)/
    parsed_ref = "clause#{$2}"
    parsed_ref += "-#{$3}" if $3
    parsed_ref += "-#{$4}" if $4
    found_ref = $1
  elsif reference =~ /(Sch. 0*(\d)(?: para. 0*(\d+))?(?:\(0*(\d+)\))?(?:\(0*(\w+)\))?)/
    parsed_ref = "schedule#{$2}"
    parsed_ref += "-#{$3}" if $3
    parsed_ref += "-#{$4}" if $4
    parsed_ref += "-#{$5}" if $5
    found_ref = $1
  end
  [parsed_ref, found_ref]
end