Sha256: 4ccda7b19d5b459f67ae7013ca67fc5f592300f8c9db2d2ec9d06af664d1598b

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

module NYTimes
	module Congress
    module AttributeTransformation
      
      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
      
      def do_transformation(transformation, value)
        send(transformation, value) unless empty?(value)
      end
      
      def date_for(string)
        Date.parse(string)
      end
  
      def string_for(value)
        value.to_s
      end
  
      def integer_for(string)
        if string.respond_to? :to_i
          string.to_i
        else
          string
        end
      end
      
      def symbol_for(string)
        string.to_sym
      end
      
      def roles_for(roles_array)
        roles_array.collect {|e| Role.new e}
      end
      
      def votes_for(votes_array)        
        votes_array.collect {|vote_hash| RollCallVote.new vote_hash['vote'] }
      end
      
      def positions_for(votes_array)        
        votes_array.collect do |vote_hash|
          vote = vote_hash['vote']
          Position.new vote['member_id'], vote['vote_position'], vote
        end
      end
      
      def empty?(value)
        value.nil? || value == "N/A" || value == ""
      end
            
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hoverbird-ny-times-congress-1.0.0 lib/ny-times/congress/attribute_transformation.rb
hoverbird-ny-times-congress-1.1.0 lib/ny-times/congress/attribute_transformation.rb