Module: Changey::DSL::ClassMethods

Defined in:
lib/changey/dsl.rb

Instance Method Summary collapse

Instance Method Details

#changey_tracksObject



19
20
21
# File 'lib/changey/dsl.rb', line 19

def changey_tracks
  @changey_tracks ||= []
end

#reset_changey!Object



23
24
25
# File 'lib/changey/dsl.rb', line 23

def reset_changey!
  @changey_tracks = nil
end

#when_attribute(attribute_name, options = {}, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/changey/dsl.rb', line 27

def when_attribute(attribute_name, options = {}, &block)
  if changey_tracks.empty?
    include InstanceMethods
  end

  unless options.key?(:changes_from) || options.key?(:changes_to)
    raise MissingChangeValue, "Attribute #{attribute_name} must specify a 'changes_to' or a 'changes_from' value"
  end

  unless block_given?
    raise MissingBlock, "Attribute #{attribute_name} must specify a block"
  end

  track = Track.new(attribute_name)
  track.direction = options.key?(:changes_from) ? :from : :to
  track.expected_value = options.key?(:changes_from) ? options[:changes_from] : options[:changes_to]

  if options.key?(:to) || options.key?(:from)
    track.expected_other_value = options.key?(:to) ? options[:to] : options[:from]
  end

  BlockDSL.new(track).instance_eval(&block)
  changey_tracks << track
  track
end