Class: SelfSysteem::AffirmationBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/self_systeem/affirmation_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ AffirmationBuilder

Returns a new instance of AffirmationBuilder.



6
7
8
# File 'lib/self_systeem/affirmation_builder.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/self_systeem/affirmation_builder.rb', line 10

def call(env)

  if env["REQUEST_PATH"].match(/\/assets/)
    status, headers, response = @app.call(env)
  else
    setup_subscriptions

    # middleware api
    status, headers, response = @app.call(env)

    # setup vars for hash to test agains later
    request_path = env["REQUEST_PATH"]
    request_method = env["REQUEST_METHOD"]
    request_parameters = {}
    request_parameters.merge!(env["rack.request.form_hash"]) if env["rack.request.form_hash"].present?
    request_parameters.merge!(env["action_controller.instance"].params.try(:to_hash)) if env["action_controller.instance"].params.present?
    @controller_instance = env["action_controller.instance"]
    controller_class_name = @controller_instance.try(:class).try(:name)
    action = @controller_instance.action_name
    session = env["action_dispatch.request.unsigned_session_cookie"]
    setup_instance_varaibles

    booster = {request_method: request_method,
                      request_path: request_path,
                            action: action,
                request_parameters: request_parameters,
             controller_class_name: controller_class_name,
                            status: status,
                          partials: @_partials,
                           layouts: @_layouts,
                         templates: @_templates,
                             files: @_files,
       relevant_instance_varaibles: @relevant_instance_varaibles.to_s,
         instance_variable_objects: @instance_variable_objects
    }

    # assign paths for dir and file names for session and affirmation files
    path = Rails.root.to_s + "/" + SelfSysteem.test_dir + "/system/support/affirmations/"
    affirmation_filename = ENV["SYSTEEM"] + ".yml"
    session_filename = ENV["SYSTEEM"] + "_session" + ".yml"
    if File.exist?(path + affirmation_filename)
      requirements = YAML.load_file(path + affirmation_filename)[:requirements]
    end

    # writes data to files as yaml
    unless File.exist?(path + affirmation_filename) && YAML.load_file(path + affirmation_filename)[:affirmations].present?
      full_path_dir = (path + affirmation_filename).match(/^(.*\/)?(?:$|(.+?)(?:(\.[^.]*$)|$))/)[1]
      FileUtils.mkdir_p full_path_dir
      File.open(path + affirmation_filename, "w") { |file| file.write({ requirements: [], affirmations: [] }.to_yaml) }
    end

    boosters = YAML.load_file(path + affirmation_filename)
    boosters[:requirements] = requirements if requirements.present?
    boosters[:affirmations] << booster

    File.open(path + affirmation_filename, 'w') do |file|
      file.write(boosters.to_yaml)
    end

    # Overwrites #{filename}_session.yml.  File ends up with the last session of the run.
    File.open(path + session_filename, "w") { |file| file.write(session.to_yaml) }

    # Dump the Database.  Overwrites to get the state of the database after the last run.
    YamlDbSynch.dump(ENV["SYSTEEM"])

    teardown_subscriptions
  end

  # response expected for middleware
  [status, headers, [response.try(:body)].flatten]
end

#setup_instance_varaiblesObject



82
83
84
85
86
# File 'lib/self_systeem/affirmation_builder.rb', line 82

def setup_instance_varaibles
  builder = SelfSysteem::InstanceVariablesBuilder.call(@controller_instance)
  @relevant_instance_varaibles = builder.relevant_instance_varaibles
  @instance_variable_objects = builder.instance_variable_objects
end

#setup_subscriptionsObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/self_systeem/affirmation_builder.rb', line 88

def setup_subscriptions
  # setup the equivalend of event listeners to build instance variables.
  # exactly what ActonController::TestCase does

  @_partials = Hash.new(0)
  @_templates = Hash.new(0)
  @_layouts = Hash.new(0)
  @_files = Hash.new(0)

  ActiveSupport::Notifications.subscribe("render_template.action_view") do |_name, _start, _finish, _id, payload|
    path = payload[:layout]
    if path
      @_layouts[path] += 1
      if path =~ /^layouts\/(.*)/
        @_layouts[$1] += 1
      end
    end
  end

  ActiveSupport::Notifications.subscribe("!render_template.action_view") do |_name, _start, _finish, _id, payload|
    path = payload[:virtual_path]
    next unless path
    partial = path =~ /^.*\/_[^\/]*$/

    if partial
      @_partials[path] += 1
      @_partials[path.split("/").last] += 1
    end

    @_templates[path] += 1
  end

  ActiveSupport::Notifications.subscribe("!render_template.action_view") do |_name, _start, _finish, _id, payload|
    next if payload[:virtual_path] # files don't have virtual path

    path = payload[:identifier]
    if path
      @_files[path] += 1
      @_files[path.split("/").last] += 1
    end
  end
end

#teardown_subscriptionsObject



131
132
133
134
135
# File 'lib/self_systeem/affirmation_builder.rb', line 131

def teardown_subscriptions
  # Removes event listeners
  ActiveSupport::Notifications.unsubscribe("render_template.action_view")
  ActiveSupport::Notifications.unsubscribe("!render_template.action_view")
end