Class: Transpec::Syntax::Have::RecordBuilder

Inherits:
RecordBuilder show all
Defined in:
lib/transpec/syntax/have/record_builder.rb

Direct Known Subclasses

OnelinerShould::HaveRecordBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from RecordBuilder

build, #build

Constructor Details

#initialize(have) ⇒ RecordBuilder

Returns a new instance of RecordBuilder.



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

def initialize(have)
  @have = have
end

Instance Attribute Details

#have ⇒ Object (readonly)

Returns the value of attribute have.



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

def have
  @have
end

Instance Method Details

#annotation ⇒ Object



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

def annotation
  return @annotation if instance_variable_defined?(:@annotation)

  @annotation = if have.accurate_conversion?
                  nil
                else
                  AccuracyAnnotation.new(have.matcher_range)
                end
end

#build_expectation(subject, type) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/transpec/syntax/have/record_builder.rb', line 37

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

#build_new_subject(subject) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/transpec/syntax/have/record_builder.rb', line 82

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

#new_owner_of_collection(subject) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/transpec/syntax/have/record_builder.rb', line 90

def new_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

#new_subject ⇒ Object



74
75
76
77
78
79
80
# File 'lib/transpec/syntax/have/record_builder.rb', line 74

def new_subject
  if have.subject_is_owner_of_collection?
    build_new_subject('obj')
  else
    build_new_subject('collection')
  end
end

#new_syntax ⇒ Object



31
32
33
34
35
# File 'lib/transpec/syntax/have/record_builder.rb', line 31

def new_syntax
  type = have.expectation.current_syntax_type
  syntax = build_expectation(new_subject, type)
  syntax << " #{source_builder.replacement_matcher_source}"
end

#old_items ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/transpec/syntax/have/record_builder.rb', line 62

def old_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

#old_subject ⇒ Object



54
55
56
57
58
59
60
# File 'lib/transpec/syntax/have/record_builder.rb', line 54

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

#old_syntax ⇒ Object



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

def old_syntax
  type = have.expectation.class.snake_case_name.to_sym
  syntax = build_expectation(old_subject, type)
  syntax << " #{have.method_name}(n).#{old_items}"
end

#positive? ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/transpec/syntax/have/record_builder.rb', line 50

def positive?
  have.expectation.positive?
end

#source_builder ⇒ Object



105
106
107
# File 'lib/transpec/syntax/have/record_builder.rb', line 105

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