Module: NYTimes::Congress::AttributeTransformation

Included in:
Congress, Legislator, Role, RollCallVote
Defined in:
lib/ny-times/congress/attribute_transformation.rb

Instance Method Summary collapse

Instance Method Details

#date_for(string) ⇒ Object



21
22
23
# File 'lib/ny-times/congress/attribute_transformation.rb', line 21

def date_for(string)
  Date.parse(string)
end

#do_transformation(transformation, value) ⇒ Object



17
18
19
# File 'lib/ny-times/congress/attribute_transformation.rb', line 17

def do_transformation(transformation, value)
  send(transformation, value) unless empty?(value)
end

#empty?(value) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/ny-times/congress/attribute_transformation.rb', line 55

def empty?(value)
  value.nil? || value == "N/A" || value == ""
end

#integer_for(string) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/ny-times/congress/attribute_transformation.rb', line 29

def integer_for(string)
  if string.respond_to? :to_i
    string.to_i
  else
    string
  end
end

#positions_for(votes_array) ⇒ Object



49
50
51
52
53
# File 'lib/ny-times/congress/attribute_transformation.rb', line 49

def positions_for(votes_array)        
  votes_array.collect do |vote_hash|
    Position.new(vote_hash['member_id'], vote_hash['vote_position'])
  end
end

#roles_for(roles_array) ⇒ Object



41
42
43
# File 'lib/ny-times/congress/attribute_transformation.rb', line 41

def roles_for(roles_array)
  roles_array.collect {|e| Role.new(e)}
end

#string_for(value) ⇒ Object



25
26
27
# File 'lib/ny-times/congress/attribute_transformation.rb', line 25

def string_for(value)
  value.to_s
end

#symbol_for(string) ⇒ Object



37
38
39
# File 'lib/ny-times/congress/attribute_transformation.rb', line 37

def symbol_for(string)
  string.to_sym
end

#transform(args, attributes_map) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/ny-times/congress/attribute_transformation.rb', line 5

def transform(args, attributes_map)
   raise "Can only transform a hash" unless args.kind_of?(Hash)
   new_values = {}
   attributes_map.each_pair do |transformation, attributes|
    attributes.each do |attribute_name|
      value = args[attribute_name.to_s]
    new_values[attribute_name] = do_transformation(transformation, value)
     end
end
new_values
end

#votes_for(votes_array) ⇒ Object



45
46
47
# File 'lib/ny-times/congress/attribute_transformation.rb', line 45

def votes_for(votes_array)        
  votes_array.collect {|vote_hash| RollCallVote.new(vote_hash['vote'])}
end