Wednesday, June 17, 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...

Sunday, June 14, 2015

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

const BookCollector = {
  addToCollection (name) {
    this.collection().push(name);
    return this;
  },
  collection () {
    return this._collected_books || (this._collected_books = []);
  }
};

class Person {
  constructor (first, last) {
    this.rename(first, last);
  }
  fullName () {
    return this.firstName + " " + this.lastName;
  }
  rename (first, last) {
    this.firstName = first;
    this.lastName = last;
    return this;
  }
};

Object.assign(Person.prototype, BookCollector);
(I am copying and pasting this for personal reference in case the original site link goes down).