Sha256: c3ae378e4e0aa553da62dee9062304584fd2b3802dee2516c68fb08b1661816d

Contents?: true

Size: 1.33 KB

Versions: 4

Compression:

Stored size: 1.33 KB

Contents

require 'osheet/template'

module Osheet
  class TemplateSet < ::Hash

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

    def initialize
      super
    end

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

    # return the template set for the named element
    def get(element, name)
      lookup(key(element.to_s, name.to_s))
    end

    private

    def lookup(key)
      self[key.first][key.last] if self[key.first]
    end

    # push the template onto the key
    def push(key, template)
      self[key.first][key.last] = template
    end

    # verify the template, init the key set, and return the key string
    #  otherwise ArgumentError it up
    def verify(template)
      unless template.kind_of?(Template)
        raise ArgumentError, 'you can only push Osheet::Template objs to the template set'
      end
      key = template_key(template)
      self[key.first] ||= {}
      self[key.first][key.last] ||= nil
      key
    end

    def template_key(template)
      key(template.element, template.name)
    end

    def key(element, name)
      [element, name]
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
osheet-0.5.0 lib/osheet/template_set.rb
osheet-0.4.0 lib/osheet/template_set.rb
osheet-0.3.0 lib/osheet/template_set.rb
osheet-0.2.0 lib/osheet/template_set.rb