Module: AeEasy::Core::Mock::FakeExecutor

Includes:
AnswersEngine::Plugin::ContextExposer
Included in:
FakeParser, FakeSeeder
Defined in:
lib/ae_easy/core/mock/fake_executor.rb

Overview

Fake executor that emulates `AnswersEngine` executor.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentString?

Page content.

Returns:

  • (String, nil)


8
9
10
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 8

def content
  @content
end

#failed_contentString?

Failed page content.

Returns:

  • (String, nil)


11
12
13
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 11

def failed_content
  @failed_content
end

Class Method Details

.check_compatibility(source, fragment) ⇒ Hash

Validate executor methods compatibility.

Parameters:

  • source (Array)

    Answersengine executor method collection.

  • fragment (Array)

    Fake executor method collection.

Returns:

  • (Hash)

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 23

def self.check_compatibility source, fragment
  report = AeEasy::Core.analyze_compatibility source, fragment

  unless report[:new].count < 1
    # Warn when outdated
    warn <<-LONGDESC.gsub(/^\s+/,'')
      It seems answersengine has new unmapped methods, try updating
      ae_easy-core gem or contacting gem maintainer to update it.
      New methods: #{report[:new].join ', '}
    LONGDESC
  end

  # Ensure no missing methods
  unless report[:is_compatible]
    message = <<-LONGDESC.gsub(/^\s+/,'')
      There are missing methods! Check your answersengine gem version.
      Missing methods: #{report[:missing].join ', '}
    LONGDESC
    raise AeEasy::Core::Exception::OutdatedError.new(message)
  end

  report
end

Instance Method Details

#clear_draft_outputsObject

Remove all elements on outputs.



67
68
69
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 67

def clear_draft_outputs
  @outputs.clear
end

#clear_draft_pagesObject

Remove all elements on pages.



61
62
63
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 61

def clear_draft_pages
  @pages.clear
end

#dbObject

Fake database to represent what it is saved.



72
73
74
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 72

def db
  @db ||= AeEasy::Core::Mock::FakeDb.new
end

#execute_script(file_path, vars = {}) ⇒ Object

Execute an script file as an executor.

Parameters:

  • file_path (String)

    Script file path to execute.



200
201
202
203
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 200

def execute_script file_path, vars = {}
  eval(File.read(file_path), isolated_binding(vars), file_path)
  flush
end

#find_output(collection = 'default', query = {}) ⇒ Hash?

Find one output by collection and query with pagination.

Parameters:

  • collection (String) (defaults to: 'default')

    ('default') Collection name.

  • query (Hash) (defaults to: {})

    ({}) Filters to query.

Returns:

  • (Hash, nil)


192
193
194
195
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 192

def find_output collection = 'default', query = {}
  result = find_outputs(collection, query, 1, 1)
  result.nil? ? nil : result.first
end

#find_outputs(collection = 'default', query = {}, page = 1, per_page = 30) ⇒ Array

Find outputs by collection and query with pagination.

Parameters:

  • collection (String) (defaults to: 'default')

    ('default') Collection name.

  • query (Hash) (defaults to: {})

    ({}) Filters to query.

  • page (Integer) (defaults to: 1)

    (1) Page number.

  • per_page (Integer) (defaults to: 30)

    (30) Page size.

Returns:

  • (Array)


177
178
179
180
181
182
183
184
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 177

def find_outputs collection = 'default', query = {}, page = 1, per_page = 30
  count = 0
  offset = (page - 1) * per_page
  fixed_query = query.merge(
    '_collection' => collection
  )
  db.query :outputs, fixed_query, offset, per_page
end

#flushObject

Save all drafts into db and clear draft queues.



164
165
166
167
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 164

def flush
  flush_pages
  flush_outputs
end

#flush_outputsObject

Save draft outputs into db and clear draft queue.



158
159
160
161
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 158

def flush_outputs
  save_outputs outputs
  clear_draft_outputs
end

#flush_pagesObject

Save draft pages into db and clear draft queue.



152
153
154
155
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 152

def flush_pages
  save_pages pages
  clear_draft_pages
end

#initialize(opts = {}) ⇒ Object

Initialize object.

Parameters:

  • opts (Hash) (defaults to: {})

    ({}) Configuration options.

Options Hash (opts):

  • :pages (Array) — default: nil

    Array to initialize pages, can be nil for empty.

  • :outputs (Array) — default: nil

    Array to initialize outputs, can be nil for empty.

  • :job_id (Integer) — default: nil

    A number to represent the job_id.

  • :page (Hash) — default: nil

    Current page.

Raises:

  • (ArgumentError)

    When pages or outputs are not Array.



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 85

def initialize opts = {}
  unless opts[:pages].nil? || opts[:pages].is_a?(Array)
    raise ArgumentError.new "Pages must be an array."
  end
  @pages = opts[:pages]
  unless opts[:outputs].nil? || opts[:outputs].is_a?(Array)
    raise ArgumentError.new "Outputs must be an array."
  end
  @outputs = opts[:outputs]
  self.job_id = opts[:job_id]
  self.page = opts[:page]
end

#job_idInteger?

Fake job ID used by executor.

Returns:

  • (Integer, nil)


100
101
102
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 100

def job_id
  db.job_id
end

#job_id=(value) ⇒ Object

Set fake job id value.



105
106
107
108
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 105

def job_id= value
  db.job_id = value
  page['job_id'] = value
end

#outputsArray

Draft outputs, usually get saved after execution.

Returns:

  • (Array)


55
56
57
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 55

def outputs
  @outputs ||= []
end

#pageHash?

Current page used by executor.

Returns:

  • (Hash, nil)


112
113
114
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 112

def page
  @page ||= AeEasy::Core::Mock::FakeDb.build_fake_page job_id: job_id
end

#page=(value) ⇒ Object

Set current page.



117
118
119
120
121
122
123
124
125
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 117

def page= value
  unless value.nil?
    value = AeEasy::Core::Mock::FakeDb.build_page value
    self.job_id = value['job_id'] unless value['job_id'].nil?
    value['job_id'] ||= job_id
    db.page_gid = value['gid'] unless value['gid'].nil?
  end
  @page = value
end

#pagesArray

Draft pages, usually get saved after execution.

Returns:

  • (Array)


49
50
51
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 49

def pages
  @pages ||= []
end

#save_outputs(list) ⇒ Object

Save an output collection on db.

Parameters:

  • list (Array)

    Collection of outputs to save.



147
148
149
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 147

def save_outputs list
  list.each{|output| db.outputs << output}
end

#save_pages(list) ⇒ Object

Save a page collection on db.

Parameters:

  • list (Array)

    Collection of pages to save.



140
141
142
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 140

def save_pages list
  list.each{|page| db.pages << page}
end

#saved_outputsObject

Retrive a list of saved outputs.



133
134
135
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 133

def saved_outputs
  db.outputs
end

#saved_pagesObject

Retrive a list of saved pages. Drafted pages can be included.



128
129
130
# File 'lib/ae_easy/core/mock/fake_executor.rb', line 128

def saved_pages
  db.pages
end