Class: SampleModels::Sampler

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

Defined Under Namespace

Classes: ConfigureRecipient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ Sampler

Returns a new instance of Sampler.



6
7
8
9
10
11
12
13
14
# File 'lib/sample_models/sampler.rb', line 6

def initialize(model_class)
  @model_class = model_class
  @attribute_sequences = Hash.new { |h,k| h[k] = {} }
  @defaults = HashWithIndifferentAccess.new
  @forced_unique = []
  @forced_email_format = []
  @named_samples = HashWithIndifferentAccess.new
  @polymorphic_default_classes = HashWithIndifferentAccess.new
end

Instance Attribute Details

#before_saveObject

Returns the value of attribute before_save.



3
4
5
# File 'lib/sample_models/sampler.rb', line 3

def before_save
  @before_save
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



4
5
6
# File 'lib/sample_models/sampler.rb', line 4

def defaults
  @defaults
end

#named_samplesObject

Returns the value of attribute named_samples.



3
4
5
# File 'lib/sample_models/sampler.rb', line 3

def named_samples
  @named_samples
end

#polymorphic_default_classesObject

Returns the value of attribute polymorphic_default_classes.



3
4
5
# File 'lib/sample_models/sampler.rb', line 3

def polymorphic_default_classes
  @polymorphic_default_classes
end

Instance Method Details

#attribute_sequence(pass, column) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/sample_models/sampler.rb', line 16

def attribute_sequence(pass, column)
  @attribute_sequences[pass][column.name] ||= begin
    AttributeSequence.build(
      pass, model, column, @forced_unique.include?(column.name),
      @forced_email_format.include?(column.name)
    )
  end
end

#configure(block) ⇒ Object



25
26
27
28
# File 'lib/sample_models/sampler.rb', line 25

def configure(block)
  recipient = ConfigureRecipient.new(self)
  block.call(recipient)
end

#first_pass_attribute_sequence(column) ⇒ Object



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

def first_pass_attribute_sequence(column)
  attribute_sequence(:first, column)
end

#force_email_format(attr) ⇒ Object



34
35
36
# File 'lib/sample_models/sampler.rb', line 34

def force_email_format(attr)
  @forced_email_format << attr.to_s
end

#force_unique(attr) ⇒ Object



38
39
40
# File 'lib/sample_models/sampler.rb', line 38

def force_unique(attr)
  @forced_unique << attr.to_s
end

#modelObject



46
47
48
# File 'lib/sample_models/sampler.rb', line 46

def model
  SampleModels.models[@model_class]
end

#sample(*args) ⇒ Object



50
51
52
# File 'lib/sample_models/sampler.rb', line 50

def sample(*args)
  Creation.new(self, *args).run
end

#second_pass_attribute_sequence(column) ⇒ Object



54
55
56
# File 'lib/sample_models/sampler.rb', line 54

def second_pass_attribute_sequence(column)
  attribute_sequence(:second, column)
end