Sha256: b2c20b0a17abbc762d28db363dab52e6f1d59d0e433f763bdd1b819ca57d5512

Contents?: true

Size: 1003 Bytes

Versions: 1

Compression:

Stored size: 1003 Bytes

Contents

module Depot
  class Context
    attr_reader :klass, :base

    def initialize(klass, base)
      @klass = klass
      @base = base
    end

    def finder(criteria)
      @finder = criteria
    end

    def render_template(template, assigns = {}, view_path = nil)
      view_path ||= Rails.root.join("db", "seeds")
      view = Depot::Template.new(view_path)
      view.assign(assigns)
      view.render :template => template.to_s
    end

    def create(attributes = {})
      # remove method reference symbol
      key = attributes[:as]
      attributes.except! :as

      # alternate syntax to define attributes
      if block_given?
        klass_instance = @klass.new
        yield klass_instance
        attributes = klass_instance.attributes
      end

      # finder criteria
      criteria = @finder || attributes.keys.first

      # okey, let's do it
      entry = @klass.send("find_or_create_by_#{criteria}", attributes)
      @base.entries[key] = entry if key
      entry
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
depot-0.2.0 lib/depot/context.rb