This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ||= {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Test | |
desc "lala" | |
def method1 | |
end | |
desc "yoyo" | |
def method4 | |
end | |
desc "stuff" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This | |
export default Component.extend({ | |
value: 1 | |
}) | |
// Is pretty much the same as | |
var x = 1; | |
export default Component.extend({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`; | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')}`); | |
}) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}) |