Class: Transpec::Syntax::Have::HaveRecord

Inherits:
Record
  • Object
show all
Defined in:
lib/transpec/syntax/have/have_record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Record

#==, #hash, #to_s

Constructor Details

#initialize(have) ⇒ HaveRecord

Returns a new instance of HaveRecord.



11
12
13
# File 'lib/transpec/syntax/have/have_record.rb', line 11

def initialize(have)
  @have = have
end

Instance Attribute Details

#haveObject (readonly)

Returns the value of attribute have.



9
10
11
# File 'lib/transpec/syntax/have/have_record.rb', line 9

def have
  @have
end

Instance Method Details

#build_converted_subject(subject) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/transpec/syntax/have/have_record.rb', line 76

def build_converted_subject(subject)
  if have.subject_is_owner_of_collection?
    converted_owner_of_collection(subject)
  else
    subject << ".#{have.default_query_method}"
  end
end

#build_expectation(subject, type) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/transpec/syntax/have/have_record.rb', line 31

def build_expectation(subject, type)
  case type
  when :should
    syntax = "#{subject}.should"
    syntax << '_not' unless positive?
  when :expect
    syntax = "expect(#{subject})."
    syntax << (positive? ? 'to' : 'not_to')
  end

  syntax
end

#converted_owner_of_collection(subject) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/transpec/syntax/have/have_record.rb', line 84

def converted_owner_of_collection(subject)
  subject << '.'

  if have.collection_accessor_is_private?
    subject << "send(#{have.collection_accessor.inspect}"
    subject << ', ...' if have.items_method_has_arguments?
    subject << ')'
  else
    subject << "#{have.collection_accessor}"
    subject << '(...)' if have.items_method_has_arguments?
  end

  subject << ".#{have.query_method}"
end

#converted_subjectObject



68
69
70
71
72
73
74
# File 'lib/transpec/syntax/have/have_record.rb', line 68

def converted_subject
  if @have.subject_is_owner_of_collection?
    build_converted_subject('obj')
  else
    build_converted_subject('collection')
  end
end

#converted_syntaxObject



23
24
25
26
27
28
29
# File 'lib/transpec/syntax/have/have_record.rb', line 23

def converted_syntax
  @converted_syntax ||= begin
    type = have.expectation.current_syntax_type
    syntax = build_expectation(converted_subject, type)
    syntax << " #{source_builder.replacement_matcher_source}"
  end
end

#original_itemsObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/transpec/syntax/have/have_record.rb', line 56

def original_items
  if have.subject_is_owner_of_collection?
    if have.items_method_has_arguments?
      "#{have.collection_accessor}(...)"
    else
      have.collection_accessor
    end
  else
    'items'
  end
end

#original_subjectObject



48
49
50
51
52
53
54
# File 'lib/transpec/syntax/have/have_record.rb', line 48

def original_subject
  if have.subject_is_owner_of_collection?
    'obj'
  else
    'collection'
  end
end

#original_syntaxObject



15
16
17
18
19
20
21
# File 'lib/transpec/syntax/have/have_record.rb', line 15

def original_syntax
  @original_syntax ||= begin
    type = have.expectation.class.snake_case_name.to_sym
    syntax = build_expectation(original_subject, type)
    syntax << " #{have.method_name}(n).#{original_items}"
  end
end

#positive?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/transpec/syntax/have/have_record.rb', line 44

def positive?
  have.expectation.positive?
end

#source_builderObject



99
100
101
# File 'lib/transpec/syntax/have/have_record.rb', line 99

def source_builder
  @source_builder ||= SourceBuilder.new(have, 'n')
end