Sha256: e592bef05938c9878d96b52de39d679202f9c62549ecea3fa4d4b8d0c402ed70

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

module ActiveScaffold::DataStructures
  class ActionLinks
    include Enumerable

    def initialize
      @set = []
    end

    # adds an ActionLink, creating one from the arguments if need be
    def add(action, options = {})
      link = action.is_a?(ActiveScaffold::DataStructures::ActionLink) ? action : ActiveScaffold::DataStructures::ActionLink.new(action, options)
      # NOTE: this duplicate check should be done by defining the comparison operator for an Action data structure
      existing = @set.find {|a| a.action == link.action and a.controller == link.controller and a.parameters == link.parameters}
      unless existing
        @set << link
        link
      else
        existing
      end
    end
    alias_method :<<, :add

    # finds an ActionLink by matching the action
    def [](val)
      @set.find {|item| item.action == val.to_s}
    end

    def delete(val)
      @set.delete_if{|item| item.action == val.to_s}
    end

    # iterates over the links, possibly by type
    def each(type = nil)
      type = type.to_sym if type
      @set.each {|item|
        next if type and item.type != type
        yield item
      }
    end
    
    def collect_by_type(type = nil)
      links = []
      each(type) {|link| links << link}
      links
    end

    def empty?
      @set.size == 0
    end

    protected

    # called during clone or dup. makes the clone/dup deeper.
    def initialize_copy(from)
      @set = []
      from.instance_variable_get('@set').each { |link| @set << link.clone }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active_scaffold-3.0.2 lib/active_scaffold/data_structures/action_links.rb
active_scaffold-3.0.1 lib/active_scaffold/data_structures/action_links.rb
active_scaffold-3.0.0 lib/active_scaffold/data_structures/action_links.rb