porous 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 764d087181654089e95897a26692efd6bf2b3302f36a1917695c9e5e7ba3c088
4
+ data.tar.gz: 7eb52c0975ce2611e1e0b57591cd0da848c639a0873785a3e6023e3506852a18
5
+ SHA512:
6
+ metadata.gz: 5ca6f72a3d234ca0e368da80f7c4c839fd6d80b5577ef3eca7df9f2c0788bbb3891a099bb62568eaa3357f3940c8e6c6cd65f5dfd42d25713cb85ea812696df7
7
+ data.tar.gz: e16772a071f6cf91395774bac8b01374173624f5fff38e97539fbe3b72c6586538b2f716a851093cb46ee9aaa583ef1e713ed2baae042f305812ff14afd067ab
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 3.3.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ ## [Planned]
2
+
3
+ - Server-side hot reloading
4
+ - Dynamic page metadata
5
+ - Client-side component rendering
6
+ - Client-side routing
7
+ - Websockets support
8
+ - Client-side hot reloading
9
+
10
+ ## [0.1.0] - 15 February 2024
11
+
12
+ - `porous server` to run apps
13
+ - Only server-side HTML rendering
14
+ - Component-based server-side routing
15
+ - No Entity support
16
+ - No Event support
17
+ - No persistence
18
+ - No client-side (transpiled) components
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at git@exastencil.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Exa Stencil
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.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Porous
2
+
3
+ Porous is a web engine that uses isomorphic Ruby components to build a Progressive Web App. Its use is analogous to a web framework, but the approach is entirely different.
4
+
5
+ This project is a work-in-progress and is not yet even in the Proof of Concept phase. However, if you are interested in a full-stack, everything included solution, that only requires you to use one language (that is arguably easy and enjoyable to write) then feel free to follow this project.
6
+
7
+ The closest thing to this I could find was [Volt](https://github.com/voltrb/volt) or [Silica](https://github.com/youchan/silica), neither of which are active or match the overall development flow I'm looking for.
8
+
9
+ ## Design
10
+
11
+ Applications are composed of `Page`s which are in turn composed of `Component`s. Data is persisted as `Entity`s in configurable store options (memory, disk, database). Client-server communication occurs as `Event`s over WebSockets.
12
+
13
+ ### Page
14
+
15
+ A page is conceptually similar to what would be rendered when visiting a specific URL. So `www.example.com/blog/45` might render a `Blog` page, while `www.example.com/login` might render a `Login` page. Generally, code contained in pages are not reused.
16
+
17
+ ### Components
18
+
19
+ A component is any composable unit of code responsible for rendering markup, potentially based on some state. This is somewhat equivalent to Web Components, in that it can also have some behaviour attached. But it can also simply be based to remove code duplication. Essentially any markup that has behaviour attached or would otherwise create code duplication should probably be in a Components.
20
+
21
+ ### Entities
22
+
23
+ Data in a Porous application is stored as entities. An `Entity` is a agglomeration of virtual attributes constructed from `Datom`s – pieces of information we know to be true about that entity at a point in time. In this way, full history of an entity is automatically maintained. `Datom`s are immutable, allowing for simpler synchronisation between client and server.
24
+
25
+ ### Events
26
+
27
+ Every user maintains one open channel (WebSocket) to the server which can be used to send and receive `Event`s. This is the sole communication mechanism and is used for "database queries", push notifications, presence, analytics, etc.
28
+
29
+ ## Installation
30
+
31
+ Porous is not a framework. You don't build an application with it as a dependency, you run it with your application configuration as input. As such you can easily install the gem globally to use it:
32
+
33
+ $ gem install porous
34
+
35
+ ## Usage
36
+
37
+ Porous is still pre-alpha and so is not ready for usage yet, but the general idea is that you would define your application's entities, pages, components and events in Ruby scripts structured in a specific way. Then you would simply run `porous` while pointing it to that folder and it will spin up a Rack-compatible web server for you to use.
38
+
39
+ To start a new Porous project simply `gem install porous` using whichever Ruby environment you want to use (Ruby 3.0 minimum). Then change to that directory and run:
40
+
41
+ $ porous server
42
+
43
+ By default Porous will run at `loclahost:9292`. Now you can edit `pages/home.rb` or add more pages. Finally restart the server and refresh the page. Hot-reloading will be coming later.
44
+
45
+ ### Running examples
46
+
47
+ To test out some example "apps" using Porous you can navigate to the examples folder and in any folder run:
48
+
49
+ $ porous server
50
+
51
+ ## Development
52
+
53
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/exastencil/porous. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/exastencil/porous/blob/master/CODE_OF_CONDUCT.md).
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
62
+
63
+ ## Code of Conduct
64
+
65
+ Everyone interacting in the Porous project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/exastencil/porous/blob/master/CODE_OF_CONDUCT.md).
66
+
67
+ ## Acknowledgements
68
+
69
+ The work done by Michał Kalbarczyk ([fazibear](https://github.com/fazibear)) on [Inesita](https://github.com/inesita-rb/inesita) and his [VirtualDOM wrapper](https://github.com/fazibear/opal-virtual-dom) which served as the starting point for my implementation of Porous.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/exe/porous ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'porous/cli'
3
+
4
+ Porous::CLI.start ARGV
@@ -0,0 +1,22 @@
1
+ module Porous
2
+ class CLI < Thor
3
+ include Thor::Actions
4
+
5
+ check_unknown_options!
6
+
7
+ namespace :build
8
+
9
+ desc 'build', 'Build static assets'
10
+
11
+ def build
12
+ empty_directory 'static/dist', force: options[:force]
13
+ transpile
14
+ end
15
+
16
+ no_commands do
17
+ def transpile
18
+ # TODO: Use Opal::Builder to generate pages, components and entities into static/dist
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,29 @@
1
+ module Porous
2
+ class CLI < Thor
3
+ include Thor::Actions
4
+
5
+ check_unknown_options!
6
+
7
+ namespace :new
8
+
9
+ desc 'new PROJECT_NAME', 'Create Porous app'
10
+
11
+ method_option :force,
12
+ aliases: :f,
13
+ type: :boolean,
14
+ default: false,
15
+ desc: 'Force overwriting files'
16
+
17
+ def new(project_dir)
18
+ directory('template', project_dir, project_name: project_dir)
19
+
20
+ inside project_dir do
21
+ # run 'bundle install'
22
+ end
23
+ end
24
+
25
+ def self.source_root
26
+ File.dirname(__FILE__)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,26 @@
1
+ module Porous
2
+ class CLI < Thor
3
+ check_unknown_options!
4
+
5
+ namespace :server
6
+
7
+ desc 'server [OPTIONS]', 'Starts Porous server'
8
+ method_option :port,
9
+ aliases: :p,
10
+ type: :numeric,
11
+ default: 9292,
12
+ desc: 'The port Porous will listen on'
13
+
14
+ method_option :host,
15
+ aliases: :h,
16
+ type: :string,
17
+ default: '127.0.0.1',
18
+ desc: 'The host address Porous will bind to'
19
+
20
+ def server
21
+ Rackup::Server.start environment: 'development', builder: <<-BUILDER
22
+ run Porous::Server.new
23
+ BUILDER
24
+ end
25
+ end
26
+ end
@@ -0,0 +1 @@
1
+ static/dist
@@ -0,0 +1,7 @@
1
+ # <%= config[:project_name] %>
2
+
3
+ ```sh
4
+ $ porous run
5
+ ```
6
+
7
+ Go to [http://localhost:8080/](http://localhost:8080/)
@@ -0,0 +1,48 @@
1
+ class Home
2
+ include Porous::Page
3
+ include Porous::Component
4
+
5
+ def route = '/'
6
+
7
+ def render
8
+ div class: 'container p-8 mx-auto lg:h-full xl:px-0 flex flex-wrap' do
9
+ div class: 'flex items-center w-full lg:w-1/2' do
10
+ div class: 'max-w-2xl mb-8' do
11
+ h1 class: 'text-4xl font-bold leading-snug tracking-tight text-gray-800 lg:text-4xl lg:leading-tight xl:text-6xl xl:leading-tight dark:text-white' do
12
+ span class: "bg-gradient-to-br from-pink-500 to-violet-500 bg-clip-text text-transparent box-decoration-clone" do
13
+ text 'Welcome to Porous!'
14
+ end
15
+ end
16
+ p class: 'py-5 text-xl leading-normal text-gray-500 lg:text-xl xl:text-2xl dark:text-gray-300' do
17
+ text 'Porous is an all-in-one application engine for building applications with Web technologies.'
18
+ end
19
+
20
+ div class: 'flex flex-col items-start space-y-3 sm:space-x-4 sm:space-y-0 sm:items-center sm:flex-row' do
21
+ a href: 'https://github.com/exastencil/porous', target: '_blank', rel: 'noopener',
22
+ class: "group relative inline-flex h-12 items-center justify-center overflow-hidden rounded-md bg-indigo-600 px-6 font-medium text-neutral-200 transition hover:scale-110" do
23
+ span 'Get Started'
24
+ div class: "absolute inset-0 flex h-full w-full justify-center [transform:skew(-12deg)_translateX(-100%)] group-hover:duration-1000 group-hover:[transform:skew(-12deg)_translateX(100%)]" do
25
+ div class: "relative h-full w-8 bg-white/20"
26
+ end
27
+ end
28
+ a href: 'https://github.com/exastencil/porous', target: '_blank', rel: 'noopener',
29
+ class: 'flex items-center space-x-2 text-gray-500 dark:text-gray-400' do
30
+ svg role: 'img', width: '24', height: '24', class: 'w-5 h-5', viewBox: '0 0 24 24', fill: 'currentColor',
31
+ xmlns: 'http://www.w3.org/2000/svg' do
32
+ title 'GitHub'
33
+ path d: 'M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12'
34
+ end
35
+ span 'View on GitHub'
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ div class: 'flex items-center justify-center w-full lg:w-1/2' do
42
+ div do
43
+ img src: '/static/hero.png', width: '616', height: '617', class: 'object-cover', alt: 'Hero Illustration'
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
data/lib/porous/cli.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'thor'
2
+
3
+ require 'porous'
4
+
5
+ require 'rack'
6
+ require 'rackup/server'
7
+
8
+ require 'porous/cli/build'
9
+ require 'porous/cli/new'
10
+ require 'porous/cli/server'
@@ -0,0 +1,15 @@
1
+ module Porous
2
+ module Component
3
+ module ClassMethods
4
+ def inject(clazz, opts = {})
5
+ method_name = opts[:as] || clazz.to_s.downcase
6
+ @injections ||= {}
7
+ @injections[method_name] = clazz
8
+ end
9
+
10
+ def injections
11
+ @injections || {}
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ module Porous
2
+ module Component
3
+ module Render
4
+ def render
5
+ raise Error, "Implement #render in #{self.class} component"
6
+ end
7
+
8
+ def before_render
9
+ end
10
+
11
+ def render_virtual_dom
12
+ before_render
13
+ @__virtual_nodes__ = []
14
+ render
15
+ to_vnode
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ module Porous
2
+ module Component
3
+ module Virtual
4
+ # Used to render nested components (no caching on server)
5
+ def component(comp, opts = {})
6
+ raise Error, "Component is nil in #{self.class} class" if comp.nil?
7
+
8
+ @__virtual_nodes__ ||= []
9
+ comp = (comp.is_a?(Class) ? comp.new(opts[:props] || {}) : comp)
10
+ .with_root_component(@root_component)
11
+ .inject
12
+ comp.init
13
+ @__virtual_nodes__ << comp.render_virtual_dom
14
+ self
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,34 @@
1
+ module Porous
2
+ module Component
3
+ include VirtualDOM::DOM
4
+ include Virtual
5
+ include Render
6
+ include Injection
7
+
8
+ def self.included(base)
9
+ base.extend Porous::Component::ClassMethods
10
+ end
11
+
12
+ def initialize(props = {})
13
+ @props = props
14
+ @root_component = self
15
+ init_injections
16
+ inject
17
+ @virtual_dom = render_virtual_dom
18
+ self
19
+ end
20
+
21
+ def props
22
+ @props || {}
23
+ end
24
+
25
+ def with_props(props)
26
+ @props = props
27
+ self
28
+ end
29
+
30
+ def to_s
31
+ @virtual_dom.to_s
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,38 @@
1
+ module Porous
2
+ module Injection
3
+ def init; end
4
+
5
+ def with_root_component(component)
6
+ @root_component = component
7
+ self
8
+ end
9
+
10
+ def inject
11
+ @root_component.injections.each do |name, instance|
12
+ define_singleton_method(name) do
13
+ instance
14
+ end
15
+ end
16
+ self
17
+ end
18
+
19
+ attr_reader :injections
20
+
21
+ def init_injections
22
+ @injections ||= {}
23
+ self.class.injections.each do |name, clazz|
24
+ if clazz.included_modules.include?(Porous::Injection)
25
+ @injections[name] = clazz
26
+ .new
27
+ .with_root_component(@root_component)
28
+ else
29
+ raise Error, "Invalid #{clazz} class, should mixin Porous::Injection"
30
+ end
31
+ end
32
+ @injections.each do |key, instance|
33
+ instance.inject
34
+ instance.init
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,11 @@
1
+ module Porous
2
+ module Page
3
+ # Define the route according to the Router::Routes rules
4
+ def route!
5
+ path = route
6
+ @routes ||= Routes.new.tap do |routes|
7
+ routes.route path, to: self.class
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,113 @@
1
+ module Porous
2
+ module Router
3
+ include Porous::Component
4
+
5
+ attr_reader :params
6
+
7
+ def initialize(props = {})
8
+ @props = props
9
+ @routes = Routes.new
10
+
11
+ # Extract the routes from all Pages
12
+ ObjectSpace.each_object(Class).select { |c| c.included_modules.include? Porous::Page }.each do |klass|
13
+ # TODO: Figure out where these classes are coming from
14
+ # puts "#{klass}: #{klass.ancestors.first} (#{klass.respond_to? :new})"
15
+ next if klass.to_s.start_with? '#<Class:' # skip singleton classes
16
+
17
+ @routes.combine klass.new.route!
18
+ end
19
+
20
+ raise Error, 'No Porous::Page components found!' if @routes.routes.empty?
21
+
22
+ find_route
23
+ parse_url_params
24
+ end
25
+
26
+ def self.included(base)
27
+ base.extend(Porous::Component::ClassMethods)
28
+ end
29
+
30
+ def find_route
31
+ @routes.routes.each do |route|
32
+ next unless path.match(route[:regex])
33
+ return go_to(url_for(route[:redirect_to])) if route[:redirect_to]
34
+
35
+ return @route = route
36
+ end
37
+ raise Error, "Can't find route for url"
38
+ end
39
+
40
+ def find_component(route)
41
+ call_on_enter_callback(route)
42
+ @component_props = route[:component_props]
43
+ route[:component]
44
+ end
45
+
46
+ def render
47
+ component find_component(@route), props: @component_props if @route
48
+ end
49
+
50
+ def call_on_enter_callback(route)
51
+ return unless route[:on_enter]
52
+
53
+ if route[:on_enter].respond_to?(:call)
54
+ route[:on_enter].call
55
+ end
56
+ end
57
+
58
+ def go_to(path)
59
+ # Figure out how to change path
60
+ @props[:path] = path
61
+ find_route
62
+ parse_url_params
63
+ render!
64
+ false
65
+ end
66
+
67
+ def parse_url_params
68
+ @params = component_url_params
69
+ query[1..-1].split('&').each do |param|
70
+ key, value = param.split('=')
71
+ @params[key] = value
72
+ end unless query.empty?
73
+ end
74
+
75
+ def component_url_params
76
+ Hash[@route[:params].zip(path.match(@route[:regex])[1..-1])]
77
+ end
78
+
79
+ def url_for(name, params = nil)
80
+ route = @routes.routes.find do |r|
81
+ case name
82
+ when String
83
+ r[:name] == name || r[:path] == name
84
+ when Object
85
+ r[:component] == name
86
+ else
87
+ false
88
+ end
89
+ end
90
+ route ? url_with_params(route, params) : raise(Error, "Route '#{name}' not found.")
91
+ end
92
+
93
+ def query
94
+ @props ? @props[:query] : '' # Browser.query
95
+ end
96
+
97
+ def path
98
+ @props ? @props[:path] : '/' # @props[:path] # Browser.path
99
+ end
100
+
101
+ def current_url?(name)
102
+ path == url_for(name, params)
103
+ end
104
+
105
+ def url_with_params(route, params)
106
+ path = route[:path]
107
+ params.each do |key, value|
108
+ path = path.gsub(":#{key}", "#{value}")
109
+ end if params
110
+ path
111
+ end
112
+ end
113
+ end