Sha256: b811674e2b2e846ea3ce9f03ae9e28338ecc04f0925d835e5220f8396b66f697

Contents?: true

Size: 1.47 KB

Versions: 5

Compression:

Stored size: 1.47 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|
          Position.new(vote_hash['member_id'], vote_hash['vote_position'])
        end
      end
      
      def empty?(value)
        value.nil? || value == "N/A" || value == ""
      end
            
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
hoverbird-ny-times-congress-1.2.0 lib/ny-times/congress/attribute_transformation.rb
hoverbird-ny-times-congress-1.2.1 lib/ny-times/congress/attribute_transformation.rb
hoverbird-ny-times-congress-1.2.2 lib/ny-times/congress/attribute_transformation.rb
hoverbird-ny-times-congress-1.3.0 lib/ny-times/congress/attribute_transformation.rb
hoverbird-ny-times-congress-1.3.1 lib/ny-times/congress/attribute_transformation.rb