class Fabrication::Schematic::Attribute

Attributes

klass[RW]
name[RW]
params[W]
value[RW]

Public Class Methods

new(klass, name, value, params = {}, &block) click to toggle source
# File lib/fabrication/schematic/attribute.rb, line 7
def initialize(klass, name, value, params = {}, &block)
  self.klass = klass
  self.name = name
  self.params = params
  self.value = value.nil? ? block : value
end

Public Instance Methods

params() click to toggle source
# File lib/fabrication/schematic/attribute.rb, line 14
def params
  @params ||= {}
end
processed_value(processed_attributes) click to toggle source
# File lib/fabrication/schematic/attribute.rb, line 26
def processed_value(processed_attributes)
  if process_count
    (1..process_count).map { |i| execute(processed_attributes, i, &value) }
  elsif value_proc?
    execute(processed_attributes, &value)
  else
    value
  end
end
transient!() click to toggle source
# File lib/fabrication/schematic/attribute.rb, line 18
def transient!
  params[:transient] = true
end
transient?() click to toggle source
# File lib/fabrication/schematic/attribute.rb, line 22
def transient?
  params[:transient]
end
value_proc?() click to toggle source
# File lib/fabrication/schematic/attribute.rb, line 40
def value_proc?
  value.is_a?(Proc)
end
value_static?() click to toggle source
# File lib/fabrication/schematic/attribute.rb, line 36
def value_static?
  !value_proc?
end

Private Instance Methods

count() click to toggle source
# File lib/fabrication/schematic/attribute.rb, line 54
def count
  params[:count]
end
execute(...) click to toggle source
# File lib/fabrication/schematic/attribute.rb, line 46
def execute(...)
  Fabrication::Schematic::Runner.new(klass).instance_exec(...)
end
process_count() click to toggle source
# File lib/fabrication/schematic/attribute.rb, line 50
def process_count
  count || rand || rand_range
end
rand() click to toggle source
# File lib/fabrication/schematic/attribute.rb, line 58
def rand
  return unless params[:rand]

  range = params[:rand]
  range = 1..range unless range.is_a? Range

  Kernel.rand(range)
end
rand_range() click to toggle source
# File lib/fabrication/schematic/attribute.rb, line 67
def rand_range
  Kernel.rand((params[:start_range]..params[:end_range])) if params[:start_range] && params[:end_range]
end