Class: Fie::State

Inherits:
Object
  • Object
show all
Includes:
Changelog, Track
Defined in:
lib/fie/state.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Changelog

#update_object_using_changelog

Methods included from Track

#track_changes_in_objects, #untrack_changes_in_objects

Constructor Details

#initialize(view_variables: nil, controller_name:, action_name:, uuid:, attributes: nil) ⇒ State

Returns a new instance of State.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fie/state.rb', line 10

def initialize(view_variables: nil, controller_name:, action_name:, uuid:, attributes: nil)
  @controller_name = controller_name
  @action_name = action_name
  @uuid = uuid
  @view_variables = view_variables

  if !view_variables.nil?
    initialize_getters_and_setters(view_variables)
  else
    initialize_getters_and_setters(attributes, variables_from_view: false)
  end

  track_changes_in_objects(self)
end

Class Method Details

._load(string_params) ⇒ Object



84
85
86
87
# File 'lib/fie/state.rb', line 84

def self._load(string_params)
  params = Marshal.load(string_params)
  new(params)
end

Instance Method Details

#_dump(level) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/fie/state.rb', line 73

def _dump(level)
  untrack_changes_in_objects(self)

  Marshal.dump({
    controller_name: @controller_name,
    action_name: @action_name,
    uuid: @uuid,
    attributes: attributes
  })
end

#attributesObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fie/state.rb', line 25

def attributes
  instance_variables_names = instance_variables - [ :@controller_name, :@action_name, :@uuid, :@view_variables, :@fie_tracked ]

  attribute_entries_array = instance_variables_names.map do |instance_variable_name|
    attribute_name = instance_variable_name.to_s.delete('@')
    attribute_value = instance_variable_get(instance_variable_name)

    [attribute_name, attribute_value]
  end

  attribute_entries_array.to_h
end

#inspectObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fie/state.rb', line 38

def inspect
  object_reference = self.to_s
  pretty_print = "\e[0;33m#{ object_reference[0..-2] }\e[0m "

  attributes.each do |name, value|
    if value.is_a? String
      value = "\e[0;31m#{ value.inspect }\e[0m"
    else
      value = "\e[1;34m#{ value.inspect }\e[0m"
    end

    pretty_print += "\n #{ name }: #{ value }"
  end

  pretty_print += '>'

  pretty_print
end

#permeateObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fie/state.rb', line 57

def permeate
  redis.set Fie::Commander.commander_name(@uuid), Marshal.dump(self)

  rendered_view = ApplicationController.render \
    "#{ @controller_name }/#{ @action_name }",
    assigns: attributes.merge(fie_controller_name: @controller_name, fie_action_name: @action_name),
    layout: 'fie'

  ActionCable.server.broadcast \
    Fie::Commander.commander_name(@uuid),
    command: 'refresh_view',
    parameters: {
      html: rendered_view
    }
end