Class: Seedlings

Inherits:
Object
  • Object
show all
Defined in:
lib/seedlings.rb,
lib/seedlings/version.rb

Overview

Seedlings.rb

A Seeding system that handles all the annoying corner-cases for you.

Defined Under Namespace

Modules: ActiveModel

Constant Summary collapse

VERSION =
"0.0.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, options, data) ⇒ Seedlings

Returns a new instance of Seedlings.



19
20
21
22
23
24
# File 'lib/seedlings.rb', line 19

def initialize klass, options, data
  self.klass = klass
  self.options = { :update_existing => true }.merge(options)
  self.data = data
  adapt!
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



7
8
9
# File 'lib/seedlings.rb', line 7

def data
  @data
end

#klassObject

Returns the value of attribute klass.



7
8
9
# File 'lib/seedlings.rb', line 7

def klass
  @klass
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/seedlings.rb', line 7

def options
  @options
end

Class Method Details

.plant(klass, options, *data) ⇒ Object



9
10
11
12
# File 'lib/seedlings.rb', line 9

def self.plant klass, options, *data
  thang = new(klass, options, data)
  thang.plant!
end

.plant_and_return(klass, options, data) ⇒ Object



14
15
16
17
# File 'lib/seedlings.rb', line 14

def self.plant_and_return klass, options, data
  seedling = new(klass, options, [data])
  seedling.plant_and_return data
end

Instance Method Details

#adapt!Object

determine what manner of class we’ve been asked to seed and load the appropriate adapter. Yes, I know it only does one thing now. N.B. I’m open to more efficient suggestions here.



30
31
32
# File 'lib/seedlings.rb', line 30

def adapt!
  extend Seedlings::ActiveModel
end

#plant!Object

Get this planting party started



36
37
38
39
40
# File 'lib/seedlings.rb', line 36

def plant!
  data.each do |the_data|
    find_and_update_model_with the_data unless the_data.nil?
  end
end

#plant_and_return(the_data) ⇒ Object



42
43
44
# File 'lib/seedlings.rb', line 42

def plant_and_return the_data
  find_and_update_model_with the_data unless the_data.nil?
end