Class: Treant

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

Instance Method Summary collapse

Constructor Details

#initialize(model, *unique_keys) ⇒ Treant

Returns a new instance of Treant.



4
5
6
7
# File 'lib/treant.rb', line 4

def initialize(model, *unique_keys)
  @model = model
  @unique_keys = unique_keys
end

Instance Method Details

#seed {|attrs| ... } ⇒ Object

Yields:

  • (attrs)


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/treant.rb', line 9

def seed
  attrs = OpenStruct.new

  yield(attrs)

  conditions = @unique_keys.each_with_object({}) { |k, h| h[k] = attrs[k] }

  record = @model.find_or_initialize_by(conditions)

  record.update!(attrs.to_h)

  record
end