Skip to content

Instantly share code, notes, and snippets.

/*gist* Secret

Created March 16, 2009 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/cef3c9bf2f756779aca2 to your computer and use it in GitHub Desktop.
Save anonymous/cef3c9bf2f756779aca2 to your computer and use it in GitHub Desktop.
class Trait < Module
def self.new(&blk)
m = super{ } # note: passing empty block overrides &blk
class << m
include ClassMethods
end
m.deferred_block = blk
m
end
module ClassMethods
attr_accessor :deferred_block
def included(mod)
mod.class_eval(&@deferred_block)
end
end
end
module Traits
Comment = Trait.new do
#
# Behaviors
#
include ::DataMapper::Resource
#
# Properties
#
include ::Traits::UserGeneratedContent
#
# Associations
#
belong_to parent_association_name
#
# Validations
#
validates_present parent_association_name
#
# API
#
def recent?
# ...
end
def by_author?
# ...
end
def reply?
# ...
end
end
end
class MessageComment
def self.parent_association_name
:message
end
include ::Traits::Comment
end
class ArticleComment
def self.parent_association_name
:article
end
include ::Traits::Comment
end
class TicketComment
def self.parent_association_name
:ticket
end
include ::Traits::Comment
#
# API
#
def by_staff_member?
# ...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment