Skip to content

Instantly share code, notes, and snippets.

View ksol's full-sized avatar
🏠
Working from home, most of the time

Kevin Soltysiak ksol

🏠
Working from home, most of the time
  • Scalingo
  • Strasbourg, France
  • 23:51 (UTC +02:00)
  • X @ksol
View GitHub Profile
@ksol
ksol / memoizer2.rb
Created October 16, 2015 07:50
Example without decorators
class Memoizer
def self.memoize(method_name)
# Getting the method as an object
target_method = instance_method(method_name)
# Overriding it !
define_method(method_name) do |*args, &blk|
# Initializing the cache object
@_memoize_storage ||= {}
@ksol
ksol / memoizer.rb
Created October 14, 2015 21:00
method_added example 2
class Memoizer
def self.memoize
@memoized = true
end
def self.method_added(method_name)
if @memoized
# We're disabling memoization so that when we call "define_method",
# we're not going to loop indefinitely
@memoized = false
@ksol
ksol / thor.rb
Created October 14, 2015 20:29
Thor example
class MyCLI < Thor
desc "hello NAME", "say hello to NAME"
option :from
def hello(name)
puts "from: #{options[:from]}" if options[:from]
puts "Hello #{name}"
end
end
@ksol
ksol / descriptor.rb
Last active October 14, 2015 20:54
method_added example 1
class Descriptor
def self.desc(input)
@desc = input
end
def self.method_added(method_name)
puts "Adding #{method_name.inspect}: description is #{@desc}"
@desc = nil
end
class Test
desc "lala"
def method1
end
desc "yoyo"
def method4
end
desc "stuff"
@ksol
ksol / lala.js
Created September 29, 2015 16:10
// This
export default Component.extend({
value: 1
})
// Is pretty much the same as
var x = 1;
export default Component.extend({
@ksol
ksol / gist:84503db96217655f0476
Last active August 29, 2015 14:22
Ember.js: declaring a computed property/observer with ES7 decorators - http://blog.ksol.fr/ember-js-declaring-computed-property-and-observers/
import computed, { observes } from 'ember-computed-decorators';
export default Ember.Object.extend({
firstName: "Bob",
lastName: "Jones",
@computed('firstName', 'lastName')
fullName: function(firstName, lastName) {
return `${firstName} ${lastName}`;
},
@ksol
ksol / gist:d2ed38d8e40d49b8929f
Last active August 29, 2015 14:22
Ember.js: declaring a computed property with Ember.computed and an observer with Ember.observer - http://blog.ksol.fr/ember-js-declaring-computed-property-and-observers/
export default Ember.Object.extend({
firstName: "Bob",
lastName: "Jones",
fullName: Ember.computed('firstName', 'lastName', function() {
return `${this.get('firstName')} ${this.get('lastName')}`;
}),
nameHasChanged: Ember.observer('fullName', function() {
console.log(`name has changed! Now it is ${this.get('fullName')}`);
})
});
@ksol
ksol / gist:37fedda82c6ab995b68a
Last active August 29, 2015 14:22
Ember.js: declaring a computed property/observer with prototype extension - http://blog.ksol.fr/ember-js-declaring-computed-property-and-observers/
export default Ember.Object.extend({
firstName: "Bob",
lastName: "Jones",
fullName: function() {
return `${this.get('firstName')} ${this.get('lastName')}`;
}.property('firstName', 'lastName'),
nameHasChanged: function() {
console.log(`name has changed! Now it is ${this.get('fullName')}`);
}.observes('fullName')
});
@ksol
ksol / gist:cf199f1f76ad8221a065
Last active August 1, 2016 01:59
Ember.js: sorting with computed properties
export default Ember.Controller.extend({
posts: [], // Filled by real world data by the route
postsSorting: ['name'],
postsSortingDesc: ['name:desc'],
sortedPosts: Ember.computed.sort('posts', 'postsSorting'),
sortedPostsDesc: Ember.computed.sort('posts', 'postsSortingDesc'),
customSortedPost: Ember.computed.sort('posts', function(a, b) {
// return a negative when a < b, 0 when a = b, and a positive when a > b
})