fie 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b405e7119223dc975863c197b4e7874ac2d69feb
4
+ data.tar.gz: c35975258e88f2bc02dc29b51a0bad2e8f4d05b6
5
+ SHA512:
6
+ metadata.gz: 193d4cc8988d4ab373b1bd7af6055104ee11d253452c3cec808c83843de133cb6fe267edc15fa4c27aa87a1b22f163066258b0da14cc5b220db6e18c6569acd8
7
+ data.tar.gz: 84c969b2831a1ede1f6207ab2f3dd79df7e8a505bf2c4b7dc4c0e32e432d5439b1656b87ea6da90c8d445426754103954b7b974f8dc3043f1904a264e1766578
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Eran Peer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ <!-- # Fie
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fie`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ [![Join the chat at https://gitter.im/rails-fie/Lobby](https://badges.gitter.im/rails-fie/Lobby.svg)](https://gitter.im/rails-fie/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'fie'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install fie
24
+
25
+ ## Usage
26
+
27
+ TODO: Write usage instructions here
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
+
33
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fie. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
38
+
39
+ ## License
40
+
41
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
42
+
43
+ ## Code of Conduct
44
+
45
+ Everyone interacting in the Fie project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/fie/blob/master/CODE_OF_CONDUCT.md).
46
+
47
+ [![Beerpay](https://beerpay.io/raen79/fie/badge.svg?style=beer-square)](https://beerpay.io/raen79/fie) [![Beerpay](https://beerpay.io/raen79/fie/make-wish.svg?style=flat-square)](https://beerpay.io/raen79/fie?focus=wish)
48
+ -->
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fie"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ module Fie
2
+ require 'fie/railtie'
3
+ require 'fie/commander'
4
+ require 'fie/manipulator'
5
+ end
@@ -0,0 +1,118 @@
1
+ require 'fie/state'
2
+ require 'fie/pools'
3
+ require 'redis'
4
+
5
+ module Fie
6
+ class Commander < ActionCable::Channel::Base
7
+ @@pools_subjects = Set.new
8
+ @@disable_override = false
9
+
10
+ def subscribed
11
+ stream_from Commander.commander_name(params['identifier'])
12
+ initialize_pools
13
+ end
14
+
15
+ def initialize_state(params)
16
+ self.state = State.new \
17
+ view_variables: params['view_variables'],
18
+ controller_name: params['controller_name'],
19
+ action_name: params['action_name'],
20
+ uuid: self.params[:identifier]
21
+ end
22
+
23
+ def handle_pool_callbacks(params)
24
+ subject = params['subject']
25
+ object = Marshal.load(params['object'])
26
+
27
+ run_hook :"#{subject}_pool", object
28
+ end
29
+
30
+ def unsubscribed
31
+ redis.del Fie::Commander.commander_name(params[:identifier])
32
+ end
33
+
34
+ def state
35
+ Marshal.load redis.get(Fie::Commander.commander_name params[:identifier])
36
+ end
37
+
38
+ def state=(state)
39
+ redis.set Fie::Commander.commander_name(params[:identifier]), Marshal.dump(state)
40
+ end
41
+
42
+ def modify_state_using_changelog(params)
43
+ state.update_object_using_changelog(params['objects_changelog'])
44
+ end
45
+
46
+ def execute_js_function(name, *arguments)
47
+ ActionCable.server.broadcast \
48
+ Fie::Commander.commander_name(params[:identifier]),
49
+ command: 'execute_function',
50
+ parameters: {
51
+ name: name,
52
+ arguments: arguments
53
+ }
54
+ end
55
+
56
+ private
57
+ def redis
58
+ $redis ||= Redis.new
59
+ end
60
+
61
+ def initialize_pools
62
+ ActionCable.server.broadcast \
63
+ Fie::Commander.commander_name(params[:identifier]),
64
+ command: 'subscribe_to_pools',
65
+ parameters: {
66
+ subjects: @@pools_subjects.to_a
67
+ }
68
+ end
69
+
70
+ class << self
71
+ def pool(subject, &block)
72
+ @@pools_subjects.add(subject)
73
+
74
+ pool_name = Fie::Pools.pool_name(subject)
75
+ define_method("#{pool_name}_callback") do |object:|
76
+ @published_object = Marshal.load(object)
77
+ instance_eval(&block)
78
+ end
79
+ end
80
+
81
+ def commander_name(connection_uuid)
82
+ "commander_#{connection_uuid}"
83
+ end
84
+
85
+ def method_added(name)
86
+ super_commander_method_names = [:subscribed, :refresh_view, :identifier, :state, :unsubscribed, :modify_state_using_changelog]
87
+
88
+ unless @@disable_override || super_commander_method_names.include?(name)
89
+ @@disable_override = true
90
+
91
+ restructure_subclass_method_parameters(name)
92
+
93
+ @@disable_override = false
94
+ end
95
+ end
96
+
97
+ private
98
+ def restructure_subclass_method_parameters(name)
99
+ alias_method("sub_#{name}", name)
100
+ remove_method(name)
101
+ define_method(name) do |params|
102
+ @caller = params['caller'].symbolize_keys
103
+ @controller_name = params['controller_name']
104
+ @action_name = params['action_name']
105
+ @connection_uuid = self.params['identifier']
106
+
107
+ ['caller', 'action', 'controller_name', 'action_name'].each { |param| params.delete param }
108
+
109
+ if params.blank?
110
+ self.send(:"sub_#{name}")
111
+ else
112
+ self.send(:"sub_#{name}", params.symbolize_keys)
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1 @@
1
+ class Fie::CommanderClosed < StandardError; end
@@ -0,0 +1,39 @@
1
+ require 'fie/commander_closed'
2
+ require 'redis'
3
+
4
+ module Fie
5
+ module Manipulator
6
+ def state
7
+ commander_name = Commander.commander_name(@fie_connection_uuid)
8
+
9
+ if commander_exists?(commander_name)
10
+ Marshal.load redis.get(commander_name)
11
+ else
12
+ raise Fie::CommanderClosed, commander_name
13
+ end
14
+ end
15
+
16
+ def execute_js_function(name, *arguments)
17
+ commander_name = Commander.commander_name(@fie_connection_uuid)
18
+
19
+ if commander_exists?(commander_name)
20
+ ActionCable.server.broadcast \
21
+ commander_name,
22
+ command: 'execute_function',
23
+ parameters: {
24
+ name: name,
25
+ arguments: arguments
26
+ }
27
+ end
28
+ end
29
+
30
+ private
31
+ def commander_exists?(commander_name)
32
+ !redis.get(commander_name).nil?
33
+ end
34
+
35
+ def redis
36
+ $redis ||= Redis.new
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,23 @@
1
+ module Fie
2
+ class Pools < ActionCable::Channel::Base
3
+ def subscribed
4
+ stream_from Pools.pool_name(params[:identifier])
5
+ end
6
+
7
+ class << self
8
+ def pool_name(subject)
9
+ "pool_#{subject}"
10
+ end
11
+
12
+ def publish(subject, object)
13
+ ActionCable.server.broadcast \
14
+ pool_name(subject),
15
+ command: 'publish_to_pool',
16
+ parameters: {
17
+ subject: subject,
18
+ object: Marshal.dump(object)
19
+ }
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ module Fie
2
+ class Railtie < Rails::Railtie
3
+ initializer 'fie.load_layout_path', :after => :add_view_paths do |app|
4
+ ActiveSupport.on_load(:action_controller) do
5
+ append_view_path("#{Gem.loaded_specs['fie'].full_gem_path}/lib")
6
+ end
7
+ end
8
+
9
+ initializer 'fie.load_javascript' do |app|
10
+ Rails.application.config.assets.paths << "#{Gem.loaded_specs['fie'].full_gem_path}/vendor/javascript"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,131 @@
1
+ require 'redis'
2
+ require 'fie/state/track'
3
+ require 'fie/state/changelog'
4
+
5
+ module Fie
6
+ class State
7
+ include Fie::Track
8
+ include Fie::Changelog
9
+
10
+ def initialize(view_variables: nil, controller_name:, action_name:, uuid:, attributes: nil)
11
+ @controller_name = controller_name
12
+ @action_name = action_name
13
+ @uuid = uuid
14
+ @view_variables = view_variables
15
+
16
+ if !view_variables.nil?
17
+ initialize_getters_and_setters(view_variables)
18
+ else
19
+ initialize_getters_and_setters(attributes, variables_from_view: false)
20
+ end
21
+
22
+ track_changes_in_objects(self)
23
+ end
24
+
25
+ def attributes
26
+ instance_variables_names = instance_variables - [ :@controller_name, :@action_name, :@uuid, :@view_variables ]
27
+
28
+ attribute_entries_array = instance_variables_names.map do |instance_variable_name|
29
+ attribute_name = instance_variable_name.to_s.gsub('@', '')
30
+ attribute_value = instance_variable_get(instance_variable_name)
31
+
32
+ [attribute_name, attribute_value]
33
+ end
34
+
35
+ attribute_entries_array.to_h
36
+ end
37
+
38
+ def inspect
39
+ object_reference = self.to_s
40
+ pretty_print = "\e[0;33m#{object_reference[0..-2]}\e[0m "
41
+
42
+ attributes.each do |name, value|
43
+ if value.is_a? String
44
+ value = "\e[0;31m#{value.inspect}\e[0m"
45
+ else
46
+ value = "\e[1;34m#{value.inspect}\e[0m"
47
+ end
48
+
49
+ pretty_print += "\n #{name}: #{value}"
50
+ end
51
+
52
+ pretty_print += '>'
53
+
54
+ pretty_print
55
+ end
56
+
57
+ def permeate
58
+ redis.set Fie::Commander.commander_name(@uuid), Marshal.dump(self)
59
+
60
+ rendered_view = ApplicationController.render \
61
+ "#{@controller_name}/#{@action_name}",
62
+ assigns: attributes.merge(fie_controller_name: @controller_name, fie_action_name: @action_name),
63
+ layout: 'fie'
64
+
65
+ ActionCable.server.broadcast \
66
+ Fie::Commander.commander_name(@uuid),
67
+ command: 'refresh_view',
68
+ parameters: {
69
+ html: rendered_view
70
+ }
71
+ end
72
+
73
+ def _dump(level)
74
+ untrack_changes_in_objects(self)
75
+
76
+ Marshal.dump({
77
+ controller_name: @controller_name,
78
+ action_name: @action_name,
79
+ uuid: @uuid,
80
+ attributes: attributes
81
+ })
82
+ end
83
+
84
+ def self._load(string_params)
85
+ params = Marshal.load(string_params)
86
+ new(params)
87
+ end
88
+
89
+ private
90
+ def unmarshal_value(value)
91
+ encryptor = ActiveSupport::MessageEncryptor.new Rails.application.credentials[:secret_key_base]
92
+ decrypted_value = encryptor.decrypt_and_verify(value)
93
+ Marshal.load(decrypted_value)
94
+ end
95
+
96
+ def initialize_getters_and_setters(variables, variables_from_view: true)
97
+ variables.delete('fie_controller_name') if variables['fie_controller_name']
98
+ variables.delete('fie_action_name') if variables['fie_action_name']
99
+
100
+ variables.each do |variable_name, variable_value|
101
+ self.class_eval do
102
+ define_method(variable_name) do
103
+ instance_variable_get("@#{variable_name}")
104
+ end
105
+
106
+ define_method("#{variable_name}=") do |value|
107
+ instance_variable_set("@#{variable_name}", value)
108
+ end
109
+ end
110
+
111
+ if variables_from_view
112
+ instance_variable_set("@#{variable_name}", unmarshal_value(variable_value))
113
+ else
114
+ instance_variable_set("@#{variable_name}", variable_value)
115
+ end
116
+ end
117
+ end
118
+
119
+ def redis
120
+ $redis ||= Redis.new
121
+ end
122
+
123
+ def initialize_from_attributes(attributes)
124
+ unless attributes.blank?
125
+ attributes.each do |attribute_name, attribute_value|
126
+ instance_variable_set("@#{attribute_name}", attribute_value)
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,48 @@
1
+ module Fie
2
+ module Changelog
3
+ def update_object_using_changelog(changelog, object = self)
4
+ object = object.with_indifferent_access if object.is_a?(Hash)
5
+
6
+ unless changelog.blank?
7
+ changelog.each do |node_name, _|
8
+ changelog_node = changelog[node_name]
9
+ is_not_end_of_tree = changelog_node.is_a? Hash
10
+
11
+ if is_not_end_of_tree
12
+ traverse \
13
+ changelog: changelog,
14
+ object: object,
15
+ node_name: node_name,
16
+ changelog_node: changelog_node
17
+ else
18
+ update_object_value \
19
+ object: object,
20
+ node_name:node_name,
21
+ value: changelog_node
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ private
28
+ def traverse(changelog:, object:, node_name:, changelog_node:)
29
+ if object.is_a?(Hash) || object.is_a?(Array)
30
+ node_name = node_name.to_i if object.is_a?(Array)
31
+ object_node = object[node_name]
32
+ update_object_using_changelog(changelog_node, object_node)
33
+ else
34
+ object_node = object.send(node_name)
35
+ update_object_using_changelog(changelog_node, object_node)
36
+ end
37
+ end
38
+
39
+ def update_object_value(object:, node_name:, value:)
40
+ if object.is_a?(Hash) || object.is_a?(Array)
41
+ node_name = node_name.to_i if object.is_a?(Array)
42
+ object[node_name] = value
43
+ else
44
+ object.send("#{node_name}=", value)
45
+ end
46
+ end
47
+ end
48
+ end