Class: TestDummy::Operation
- Inherits:
-
Object
- Object
- TestDummy::Operation
- Defined in:
- lib/test_dummy/operation.rb
Constant Summary collapse
- VALID_OPTIONS =
Constants ============================================================
[ :block, :fields, :only, :except, :after, :force, :with, :from, :model_class, :foreign_key, :inherit ].freeze
Instance Attribute Summary collapse
-
#after ⇒ Object
readonly
Returns the value of attribute after.
-
#except ⇒ Object
readonly
Returns the value of attribute except.
-
#foreign_key ⇒ Object
readonly
Returns the value of attribute foreign_key.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#only ⇒ Object
readonly
Returns the value of attribute only.
-
#source_keys ⇒ Object
readonly
Returns the value of attribute source_keys.
-
#source_methods ⇒ Object
readonly
Properties ===========================================================.
Instance Method Summary collapse
-
#apply!(model, create_options, tags) ⇒ Object
Called to apply this operation.
- #assignments(model, create_options, tags) ⇒ Object
- #fields(tags = nil) ⇒ Object
-
#initialize(options) ⇒ Operation
constructor
Instance Methods =====================================================.
- #trigger?(tags) ⇒ Boolean
Constructor Details
#initialize(options) ⇒ Operation
Instance Methods =====================================================
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/test_dummy/operation.rb', line 30 def initialize() @blocks = [ ] = .keys - VALID_OPTIONS if (.any?) raise TestDummy::Exception, "Unknown options to #{self.class}: #{.inspect}" end () () () () () () () () () () assign_reflection_block!() assign_default_block!() end |
Instance Attribute Details
#after ⇒ Object (readonly)
Returns the value of attribute after.
24 25 26 |
# File 'lib/test_dummy/operation.rb', line 24 def after @after end |
#except ⇒ Object (readonly)
Returns the value of attribute except.
23 24 25 |
# File 'lib/test_dummy/operation.rb', line 23 def except @except end |
#foreign_key ⇒ Object (readonly)
Returns the value of attribute foreign_key.
26 27 28 |
# File 'lib/test_dummy/operation.rb', line 26 def foreign_key @foreign_key end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
25 26 27 |
# File 'lib/test_dummy/operation.rb', line 25 def model_class @model_class end |
#only ⇒ Object (readonly)
Returns the value of attribute only.
22 23 24 |
# File 'lib/test_dummy/operation.rb', line 22 def only @only end |
#source_keys ⇒ Object (readonly)
Returns the value of attribute source_keys.
21 22 23 |
# File 'lib/test_dummy/operation.rb', line 21 def source_keys @source_keys end |
#source_methods ⇒ Object (readonly)
Properties ===========================================================
20 21 22 |
# File 'lib/test_dummy/operation.rb', line 20 def source_methods @source_methods end |
Instance Method Details
#apply!(model, create_options, tags) ⇒ Object
Called to apply this operation. The model, create_options and tags arguments can be specified to provide more context.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/test_dummy/operation.rb', line 141 def apply!(model, , ) _assignments = assignments(model, , ) return if (_assignments === false) value = nil # The defined blocks are tried in sequence until one of them returns # a non-nil value. @blocks.find do |block| value = case (block.arity) when 0 block.call when 1 block.call(model) when 2 block.call(model, _assignments) when 3 block.call(model, _assignments, ) else block.call(model, _assignments, , ) end !value.nil? end return unless (_assignments) model and !value.nil? and _assignments.each do |field| next unless (field) model.__send__(:"#{field}=", value) end end |
#assignments(model, create_options, tags) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/test_dummy/operation.rb', line 78 def assignments(model, , ) unless (trigger?()) return false end if (@force) return @fields end # ActiveRecord::Base derived models will return an array of strings listing # the fields that have been altered before the model is saved. This # includes any fields that have been populated through the constructor # call, carried through via scope, or have been altered through accessors. if (model and model.respond_to?(:changed)) # If any of the source methods are listed as "changed", then this is # interpreted as a hit, that the fields are already defined or will be. if ((@source_methods & model.changed.collect(&:to_sym)).any?) return false end end # If this operation does not populate any fields, then there's no further # testing required. The processing can continue without assignments. unless (@fields) return end if () # If any of the source keys are listed in the options, then this is # interpreted as a hit, that the fields are already defined or will be. if ((@source_keys & .keys.collect(&:to_sym)).any?) return false end end # Unless there's a model defined, at this point, there's nothing else to # test and exclude defaults or assignments. unless (model) return @fields end # If there are potentially unassigned fields, the only way to proceed is # to narrow it down to the ones that are still `nil`. The fields that are # mapped to columns are considered unpopulated or set with defaults because # of the previous tests. The remainder need to be tested by calling send. columns = model.class.column_names.collect(&:to_sym) applies_to_fields =@fields.select do |field| columns.include?(field) or model.__send__(field).nil? end # If any of these fields need to be assigned, then that's the next step. if (applies_to_fields.any?) return applies_to_fields end # Otherwise there are fields defined, but none of them need to be assigned, # so the operation is not necessary. false end |
#fields(tags = nil) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/test_dummy/operation.rb', line 58 def fields( = nil) if (trigger?()) @fields else [ ] end end |
#trigger?(tags) ⇒ Boolean
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/test_dummy/operation.rb', line 66 def trigger?() if (@only) return false if (! or ( & @only).empty?) end if (@except) return false if ( and ( & @except).any?) end true end |