Skip to main content

Posts

Showing posts from June, 2015

Source Location, i.e. Where the heck is this method defined?

Just a short one for now.

Most of the time, if you are working on a well defined project, it's pretty easy to find where a function is if you want to change/tweak it. You can look in the class where it is defined and/or see if there are any mixins or decorators.

Sometimes, it can be downright impossible to tell.

Enter source_location

Fire up your rails console and put in the following (for a class method :search on User)

User.method(:search).source_location

if you have an instance method

User.new.method(:search).source_location

This should give you something like the following

["/Users/yourdrive/Projects/prokect/vendor/cache/thinking-sphinx-9b71ce0f7d9d/lib/thinking_sphinx/search_methods.rb",
367]
This should at least give you some idea of what is going on...

Mixins in ES6

I had been looking for a good way to do mixins in ES6.

Looks like I finally found one.

http://raganwald.com/2015/06/10/mixins.html

Essentially,

create the mixin as a const

Use Object.assign to add the properties to the Class

constBookCollector={addToCollection(name){this.collection().push(name);returnthis;},collection(){returnthis._collected_books||(this._collected_books=[]);