-
-
Save anonymous/cef3c9bf2f756779aca2 to your computer and use it in GitHub Desktop.
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 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