Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Created May 26, 2012 16:26
Show Gist options
  • Select an option

  • Save practicingruby/2794514 to your computer and use it in GitHub Desktop.

Select an option

Save practicingruby/2794514 to your computer and use it in GitHub Desktop.
ARTICLE_DATA = { 1 => { :status => "published" },
2 => { :status => "draft" } }
module BrokenRecord
class Table
def initialize(record_class)
@record_class = record_class
end
def find(id)
@record_class.new(ARTICLE_DATA[id])
end
end
class Row
def initialize(params)
@params = params
end
def method_missing(id, *a, &b)
@params[id] || raise
end
end
module Mapper
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def method_missing(id, *a, &b)
Table.new(self).send(id, *a, &b)
end
end
def initialize(params)
@__row__ = Row.new(params)
end
def method_missing(id, *a, &b)
@__row__.send(id, *a, &b)
end
end
end
class Article
include BrokenRecord::Mapper
def published?
status == "published"
end
end
p Article.find(1).published?
p Article.find(2).published?
@loveybot

Copy link
Copy Markdown

Yay Ruby!!

@JEG2

JEG2 commented May 26, 2012

Copy link
Copy Markdown

Does this solve the same problem in a different way?

#
# I like this because you resolve the illegal add when it is defined, instead
# of every single time a method is called.  Plus it allows you to be choosy.
# Maybe it is OK to override some public methods.
# 
# This is just a crude example.  If I wanted to go this way, I would code up a
# private_and_not_overriden construct that added the method to the special list
# and made it private.
#

class Model
  METHODS_YOU_CANNOT_OVERRIDE = %w[cant_touch_this]

  def self.method_added(name)
    if METHODS_YOU_CANNOT_OVERRIDE.include? name.to_s
      fail "You cannot override this method"
    end
  end

  def show_fair_game
    fair_game
  end

  private

  def cant_touch_this
    "I am forever!"
  end

  def fair_game
    "Feel free to replace me."
  end
end

class LegalModel < Model
  private

  def fair_game
    "You have been assimilated."
  end
end

p LegalModel.new.show_fair_game

class IllegalModel < Model
  private

  def cant_touch_this
    "BOOM!"
  end
end

@practicingruby

Copy link
Copy Markdown
Author

@JEG2:

It only addresses a single concern: accidental internal method collisions. It does this by blacklisting rather than whitelisting, which IMO is not a good default. Another thing too: most collisions are coincidental, and this would raise an error where one may not be needed.

Your solution DOES NOT do any of the following things:

  1. Force the child to interact with its parent via its public interface
  2. Separate the child's state (i.e. instance variables) from the parent's
  3. Decouple the child and parent (a child implemented via delegation can easily point itself at a different parent, the same cannot be said for subclassing or module extension)

Those things are just as important as the late binding problem to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment