Class: Normalizy::RSpec::Matcher
- Inherits:
-
Object
- Object
- Normalizy::RSpec::Matcher
- Defined in:
- lib/normalizy/rspec/matcher.rb
Instance Method Summary collapse
- #description ⇒ Object
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
- #from(value) ⇒ Object
-
#initialize(attribute) ⇒ Matcher
constructor
A new instance of Matcher.
- #matches?(subject) ⇒ Boolean
- #to(value) ⇒ Object
- #with(value) ⇒ Object
Constructor Details
#initialize(attribute) ⇒ Matcher
Returns a new instance of Matcher.
10 11 12 |
# File 'lib/normalizy/rspec/matcher.rb', line 10 def initialize(attribute) @attribute = attribute end |
Instance Method Details
#description ⇒ Object
14 15 16 17 18 |
# File 'lib/normalizy/rspec/matcher.rb', line 14 def description return "normalizy #{@attribute} with #{with_expected}" if @with.present? "normalizy #{@attribute} from #{from_value} to #{to_value}" end |
#failure_message ⇒ Object
20 21 22 23 24 |
# File 'lib/normalizy/rspec/matcher.rb', line 20 def return "expected: #{with_expected}\n got: #{actual_value}" if @with.present? "expected: #{to_value}\n got: #{actual_value}" end |
#failure_message_when_negated ⇒ Object
26 27 28 29 30 |
# File 'lib/normalizy/rspec/matcher.rb', line 26 def return "expected: value != #{with_expected}\n got: #{actual_value}" if @with.present? "expected: value != #{to_value}\n got: #{actual_value}" end |
#from(value) ⇒ Object
32 33 34 35 36 |
# File 'lib/normalizy/rspec/matcher.rb', line 32 def from(value) @from = value self end |
#matches?(subject) ⇒ Boolean
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/normalizy/rspec/matcher.rb', line 38 def matches?(subject) @subject = subject if @with.present? = @subject.class.normalizy_rules[@attribute] return false if .blank? = default_rules if .map { |option| option[:rules] }.compact.blank? return false if .blank? .each do |option| rules = option[:rules] return true if rules.is_a?(Array) && rules.include?(@with) return true if rules == @with end false else @subject.send :"#{@attribute}=", @from @subject[@attribute] == @to end end |
#to(value) ⇒ Object
65 66 67 68 69 |
# File 'lib/normalizy/rspec/matcher.rb', line 65 def to(value) @to = value self end |
#with(value) ⇒ Object
71 72 73 74 75 |
# File 'lib/normalizy/rspec/matcher.rb', line 71 def with(value) @with = value self end |