Skip to content

Instantly share code, notes, and snippets.

View bdarfler's full-sized avatar

Ben Darfler bdarfler

View GitHub Profile
class User < ActiveRecord::Base
def recent_activities(limit)
c_stream = comments.order_by(:created_at).to_enum :find_each
i_stream = items.comments.order_by(:created_at).to_enum :find_each
b_stream = bids.order_by(:created_at).to_enum :find_each
ActivityFeedAggregator.new([c_stream, i_stream, b_stream]).next_activities(limit)
end
end
class ActivityAggregator
def initialize(streams)
@streams = streams
end
def next_activities(limit)
activities = []
while (activities.size < limit) && more_activites? do
activities << next_activity
end
activities
class User < ActiveRecord::Base
def recent_activities(limit)
c_stream = comments.order_by(:created_at).to_enum :find_each
i_stream = items.comments.order_by(:created_at).to_enum :find_each
b_stream = bids.order_by(:created_at).to_enum :find_each
end
end
class User < ActiveRecord::Base
def recent_activities(limit)
[comments, items.map(&:comments), bids].
flatten.
sort_by(&:created_at).
first(limit)
end
end
val n = wordsList.length
val wLength = List.newBuilder[Int]
val wCaps = List.newBuilder[Boolean]
for( word <- wordsList ) {
 wLength += word.length
 wCaps += isCapitalized(word)
}
( wLength.result(), wCaps.result() )
val wLength = wordsIndexedSeq.map( _.length )
val wCaps = wordsIndexedSeq.map( isCapitalized )
(wLength, wCaps)
val UTF8 = "UTF-8"
val ISO8859 = "ISO-8859–1"
val REPLACEMENT_CHAR = '\uFFFD'
def bytesToString(bytes: Array[Byte], encoding: String) = {
  val upper = encoding.toUpperCase
  val firstEncoding = if (ISO8859 == upper) ISO8859 else UTF8
  val firstDecoded = new String(bytes, firstEncoding)
  if (!firstDecoded.contains(REPLECEMENT_CHAR)) {
  firstDecoded
import io.Codec
import java.nio.ByteBuffer
val UTF8 = “UTF-8”
val ISO8859 = “ISO-8859–1”
val REPLACEMENT_CHAR = ‘\uFFFD’
def bytesToString(bytes: Array[Byte], encoding: String) = {
val upper = encoding.toUpperCase
val codec = if (ISO8859 == upper) Codec.ISO8859 else Codec.UTF8
<amq:connectionFactory id="connectionFactory" brokerURL="${jms.url}"/>
<bean id="singleConnectionFactory" p:targetConnectionFactory-ref="connectionFactory" p:reconnectOnException="true"/>
<bean id="cachingConnectionFactory" p:targetConnectionFactory-ref="singleConnectionFactory" p:sessionCacheSize="100"/>
<bean id="jmsTemplate" p:connectionFactory-ref="cachingConnectionFactory"/>
<jms:listener-container connection-factory="singleConnectionFactory">
<jms:listener id="QueueHandler" destination="Queue" ref="queueHandler"/>
</jms:listener-container>
final Collection<String> parts = buildQueryParts( dbObj );
if ( parts.isEmpty() ) { return ImmutableList.of(); }
final String query = "SELECT " + columnNames + " FROM " + dbObj.getTableName() + " WHERE " + join( parts, " AND " );
return result = simpleJdbcTemplate.query( query, new BeanPropertyRowMapper<DbObj>( dbObj.getClass() ), new BeanPropertySqlParameterSource( dbObj ) );
private static final Collection<String> buildQueryParts( final LocaDbObj dbObj ) {
 return transform( filter( dbObj.getFieldNames(), new HasValue( dbObj.toMap() ) ), new BuildPart( dbObj ) );
}
private static final class HasValue implements Predicate<String> {
 private final Map<String, Object> map;
 public HasValue( final Map<String, Object> map ) {