Sha256: 55481da0bbaa5f3deca08436dcd350c95fd635ad541d22131d83fbf1052cb3ef
Contents?: true
Size: 1.16 KB
Versions: 3
Compression:
Stored size: 1.16 KB
Contents
require "declarative/deep_dup" module Declarative class Heritage < Array # Record inheritable assignments for replay in an inheriting class. def record(method, *args, &block) self << {method: method, args: DeepDup.(args), block: block} # DISCUSS: options.dup. end # Replay the recorded assignments on inheritor. # Accepts a block that will allow processing the arguments for every recorded statement. def call(inheritor, &block) each { |cfg| call!(inheritor, cfg, &block) } end module DSL def heritage @heritage ||= Heritage.new end end # To be extended into classes using Heritage. Inherits the heritage. module Inherited def inherited(subclass) super heritage.(subclass) end end # To be included into modules using Heritage. When included, inherits the heritage. module Included def included(mod) super heritage.(mod) end end private def call!(inheritor, cfg) yield cfg if block_given? # allow messing around with recorded arguments. inheritor.send(cfg[:method], *cfg[:args], &cfg[:block]) end end end
Version data entries
3 entries across 3 versions & 3 rubygems