Sha256: 8d4c46368703bedef6a4853184ae8228b54fea1253ec97ec175d060c1700cab6

Contents?: true

Size: 1.75 KB

Versions: 8

Compression:

Stored size: 1.75 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 || {}

    if rel_properties[:clone]
      node_dsl = node_dsl.clone
      rel_properties.delete(:clone)
    end

    @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 || {}

    clone = rel_properties_override.delete :clone

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

    children_dsl
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
geoff-0.2.6 lib/geoff/container.rb
geoff-0.2.5 lib/geoff/container.rb
geoff-0.2.3 lib/geoff/container.rb
geoff-0.2.2 lib/geoff/container.rb
geoff-0.2.1 lib/geoff/container.rb
geoff-0.2.0 lib/geoff/container.rb
geoff-0.1.2 lib/geoff/container.rb
geoff-0.1.1 lib/geoff/container.rb