Class: TestDummy::Definition
- Inherits:
-
Object
- Object
- TestDummy::Definition
- Defined in:
- lib/test_dummy/definition.rb
Instance Attribute Summary collapse
-
#operations ⇒ Object
readonly
Properties ===========================================================.
Instance Method Summary collapse
- #<<(operation) ⇒ Object
- #[](field) ⇒ Object
-
#apply!(model, create_options, tags) ⇒ Object
Applies the operations defined in this definition to the model supplied, taking into account any options used for creation and only triggering based on the tags specified.
- #apply_after_save!(model, create_options, tags) ⇒ Object
-
#clone ⇒ Object
(also: #dup)
Creates a copy of this Definition.
- #define_operation(model_class, fields, options) ⇒ Object
-
#fields(*tags) ⇒ Object
Returns a list of fields that could be populated with dummy data when the given tags are employed.
- #fields?(*matching_fields) ⇒ Boolean
-
#initialize(operations = nil) ⇒ Definition
constructor
Instance Methods =====================================================.
Constructor Details
#initialize(operations = nil) ⇒ Definition
Instance Methods =====================================================
12 13 14 |
# File 'lib/test_dummy/definition.rb', line 12 def initialize(operations = nil) @operations = operations ? operations.dup : [ ] end |
Instance Attribute Details
#operations ⇒ Object (readonly)
Properties ===========================================================
6 7 8 |
# File 'lib/test_dummy/definition.rb', line 6 def operations @operations end |
Instance Method Details
#<<(operation) ⇒ Object
110 111 112 |
# File 'lib/test_dummy/definition.rb', line 110 def <<(operation) @operations << operation end |
#[](field) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/test_dummy/definition.rb', line 81 def [](field) field = field.to_sym @operations.select do |operation| operation.fields.include?(field) end end |
#apply!(model, create_options, tags) ⇒ Object
Applies the operations defined in this definition to the model supplied, taking into account any options used for creation and only triggering based on the tags specified.
19 20 21 22 23 24 25 26 27 |
# File 'lib/test_dummy/definition.rb', line 19 def apply!(model, , ) @operations.each do |operation| next if (operation.after) operation.apply!(model, , ) end true end |
#apply_after_save!(model, create_options, tags) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/test_dummy/definition.rb', line 29 def apply_after_save!(model, , ) @operations.each do |operation| next unless (operation.after == :save) operation.apply!(model, , ) end true end |
#clone ⇒ Object Also known as: dup
Creates a copy of this Definition.
40 41 42 |
# File 'lib/test_dummy/definition.rb', line 40 def clone self.class.new(@operations) end |
#define_operation(model_class, fields, options) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/test_dummy/definition.rb', line 89 def define_operation(model_class, fields, ) if (fields.any?) fields.each do |field| = .merge( :fields => [ field ].flatten.collect(&:to_sym) ) model_class, foreign_key = TestDummy::Support.reflection_properties(model_class, field) if (model_class and foreign_key) [:model_class] ||= model_class [:foreign_key] ||= foreign_key end @operations << TestDummy::Operation.new() end else @operations << TestDummy::Operation.new() end end |
#fields(*tags) ⇒ Object
Returns a list of fields that could be populated with dummy data when the given tags are employed.
47 48 49 50 51 52 53 54 55 |
# File 'lib/test_dummy/definition.rb', line 47 def fields(*) = .flatten.compact @operations.each_with_object([ ]) do |operation, collection| if (_fields = operation.fields()) collection.concat(_fields) end end.compact.uniq end |
#fields?(*matching_fields) ⇒ Boolean
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/test_dummy/definition.rb', line 57 def fields?(*matching_fields) matching_fields = Hash[ matching_fields.flatten.compact.collect do |field| [ field.to_sym, false ] end ] @operations.each do |operation| operation_fields = operation.fields next unless (operation_fields) operation_fields.each do |field| next unless (field) matching_fields[field] = true end end !matching_fields.find do |field, found| !found end end |