Sha256: 70525504c11830a28d86de36da381ad0107224cb4d22d690fca7a3b9e20bbca2

Contents?: true

Size: 1.89 KB

Versions: 5

Compression:

Stored size: 1.89 KB

Contents

module Woyo

module DSL
  
  def evaluate &block
    (block.arity < 1 ? (instance_eval &block) : block.call(self)) if block_given?
    self
  end

  module ClassMethods

    def children *childs
      @children ||= []
      return @children if childs.empty?
      childs.each { |child| @children << child unless @children.include? child }
      @children.each do |child|
        class_eval("

          def #{child}s
            ( @children ||= {} )[:#{child}] ||= ( @#{child}s ||= {} )
          end

          def #{child} child_or_id, &block
            #{child} = child_or_id.kind_of?( #{child.capitalize} ) ? child_or_id : nil
            id = #{child} ? #{child}.id : child_or_id
            known = self.#{child}s[id] ? true : false
            case
            when  #{child} &&  known &&  block_given? then #{child}.evaluate &block
            when  #{child} &&  known && !block_given? then #{child}
            when  #{child} && !known &&  block_given? then self.#{child}s[id] = #{child}.evaluate &block
            when  #{child} && !known && !block_given? then self.#{child}s[id] = #{child}
            when !#{child} &&  known &&  block_given? then #{child} = self.#{child}s[id].evaluate &block
            when !#{child} &&  known && !block_given? then #{child} = self.#{child}s[id]
            when !#{child} && !known &&  block_given? then #{child} = self.#{child}s[id] = #{child.capitalize}.new id, context: self, &block
            when !#{child} && !known && !block_given? then #{child} = self.#{child}s[id] = #{child.capitalize}.new id, context: self
            end
            # maybe: god-like lists of everything at world level... would need unique ids... self.world.#{child}s[id] = #{child} if !known && self.world
            #{child}
          end

        ")
      end
    end

  end 

  def self.included(base)
    base.extend(ClassMethods)
  end
  
  def children
    @children ||= {}
  end

end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
woyo-world-0.0.6 lib/woyo/world/dsl.rb
woyo-world-0.0.5 lib/woyo/world/dsl.rb
woyo-world-0.0.4 lib/woyo/world/dsl.rb
woyo-world-0.0.3 lib/woyo/world/dsl.rb
woyo-world-0.0.2 lib/woyo/world/dsl.rb