erb-view 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install:
6
+ - gem install bundler -v 1.14.3
7
+ - gem update --system
8
+ - gem --version
9
+ addons:
10
+ code_climate:
11
+ repo_token: 559d0742a796fc8e75c289d32df56428963f4c6621ad89ab38bf432070fb867d
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in erb-view.gemspec
4
+ gemspec
5
+
6
+ gem 'pry'
7
+
8
+ group :test do
9
+ gem 'simplecov'
10
+ gem 'codeclimate-test-reporter', '~> 1.0.0'
11
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Felipe Philipp
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,94 @@
1
+ # Erb::View
2
+
3
+ [![Build Status](https://travis-ci.org/felipeelias/erb-view.svg?branch=master)](https://travis-ci.org/felipeelias/erb-view)
4
+ [![Test Coverage](https://codeclimate.com/github/felipeelias/erb-view/badges/coverage.svg)](https://codeclimate.com/github/felipeelias/erb-view/coverage)
5
+ [![Code Climate](https://codeclimate.com/github/felipeelias/erb-view/badges/gpa.svg)](https://codeclimate.com/github/felipeelias/erb-view)
6
+ [![Issue Count](https://codeclimate.com/github/felipeelias/erb-view/badges/issue_count.svg)](https://codeclimate.com/github/felipeelias/erb-view)
7
+
8
+ Simple wrapper around ERB that lets you create class based views.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'erb-view'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ ```
21
+ $ bundle
22
+ ```
23
+
24
+ Or install it yourself as:
25
+
26
+ ```
27
+ $ gem install erb-view
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ `Erb::View` lets you create reusable class based views. Start with a configuring the template directory
33
+
34
+ ```ruby
35
+ Erb.root = 'lib/templates'
36
+ ```
37
+
38
+ And then define your class.
39
+
40
+ ```ruby
41
+ class IndexView
42
+ include Erb::View
43
+
44
+ # Reads a file named `index.erb` from the `root` specified
45
+ template :index
46
+
47
+ # This method is available on the ERB context
48
+ def title
49
+ 'Index Page'
50
+ end
51
+ end
52
+ ```
53
+
54
+ In the `root` defined, create a file named `index.erb`
55
+
56
+ ```html
57
+ <h1><%= title %></h1>
58
+ <p>Hello <%= name %>!</p>
59
+ ```
60
+
61
+ And use it in your code
62
+
63
+ ```ruby
64
+ view = IndexView.new
65
+ view.render(name: 'World')
66
+ ```
67
+
68
+ The result will be
69
+
70
+ ```html
71
+ <h1>Index Page</h1>
72
+ <p>Hello World!</p>
73
+ ```
74
+
75
+ ## Development
76
+
77
+ ```sh
78
+ $ git clone git@github.com:felipeelias/erb-view.git
79
+ $ cd erb-view
80
+ $ bin/setup
81
+ $ rake test
82
+ ```
83
+
84
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
85
+
86
+ To install this gem onto your local machine, run `bundle exec rake install`
87
+
88
+ ## Contributing
89
+
90
+ Bug reports and pull requests are welcome on GitHub at https://github.com/felipeelias/erb-view.
91
+
92
+ ## License
93
+
94
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task default: :test
data/_config.yml ADDED
@@ -0,0 +1 @@
1
+ theme: jekyll-theme-minimal
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'erb/view'
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__)
data/bin/setup ADDED
@@ -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
data/erb-view.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'erb/view/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'erb-view'
8
+ spec.version = Erb::View::VERSION
9
+ spec.authors = ['Felipe Philipp']
10
+ spec.email = ['felipeelias@gmail.com']
11
+
12
+ spec.summary = 'Create class based ERB views'
13
+ spec.description = 'Simple wrapper around ERB that lets you create class based views'
14
+ spec.homepage = 'https://github.com/felipeelias/erb-view'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.14'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'minitest', '~> 5.0'
27
+ spec.add_development_dependency 'minitest-rg', '~> 5.0'
28
+ end
@@ -0,0 +1,5 @@
1
+ module Erb
2
+ module View
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
data/lib/erb/view.rb ADDED
@@ -0,0 +1,64 @@
1
+ require 'erb/view/version'
2
+ require 'erb'
3
+ require 'pathname'
4
+
5
+ module Erb
6
+ def root=(path)
7
+ @root = Pathname.new(path).expand_path
8
+ end
9
+
10
+ def root
11
+ @root ||= Pathname.new(__FILE__).join('../../lib/templates')
12
+ end
13
+ module_function :root, :root=
14
+
15
+ module View
16
+ def self.included(base)
17
+ base.extend(ClassMethods)
18
+ end
19
+
20
+ module ClassMethods
21
+ def template(name = nil)
22
+ if name
23
+ @template = name
24
+ else
25
+ @template
26
+ end
27
+ end
28
+
29
+ def exposes(keys = nil)
30
+ @exposes ||= []
31
+
32
+ if keys
33
+ @exposes.concat(Array(keys))
34
+ else
35
+ @exposes
36
+ end
37
+ end
38
+ end
39
+
40
+ def initialize
41
+ self.class.exposes.each do |method|
42
+ next if respond_to?(method)
43
+ define_singleton_method(method) do
44
+ @data.fetch(method)
45
+ end
46
+ end
47
+ end
48
+
49
+ def render(data = {})
50
+ @data = data
51
+ erb(self.class.template.to_s).result(binding)
52
+ end
53
+
54
+ private
55
+
56
+ def erb(template)
57
+ ERB.new(File.read(root.join("#{template}.erb")))
58
+ end
59
+
60
+ def root
61
+ Erb.root
62
+ end
63
+ end
64
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: erb-view
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Felipe Philipp
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-rg
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ description: Simple wrapper around ERB that lets you create class based views
70
+ email:
71
+ - felipeelias@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".codeclimate.yml"
77
+ - ".gitignore"
78
+ - ".rubocop.yml"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - _config.yml
85
+ - bin/console
86
+ - bin/setup
87
+ - erb-view.gemspec
88
+ - lib/erb/view.rb
89
+ - lib/erb/view/version.rb
90
+ homepage: https://github.com/felipeelias/erb-view
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.5.1
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Create class based ERB views
114
+ test_files: []