Class: ParsedRow

Inherits:
Object
  • Object
show all
Defined in:
app/models/parsed_row.rb

Constant Summary collapse

EXCEPTIONS =

Fields which require special parsing such as dollar amounts

[:amount, :nongift_amount, :deductible_amount]
SHARED_FIELDS =
{
  :first            => [ "First name", "First" ],
  :last             => [ "Last name", "Last" ],
  :email            => [ "Email", "Email address" ]
}
PEOPLE_FIELDS =
SHARED_FIELDS.merge( {
  :salutation       => [ "Salutation" ],     
  :title            => [ "Title" ],
  :company          => [ "Company name", "Company" ],
  :address1         => [ "Address 1", "Address1" ],
  :address2         => [ "Address 2", "Address2" ],
  :city             => [ "City" ],
  :state            => [ "State" ],
  :zip              => [ "Zip", "Zip code" ],
  :country          => [ "Country" ],
  :phone1_type      => [ "Phone1 type", "Phone 1 type" ],
  :phone1_number    => [ "Phone1 number", "Phone 1", "Phone 1 number", "Phone1" ],
  :phone2_type      => [ "Phone2 type", "Phone 2 type" ],
  :phone2_number    => [ "Phone2 number", "Phone 2", "Phone 2 number", "Phone2" ],
  :phone3_type      => [ "Phone3 type", "Phone 3 type" ],
  :phone3_number    => [ "Phone3 number", "Phone 3", "Phone 3 number", "Phone3" ],
  :website          => [ "Website" ],
  :twitter_username => [ "Twitter handle", "Twitter", "Twitter username" ],
  :facebook_page    => [ "Facebook url", "Facebook", "Facebook address", "Facebook page" ],
  :linkedin_page    => [ "Linked in url", "LinkedIn url", "LinkedIn", "LinkedIn address", "LinkedIn page" ],
  :tags             => [ "Tags" ],
  :do_not_email     => [ "Do Not Email" ],
  :person_type      => [ "Person Type" ]   
})
EVENT_FIELDS =
SHARED_FIELDS.merge( {
  :event_name       => [ "Event", "Event Name" ],
  :venue_name       => [ "Venue", "Venue Name" ],
  :show_date        => [ "Show Date", "Show" ],
  :amount           => [ "Amount", "Dollar Amount" ],
  :payment_method   => [ "Payment Method" ],
  :order_date       => [ "Order Date", "Date" ]
})
DONATION_FIELDS =
SHARED_FIELDS.merge( {
  :payment_method   => [ "Payment Method" ],
  :donation_date    => [ "Date", "Order Date" ],
  :donation_type    => [ "Donation Type", "Type" ],
  :amount           => [ "Amount" ],
  :deductible_amount=> [ "Deductible Amount" ],
  
  #Internally it is called nongift_amount but the rest of the world says non-deductible
  :nongift_amount  => [ "Non-Deductible Amount", "Non Deductible Amount" ]
  
  #TODO: Total contribution sanity check
})
FIELDS =
PEOPLE_FIELDS.merge(EVENT_FIELDS).merge(DONATION_FIELDS)
ENUMERATIONS =

Enumerated columns default to the last value if the data value is not valid.

With the way the current code is using instance_variable_get, columns that use an enumeration cannot accept multiple column names. We can only have one column name map to person_type

{
  :person_type => [ "Individual", "Corporation", "Foundation", "Government", "Other" ]
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers, row) ⇒ ParsedRow

Returns a new instance of ParsedRow.



75
76
77
78
79
80
81
82
83
84
# File 'app/models/parsed_row.rb', line 75

def initialize(headers, row)
  @headers = headers
  @row = row

  FIELDS.each do |field, columns|
    columns.each do |column|
      load_value field, column
    end
  end
end

Instance Attribute Details

#rowObject

Returns the value of attribute row.



3
4
5
# File 'app/models/parsed_row.rb', line 3

def row
  @row
end

Class Method Details

.parse(headers, row) ⇒ Object



71
72
73
# File 'app/models/parsed_row.rb', line 71

def self.parse(headers, row)
  ParsedRow.new(headers, row)
end

Instance Method Details

#amountObject



127
128
129
# File 'app/models/parsed_row.rb', line 127

def amount
  ((@amount.to_f || 0) * 100).to_i
end

#check_enumeration(field, value) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'app/models/parsed_row.rb', line 107

def check_enumeration(field, value)
  if enum = ENUMERATIONS[field]      
    if index = enum.map(&:downcase).index(value.to_s.downcase)
      enum[index]
    else
      enum.last
    end
  else
    value
  end
end

#deductible_amountObject



135
136
137
# File 'app/models/parsed_row.rb', line 135

def deductible_amount
  ((@deductible_amount.to_f || 0) * 100).to_i
end

#importing_event?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'app/models/parsed_row.rb', line 143

def importing_event?
  !self.event_name.blank?
end

#load_value(field, column) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/parsed_row.rb', line 86

def load_value(field, column)
  index = @headers.index { |h| h.to_s.downcase.strip == column.downcase }
  value = @row[index] if index
  exist = self.instance_variable_get("@#{field}")

  if exist.blank?
    value = check_enumeration(field, value)

    self.instance_variable_set("@#{field}", value)
    
    #skip amount because we have to parse it
    unless EXCEPTIONS.include? field
      self.class.class_eval { attr_reader field }
    end
  end
end

#nongift_amountObject



119
120
121
# File 'app/models/parsed_row.rb', line 119

def nongift_amount
  ((@nongift_amount.to_f || 0) * 100).to_i
end

#person_attributesObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/models/parsed_row.rb', line 151

def person_attributes
    {
      :email           => self.email,
      :salutation      => self.salutation,
      :title      => self.title,
      :first_name      => self.first,
      :last_name       => self.last,
      :company_name    => self.company,
      :website         => self.website,
      :twitter_handle  => self.twitter_username,
      :facebook_url    => self.facebook_page,
      :linked_in_url   => self.linkedin_page,
      :person_type     => self.person_type
    }
end

#preview(field_name) ⇒ Object



147
148
149
# File 'app/models/parsed_row.rb', line 147

def preview(field_name)
  field_name.to_s.ends_with?("amount") ? self.send("unparsed_#{field_name}") : self.send(field_name)
end

#tags_listObject



103
104
105
# File 'app/models/parsed_row.rb', line 103

def tags_list
  @tags.to_s.strip.gsub(/\s+/, "-").split(/[,|]+/)
end

#unparsed_amountObject



131
132
133
# File 'app/models/parsed_row.rb', line 131

def unparsed_amount
  @amount
end

#unparsed_deductible_amountObject



139
140
141
# File 'app/models/parsed_row.rb', line 139

def unparsed_deductible_amount
  @deductible_amount
end

#unparsed_nongift_amountObject



123
124
125
# File 'app/models/parsed_row.rb', line 123

def unparsed_nongift_amount
  @nongift_amount
end