Skip to content

Instantly share code, notes, and snippets.

@jvans1
jvans1 / reactex.js
Created November 30, 2016 17:23
Returning JSX
//Normal Approach:
const NameComponent = React.createClass({
propType: { name: React.propTypes.String },
render: function(){
return <h1> {{ this.props.name }} </h1>
}
})
React.createClass({
import Database.Persist.Sql
import qualified Database.Persist.TH as DPTH
--Consider the followering database structure
DPTH.share [DPTH.mkPersist DPTH.sqlSettings, DPTH.mkMigrate "migrateAll"] [DPTH.persistLowerCase|
User
name T.Text
UserPost
user_id UserId
post_id PostId
@jvans1
jvans1 / ScopedTypeVariablesTest.hs
Created May 23, 2015 17:20
ScopedTypeVariables
--#This code blows up
myFunction :: Ord a => [a] -> [a]
myFunction inputArray = reversedArray where
reversedArray :: Ord a => [a]
reversedArray = reverse inputArray
main :: IO ()
main = print $ myFunction [1,2,3]
--#Below code works
var ScoreUpdater = function(scores){
this.scores = scores
}
ScoreUpdater.prototype.avgScore = function(){
var sum = 0;
for (var i = 0; i < this.scores.length; i++) {
sum += parseInt(this.scores[i], 10);
}
var avg = sum / this.scores.length;
}
SqlCondition.new(operator, left_side, right_side).to_query(scope)
input = "(attendees.id = 1 OR id < 2)"
sql_condition1 = SqlCondition.new('=' , "attendees.id", 1).to_query(Ticket)
#leverage SqlAttribute in this class
sql_condition1.to_query #=> "select * from tickets WHERE tickets.id < 1"
sql_condition2 = SqlCondition.new('=' , "attendees.id", 1).to_query(Ticket)
module MyConcern
def shared_method
#do something with method_specific_to_class
end
def method_specific_to_class
raise NotImplementedError.new
end
end
class MyClass
@jvans1
jvans1 / gist:10119641
Last active December 28, 2020 18:38
Angular Auth Service
angular.module('myApp', []).factory( 'AuthService', ["$cookieStore", '$http',
var currentUser;
return {
login: function(email, password, fn) {
currentUser = //Login work with $http
$cookieStore.put("currentUser", currentUser.name)
},
logout: function(){
//Logout work with $http
@jvans1
jvans1 / gist:10119587
Last active August 29, 2015 13:58
errors http interceptor
angular.module('myapp').config([ '$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push('globalErrorInterceptor');
}]);
angular.module('myapp').factory('globalErrorInterceptor', ['$q', '$rootScope', '$cookieStore', '$injector', '$location', 'PathService', 'AlertService',
function ($q, $rootScope, $cookieStore, $injector, $location, Path, Alert) {
$rootScope.showSpinner = false;
$rootScope.http = null;
return {
'response': function (response) {
return response || $q.when(response)
def mode(array)
#Hash.new is good here because the 0 is default for blank values
hash = Hash.new(0)
#Just use an array literal
max = []
array.each {|x| hash[x] += 1 }
hash.select do |key, value|
@jvans1
jvans1 / ticket.rb
Last active August 29, 2015 13:57 — forked from dhh/ticket.rb
class Ticket < ActiveRecord::Base
class TicketConfirmer
attr_reader :ticket, :confirmation_errors
delegate :user, :grouper :to => :ticket
def initialize(ticket)
@ticket = ticket
@confirmation_errors = []
end
def can_confirm?