rubyview 0.0.2 → 0.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 741e5fa918e6c93f2719516d491baa3713aa6ca2e03ae3268415e33664a6177c
4
- data.tar.gz: 517c6a5099e0ee7a344472b512876c0244e27ff55b86f2f731d821566a2f328e
3
+ metadata.gz: b4b81b1a5a2306f1be3d0dc50366bde36ebc617a0e5c77960998a11a29ac1410
4
+ data.tar.gz: 111df9ded764a1944d63608795b078207c5c508289e458f91a241f1616cde3dd
5
5
  SHA512:
6
- metadata.gz: 50152215fed0616ae6773d3d814042fabed9b3a1a52cf32fe43e088d5bd0edb3a701716ec05de803eb27889f61b71f490ceb90ae5c8461ea35c94c76c06b48a7
7
- data.tar.gz: 28abd11cc8393a0ca0bccc59ce1bb9d65e48fc880a25d9300798ef2a59e4327252b09c9b252c14695fff88ce2b3760d1094a0999e488e04e69389d2d439ecc1f
6
+ metadata.gz: c1a0f5d29f17634321d3ca45a2f210b01844a04d26608be680a347d2b2616cabe7cdfdb55288296a83771cdabf6b02d60079b3ba214e727c5cafcfa646db26cd
7
+ data.tar.gz: 428df3f4b25448e4be2fa597aef40a9bda620eb0a6ee997037ed98769f4ca66275a9515f26a0703d9d5b0ddc509d142080e7c1b3ed51983dd3038746455e2d74
data/bin/rubyview CHANGED
@@ -6,6 +6,11 @@ class RubyViewCLI < Thor
6
6
  def render(template_path)
7
7
  puts RubyView.render(template_path)
8
8
  end
9
+
10
+ desc "eval CODE", "evaluates rubyview code and converts to html"
11
+ def eval(code)
12
+ puts RubyView.evaluate(code)
13
+ end
9
14
  end
10
15
 
11
16
  RubyViewCLI.start(ARGV)
@@ -3,14 +3,19 @@
3
3
 
4
4
  class RubyView
5
5
  class Context
6
- include RubyView::Helpers::Tag, DSL
6
+ include RubyView::Helpers::Tag, DSL, Helpers
7
7
  def initialize
8
8
  end
9
9
 
10
- def call(path_to_template)
10
+ def call(file_path: nil, code: nil)
11
+ return puts 'you must pass in file path or code to func' if !file_path && !code
11
12
  @_rendered_content = ""
12
- template_path = File.join(Dir.pwd, path_to_template)
13
- template_content = File.read(template_path)
13
+ template_content = if file_path
14
+ template_path = File.join(Dir.pwd, path_to_template)
15
+ File.read(template_path)
16
+ else
17
+ code
18
+ end
14
19
  instance_eval(template_content)
15
20
  @_rendered_content
16
21
  end
data/lib/rubyview/dsl.rb CHANGED
@@ -3,5 +3,6 @@ class RubyView
3
3
  def header(...); h1(...); end;
4
4
  def text(...); p(...); end;
5
5
  def container(...); div(...); end;
6
+ def image(...); img(...); end;
6
7
  end
7
8
  end
@@ -4,18 +4,34 @@ class RubyView
4
4
  def h1(...); render_tag(:h1, ...); end;
5
5
  def p(...); render_tag(:p, ...); end;
6
6
  def div(...); render_tag(:div, ...); end;
7
+ def img(image_path, **html_opts)
8
+ url_for_image = if defined?(Rails)
9
+ ActionController::Base.helpers.asset_path(image_path)
10
+ else
11
+ image_path
12
+ end
13
+ render_tag(:img, nil, **html_opts.merge({src: url_for_image}))
14
+ end
15
+
16
+ def merge_classes(classes_1, classes_2)
17
+ (classes_1.split(" ") + classes_2.split(" ")).uniq
18
+ end
7
19
 
8
20
  def render_tag(...) # for storing to rendered content variable.
9
21
  @_rendered_content += generate_tag(...)
10
22
  end
11
23
 
12
- def generate_tag(name, content, **html_opts) # for assembling HTML tag
13
- "<#{name}#{format_html_opts(**html_opts)}>#{content}</#{name}>"
24
+ def generate_tag(name, content = nil, **html_opts, &block) # for assembling HTML tag
25
+ content = block.call if block_given?
26
+ self_closing = [:img].include?(name)
27
+ end_content = "#{content}</#{name}>"
28
+ "<#{name}#{format_html_opts(**html_opts)}#{"/" if self_closing}>#{end_content if !self_closing}"
14
29
  end
15
30
 
16
31
  def format_html_opts(**html_opts)
17
32
  return "" if html_opts.empty?
18
- html_opts.map { |k, v| [k,v].join("=") }.join(" ")
33
+ _html = html_opts.map { |k, v| "#{k}='#{v}'" }.join(" ")
34
+ " " + _html # Add initial space
19
35
  end
20
36
  end
21
37
  end
@@ -0,0 +1,5 @@
1
+ # Empty helpers module with methods to be defined by user. via RubyView.add_helper_method
2
+ class RubyView
3
+ module Helpers
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ class RubyView
2
+ class RailsTemplate
3
+ def call(template, source = "")
4
+ source ||= template.source
5
+ _render = RubyView.evaluate(source)
6
+ _render.dump
7
+ end
8
+ end
9
+ ActionView::Template.register_template_handler(:view, RailsTemplate.new)
10
+ end
@@ -0,0 +1,9 @@
1
+ require 'rails'
2
+
3
+ class RubyView
4
+ class Railtie < ::Rails::Railtie
5
+ initializer "rubyview.configure_rails_initialization" do |app|
6
+ require "rubyview/rails_template"
7
+ end
8
+ end
9
+ end
data/lib/rubyview.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'pry'
1
+ require "pry"
2
2
  require_relative './rubyview/helpers/tag'
3
3
  require_relative './rubyview/dsl'
4
4
  require_relative './rubyview/context'
@@ -6,6 +6,21 @@ require_relative './rubyview/context'
6
6
  class RubyView
7
7
  def self.render(path_to_template)
8
8
  context = RubyView::Context.new
9
- context.call(path_to_template)
9
+ context.call(file_path: path_to_template)
10
10
  end
11
- end
11
+
12
+ def self.evaluate(code)
13
+ context = RubyView::Context.new
14
+ context.call(code: code)
15
+ end
16
+
17
+ def self.add_helper_method(name, &block)
18
+ RubyView::Helpers.module_eval do
19
+ define_method(name, &block)
20
+ end
21
+ end
22
+ end
23
+
24
+ if File.basename($0) != 'rubyview'
25
+ require 'rubyview/railtie' if defined?(Rails::Railtie)
26
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyview
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Indigo Tech
@@ -23,6 +23,20 @@ dependencies:
23
23
  - - ">="
24
24
  - !ruby/object:Gem::Version
25
25
  version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: pry
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
26
40
  description: Write your views with pure ruby
27
41
  email: indigo@tech.tut
28
42
  executables:
@@ -34,7 +48,10 @@ files:
34
48
  - lib/rubyview.rb
35
49
  - lib/rubyview/context.rb
36
50
  - lib/rubyview/dsl.rb
51
+ - lib/rubyview/helpers.rb
37
52
  - lib/rubyview/helpers/tag.rb
53
+ - lib/rubyview/rails_template.rb
54
+ - lib/rubyview/railtie.rb
38
55
  homepage: https://rubygems.org/gems/rubyview
39
56
  licenses:
40
57
  - MIT