Sha256: 4b26a4f120ce5f4883e4443a5aef6bcf2ae8a6981a3cfca0a489686003a6c565

Contents?: true

Size: 1.42 KB

Versions: 29

Compression:

Stored size: 1.42 KB

Contents

module ActiveScaffold::DataStructures
  class Set
    include Enumerable
    include ActiveScaffold::Configurable

    def initialize(*args)
      set_values(*args)
    end

    def set_values(*args)
      @set = []
      add *args
    end

    # the way to add items to the set.
    def add(*args)
      args.flatten! # allow [] as a param
      args.each do |arg|
        arg = arg.to_sym if arg.is_a? String
        @set << arg unless @set.include? arg # avoid duplicates
      end
    end
    alias_method :<<, :add

    # the way to remove items from the set.
    def exclude(*args)
      args.flatten! # allow [] as a param
      args.collect!(&:to_sym) # symbolize the args
      # check respond_to? :to_sym, ActionColumns doesn't respond to to_sym
      @set.reject! { |c| c.respond_to?(:to_sym) && args.include?(c.to_sym) } # reject all items specified
    end
    alias_method :remove, :exclude

    # returns an array of items with the provided names
    def find_by_names(*names)
      @set.find_all { |item| names.include? item }
    end

    # returns the item of the given name.
    def find_by_name(name)
      # this works because of `def item.=='
      item = @set.find { |c| c == name }
      item
    end
    alias_method :[], :find_by_name

    def each
      @set.each { |i| yield i }
    end

    # returns the number of items in the set
    def length
      @set.length
    end

    def empty?
      @set.empty?
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
active_scaffold-3.4.43 lib/active_scaffold/data_structures/set.rb
active_scaffold-3.4.42 lib/active_scaffold/data_structures/set.rb
active_scaffold-3.4.41.1 lib/active_scaffold/data_structures/set.rb
active_scaffold-3.4.41 lib/active_scaffold/data_structures/set.rb
active_scaffold-3.4.40 lib/active_scaffold/data_structures/set.rb
active_scaffold-3.4.39 lib/active_scaffold/data_structures/set.rb
active_scaffold-3.4.38 lib/active_scaffold/data_structures/set.rb
active_scaffold-3.4.37 lib/active_scaffold/data_structures/set.rb
active_scaffold-3.4.36 lib/active_scaffold/data_structures/set.rb
active_scaffold-3.4.35 lib/active_scaffold/data_structures/set.rb
active_scaffold-3.4.34 lib/active_scaffold/data_structures/set.rb
active_scaffold-3.4.33 lib/active_scaffold/data_structures/set.rb
active_scaffold-3.4.32 lib/active_scaffold/data_structures/set.rb
active_scaffold-3.4.31 lib/active_scaffold/data_structures/set.rb
active_scaffold-3.4.30 lib/active_scaffold/data_structures/set.rb
active_scaffold-3.4.29 lib/active_scaffold/data_structures/set.rb
active_scaffold-3.4.28 lib/active_scaffold/data_structures/set.rb
active_scaffold-3.4.27 lib/active_scaffold/data_structures/set.rb
active_scaffold-3.4.26 lib/active_scaffold/data_structures/set.rb
active_scaffold-3.4.25 lib/active_scaffold/data_structures/set.rb