Sha256: af5c67f69c0e5321c3aed2ce6eeef4a9176dc4ff18550d0fa0c05be939effdde

Contents?: true

Size: 1.12 KB

Versions: 7

Compression:

Stored size: 1.12 KB

Contents

require 'osheet/partial'

module Osheet
  class PartialSet < ::Hash

    # this class is a Hash that behaves kinda like a set.  I want to
    #  push partials into the set using the '<<' operator, only allow
    #  Osheet::Partial objs to be pushed, and then be able to reference
    #  a particular partial using a key

    def initialize
      super
    end

    def <<(partial)
      if (key = verify(partial))
        push(key, partial)
      end
    end

    # return the named partial
    def get(name)
      lookup(key(name.to_s))
    end

    private

    def lookup(key)
      self[key]
    end

    # push the partial onto the key
    def push(key, partial)
      self[key] = partial
    end

    # verify the partial, init and return the key
    #  otherwise ArgumentError it up
    def verify(partial)
      unless partial.kind_of?(Partial)
        raise ArgumentError, 'you can only push Osheet::Partial objs to the partial set'
      end
      pkey = partial_key(partial)
      self[pkey] ||= nil
      pkey
    end

    def partial_key(partial)
      key(partial.name)
    end

    def key(name)
      name.to_s
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
osheet-0.10.0 lib/osheet/partial_set.rb
osheet-0.9.2 lib/osheet/partial_set.rb
osheet-0.9.1 lib/osheet/partial_set.rb
osheet-0.9.0 lib/osheet/partial_set.rb
osheet-0.8.0 lib/osheet/partial_set.rb
osheet-0.7.0 lib/osheet/partial_set.rb
osheet-0.6.0 lib/osheet/partial_set.rb