Class: FakutoriSan::Fakutori

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Fakutori

Returns a new instance of Fakutori.



48
49
50
# File 'lib/fakutori_san/fakutori.rb', line 48

def initialize(model)
  @model = model
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



46
47
48
# File 'lib/fakutori_san/fakutori.rb', line 46

def model
  @model
end

Class Method Details

.for_model(model) ⇒ Object



41
42
43
# File 'lib/fakutori_san/fakutori.rb', line 41

def for_model(model)
  FakutoriSan.factories[model] = new(model)
end

.inherited(factory_klass) ⇒ Object



35
36
37
38
39
# File 'lib/fakutori_san/fakutori.rb', line 35

def inherited(factory_klass)
  model_klass = Object.const_get(factory_klass.name.gsub(/^FakutoriSan::|Fakutori$/, ''))
  factory_klass.for_model model_klass
rescue NameError
end

Instance Method Details

#associate(record_or_collection, to_model, options = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fakutori_san/fakutori.rb', line 100

def associate(record_or_collection, to_model, options = nil)
  if builder = association_builder_for(to_model)
    [*record_or_collection].each do |record|
      send(*[builder, record, to_model, options].compact)
    end
  else
    raise NoMethodError, "#{self.class.name} has no association builder method for model `#{to_model.inspect}'."
  end
  
  record_or_collection
end

#build(*times_and_or_type_and_or_attributes) ⇒ Object



73
74
75
# File 'lib/fakutori_san/fakutori.rb', line 73

def build(*times_and_or_type_and_or_attributes)
  multiple_times :build, times_and_or_type_and_or_attributes
end

#build_one(*type_and_or_attributes) ⇒ Object



69
70
71
# File 'lib/fakutori_san/fakutori.rb', line 69

def build_one(*type_and_or_attributes)
  make_chainable(@model.new(plan_one(*type_and_or_attributes)))
end

#create(*times_and_or_type_and_or_attributes) ⇒ Object



91
92
93
# File 'lib/fakutori_san/fakutori.rb', line 91

def create(*times_and_or_type_and_or_attributes)
  multiple_times :create, times_and_or_type_and_or_attributes
end

#create!(*times_and_or_type_and_or_attributes) ⇒ Object



95
96
97
98
# File 'lib/fakutori_san/fakutori.rb', line 95

def create!(*times_and_or_type_and_or_attributes)
  times_and_or_type_and_or_attributes << true
  create(*times_and_or_type_and_or_attributes)
end

#create_one(*type_and_or_attributes_and_or_validate) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/fakutori_san/fakutori.rb', line 77

def create_one(*type_and_or_attributes_and_or_validate)
  args = type_and_or_attributes_and_or_validate
  
  validate = args.pop if [true, false].include?(args.last)
  instance = build_one(*args)
  validate ? instance.save! : instance.save(false)
  instance
end

#create_one!(*type_and_or_attributes) ⇒ Object



86
87
88
89
# File 'lib/fakutori_san/fakutori.rb', line 86

def create_one!(*type_and_or_attributes)
  type_and_or_attributes << true
  create_one(*type_and_or_attributes)
end

#plan(*times_and_or_type_and_or_attributes) ⇒ Object



65
66
67
# File 'lib/fakutori_san/fakutori.rb', line 65

def plan(*times_and_or_type_and_or_attributes)
  multiple_times :plan, times_and_or_type_and_or_attributes
end

#plan_one(*type_and_or_attributes) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fakutori_san/fakutori.rb', line 52

def plan_one(*type_and_or_attributes)
  attributes = type_and_or_attributes.extract_options!
  type = type_and_or_attributes.pop || :valid
  m = "#{type}_attrs"
  
  if respond_to?(m)
    plan = method(m).arity.zero? ? send(m) : send(m, attributes)
    plan.merge(attributes)
  else
    raise NoMethodError, "#{self.class.name} has no attributes method for type `#{type}'"
  end
end

#scene(name, record_or_collection, options = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/fakutori_san/fakutori.rb', line 112

def scene(name, record_or_collection, options = {})
  method = "#{name}_scene"
  unless respond_to?(method)
    raise NoMethodError, "#{self.class.name} has no scene method for scene `#{name.inspect}'"
  end
  
  if record_or_collection.is_a?(Array)
    record_or_collection.each_with_index do |record, index|
      options[:index] = index
      send(method, record, options)
    end
  else
    send(method, record_or_collection, options)
  end
  
  record_or_collection
end