class SmartSeeds::Performing

Attributes

attrs[R]
model[R]
object[R]
size[R]
skippable_attributes[R]
worker[R]

Public Class Methods

new(model, attrs, size) click to toggle source
# File lib/smart_seeds/performing.rb, line 5
def initialize(model, attrs, size)
  @attrs = attrs
  @model = model
  @object = model.new
  @size = size
  @skippable_attributes = %w(id)
end

Public Instance Methods

start() click to toggle source
# File lib/smart_seeds/performing.rb, line 13
def start
  size.times do
    add_skippable_attributes
    set_default_values

    # User can send custom values in a hash: SmartSeeds.plant(Entity, {name: 'Aleah'})
    # This method overrides default values to custom('name' in the example above)
    set_custom_values if attrs.any?
    object.save
  end

  return "Done! #{size} #{model.name.downcase.pluralize(size)} was/were planted."
end

Private Instance Methods

add_skippable_attributes() click to toggle source
# File lib/smart_seeds/performing.rb, line 31
def add_skippable_attributes
  # All default attributes which defined in AR object should be skipped by default
  keys_with_default_values = model.column_defaults.select{|key, value| value.present? }.keys
  skippable_attributes.concat(keys_with_default_values)
end
generate_value(column) click to toggle source

There are generator classes for each type of columns If model's type is :integer, method below delegates to SmartSeeds::Generator::Integer

# File lib/smart_seeds/performing.rb, line 55
def generate_value(column)
  klass = "SmartSeeds::Generator::#{column.type.to_s.capitalize}".constantize
  klass.new(column, model).generate_value
end
is_column_must_be_skipped?(column_name) click to toggle source
# File lib/smart_seeds/performing.rb, line 60
def is_column_must_be_skipped?(column_name)
  skippable_attributes.include?(column_name)
end
set_custom_values() click to toggle source
# File lib/smart_seeds/performing.rb, line 45
def set_custom_values
  attrs.each do |attr|
    key = attr.first
    value = attr.last
    object[key.to_s] = value
  end
end
set_default_values() click to toggle source
# File lib/smart_seeds/performing.rb, line 37
def set_default_values
  model.columns.each do |column|
    next if is_column_must_be_skipped?(column.name)

    object[column.name] = generate_value(column)
  end
end