Class: Depot::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/depot/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, base) ⇒ Context

Returns a new instance of Context.



5
6
7
8
# File 'lib/depot/context.rb', line 5

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

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



3
4
5
# File 'lib/depot/context.rb', line 3

def base
  @base
end

#klassObject (readonly)

Returns the value of attribute klass.



3
4
5
# File 'lib/depot/context.rb', line 3

def klass
  @klass
end

Instance Method Details

#create(attributes = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/depot/context.rb', line 21

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

#finder(criteria) ⇒ Object



10
11
12
# File 'lib/depot/context.rb', line 10

def finder(criteria)
  @finder = criteria
end

#render_template(template, assigns = {}, view_path = nil) ⇒ Object



14
15
16
17
18
19
# File 'lib/depot/context.rb', line 14

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