Class: Peeky::Predicates::AttrReaderPredicate

Inherits:
Object
  • Object
show all
Defined in:
lib/peeky/predicates/attr_reader_predicate.rb

Overview

Attr Reader Predicate will match true if the method info could be considered a valid attr_reader

Instance Method Summary collapse

Instance Method Details

#match(instance, method_info) ⇒ Object

Match will return true if the method_info seems to be an :attr_reader

Parameters:

  • instance (Object)

    instance the object that has this method (required)

  • method_info (String)

    method info (required)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/peeky/predicates/attr_reader_predicate.rb', line 14

def match(instance, method_info)
  return false unless prerequisites(instance, method_info)

  method_name = method_info.name

  # Refactor: Fragile
  # Really need to handle exceptions and types better
  # old_value = instance.send(method_name)

  # This code works by
  # 1. Set @name_of_method variable to random value
  # 2. Call method name and see if it returns that value
  # 3. Return match<true> if the values are equal
  new_value = SecureRandom.alphanumeric(20)
  code = <<-RUBY
    @#{method_name} = '#{new_value}' # eg. @variable = 'a3bj7a3bj7a3bj7a3bj7'
  RUBY

  cloned = instance.clone

  cloned.instance_eval(code)
  current_value = cloned.send(method_name)
  current_value == new_value
end