Sha256: bd31e06a783e56ea4b525e4ac19fe68eecc143072627bf6ce064b113840e9498

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

module Osheet::Associations

  def self.included(receiver)
    receiver.send(:extend, ClassMethods)
  end

  module ClassMethods

    # A has many esque association helper
    #  - will provide a collection reader
    #  - will define a 'singular' item method for adding to the collection
    #  - will support adding to the collection both by black and template
    def hm(collection)
      unless collection.to_s =~ /s$/
        raise ArgumentError, "association item names must end in 's'"
      end
      plural = collection.to_s
      singular = plural.to_s.sub(/s$/, '')
      klass = Osheet.const_get(singular.capitalize)

      # define collection reader
      self.send(:define_method, plural, Proc.new do
        if instance_variable_get("@#{plural}").nil?
          instance_variable_set("@#{plural}", [])
        end
        instance_variable_get("@#{plural}")
      end)

      # define collection item writer
      self.send(:define_method, singular) do |*args, &block|
        if instance_variable_get("@#{plural}").nil?
          instance_variable_set("@#{plural}", [])
        end
        instance_variable_get("@#{plural}") << if self.respond_to?(:workbook)
          # on: worksheet, column, row
          # creating: column, row, cell
          worksheet = self.respond_to?(:worksheet) ? self.worksheet : self
          if self.workbook && (template = self.workbook.templates.get(singular, args.first))
            # add by template
            klass.new(self.workbook, worksheet, *args[1..-1], &template)
          else
            # add by block
            klass.new(self.workbook, worksheet, &block)
          end
        else
          # on: workbook
          # creating: worksheet
          if (template = self.templates.get(singular, args.first))
            # add by template
            klass.new(self, *args[1..-1], &template)
          else
            # add by block
            klass.new(self, &block)
          end
        end
      end

    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
osheet-0.4.0 lib/osheet/associations.rb
osheet-0.3.0 lib/osheet/associations.rb
osheet-0.2.0 lib/osheet/associations.rb