Sha256: 4e9b00787f9d14b1c965dc309a5b31254a6cfa495610992f3ef0adcacb0d0951

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

class Container
  attr_reader :node_dsls, :children_dsls

  def initialize dsls = {}
    @node_dsls     = dsls[:node_dsls    ] || {}
    @children_dsls = dsls[:children_dsls] || {}
  end

  def merge other
    @node_dsls    .merge! other.node_dsls
    @children_dsls.merge! other.children_dsls
  end

  def method_missing m, *args, &blk
    if m.to_s.last == "=" # assignment
      node_or_children_dsl = args.first

      # We should only have one dsls hash storing both node dsls and children
      # dsls and methods handle_node_dsl and handle_children_dsl should be
      # moved to corresponding classes.
      if node_or_children_dsl.is_a? NodeDsl
        @node_dsls[m.to_s[0..-2].to_sym] = args.first
      else
        @children_dsls[m.to_s[0..-2].to_sym] = args.first
      end

    elsif @node_dsls.has_key?(m)
      handle_node_dsl m, *args

    elsif @children_dsls.has_key?(m)
      handle_children_dsl m, *args

    else
      raise Geoff::ContainerLabelMissing, "Container has no key #{m}"
    end
  end

  def set_recipient_of_node_dsl children_dsl
    @recipient = children_dsl
  end

  private

  def handle_node_dsl m, *args
    node_dsl = @node_dsls[m]
    rel_properties = args.first
    @recipient.add_node_dsl node_dsl, rel_properties
    node_dsl
  end

  def handle_children_dsl m, *args
    children_dsl = @children_dsls[m]
    rel_properties_override = args.first || {}

    children_dsl.node_dsls.each do |node_dsl, rel_properties|
      @recipient.add_node_dsl node_dsl.clone, rel_properties.merge(rel_properties_override)
    end

    children_dsl
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
geoff-0.0.6 lib/geoff/container.rb
geoff-0.0.5 lib/geoff/container.rb
geoff-0.0.4 lib/geoff/container.rb