Module: Woyo::Evaluate::ClassMethods

Defined in:
lib/woyo/world/evaluate.rb

Instance Method Summary collapse

Instance Method Details

#children(*childs) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/woyo/world/evaluate.rb', line 13

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
        #{child}
      end

    ")
  end
end