Class: Shoulda::Matchers::ActiveModel::AllowValueMatcher::AttributeSetter

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ AttributeSetter

Returns a new instance of AttributeSetter.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 18

def initialize(args)
  @args = args
  @matcher_name = args.fetch(:matcher_name)
  @object = args.fetch(:object)
  @attribute_name = args.fetch(:attribute_name)
  @value_written = args.fetch(:value)
  @ignore_interference_by_writer = args.fetch(
    :ignore_interference_by_writer,
    Qualifiers::IgnoreInterferenceByWriter.new,
  )
  @after_set_callback = args.fetch(:after_set_callback, -> { })

  @result_of_checking = nil
  @result_of_setting = nil
end

Instance Attribute Details

#attribute_nameObject (readonly)

Returns the value of attribute attribute_name.



11
12
13
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 11

def attribute_name
  @attribute_name
end

#result_of_checkingObject (readonly)

Returns the value of attribute result_of_checking.



11
12
13
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 11

def result_of_checking
  @result_of_checking
end

#result_of_settingObject (readonly)

Returns the value of attribute result_of_setting.



11
12
13
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 11

def result_of_setting
  @result_of_setting
end

Class Method Details

.set(args) ⇒ Object



7
8
9
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 7

def self.set(args)
  new(args).set
end

Instance Method Details

#attribute_changed_value?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 139

def attribute_changed_value?
  value_written != value_read
end

#checkObject



55
56
57
58
59
60
61
62
63
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 55

def check
  if attribute_exists?
    @result_of_checking = successful_check
    true
  else
    @result_of_checking = attribute_does_not_exist_error
    false
  end
end

#checked?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 115

def checked?
  !result_of_checking.nil?
end

#descriptionObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 34

def description
  description = ":#{attribute_name} to "
  description << Shoulda::Matchers::Util.inspect_value(value_written)

  if attribute_changed_value?
    description << ' -- which was read back as '
    description << Shoulda::Matchers::Util.inspect_value(value_read)
    description << ' --'
  end

  description
end

#failure_messageObject



97
98
99
100
101
102
103
104
105
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 97

def failure_message
  if successful?
    raise "We're not supposed to be here!"
  elsif result_of_setting
    result_of_setting.message
  else
    result_of_checking.message
  end
end

#runObject



47
48
49
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 47

def run
  check && set
end

#run!Object



51
52
53
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 51

def run!
  check && set!
end

#setObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 82

def set
  object.public_send("#{attribute_name}=", value_written)
  after_set_callback.call

  @result_of_checking = successful_check

  if raise_attribute_changed_value_error?
    @result_of_setting = attribute_changed_value_error
    false
  else
    @result_of_setting = successful_setting
    true
  end
end

#set!Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 65

def set!
  if attribute_exists?
    set

    unless result_of_setting.successful?
      raise result_of_setting
    end

    @result_of_checking = successful_check
    @result_of_setting = successful_setting

    true
  else
    attribute_does_not_exist!
  end
end

#set?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 127

def set?
  !result_of_setting.nil?
end

#successful?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 107

def successful?
  successfully_checked? && successfully_set?
end

#successfully_checked?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 119

def successfully_checked?
  checked? && result_of_checking.successful?
end

#successfully_set?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 131

def successfully_set?
  set? && result_of_setting.successful?
end

#unsuccessful?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 111

def unsuccessful?
  !successful?
end

#unsuccessfully_checked?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 123

def unsuccessfully_checked?
  !successfully_checked?
end

#value_readObject



135
136
137
# File 'lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb', line 135

def value_read
  @_value_read ||= object.public_send(attribute_name)
end