well 0.1.0

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: fd9e1f1862a13a600b295889724ceb507a001f11
4
+ data.tar.gz: a8c333ed750fa9727b14491bd08ef7e1b58a46b2
5
+ SHA512:
6
+ metadata.gz: cb56363ef5d2a413e1d1d61759cd8342ca88370121249d2ffea167d3fa6a8d957c47b5115721f0bc18c0b780c12ab651a47afb68ebe52ad1fdc33a4cbf4fe19b
7
+ data.tar.gz: 15b484ef2abb05443c7a422e9f847938b6cbef5d8e9238a1503c4bc64331347502b55ec33497a91b841698c3023bea27a352fab3fe16c11a4d26f82e4c7eec6c
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Joe Corcoran
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ # Well
2
+
3
+ BEM stuff for ActionView.
4
+
5
+ [![Build](https://travis-ci.org/tape-tv/well.svg?branch=master)](https://travis-ci.org/tape-tv/well)
6
+ [![Docs](https://inch-ci.org/github/tape-tv/well.svg?branch=master)](https://inch-ci.org/github/tape-tv/well)
7
+ [![Gem](https://badge.fury.io/rb/well.svg)](https://rubygems.org/gems/well)
8
+
9
+ ## Usage
10
+
11
+ Use the DSL in your views. The following view...
12
+
13
+ ```erb
14
+ <%= block :div, 'container' do %>
15
+ Text
16
+ <%= element :p, 'foreword' do %>
17
+ More text
18
+ <% end %>
19
+ <% end %>
20
+ ```
21
+
22
+ ...produces this HTML.
23
+
24
+ ```html
25
+ <div class="container">
26
+ Text
27
+ <p class="container__foreword">
28
+ More text
29
+ </p>
30
+ </div>
31
+ ```
32
+
33
+ For more examples, including working with modifiers, see
34
+ [the docs](http://www.rubydoc.info/github/tape-tv/well/master).
35
+
36
+ ## License
37
+
38
+ MIT.
39
+
40
+ ## Contribution
41
+
42
+ Contributions welcome! Create a pull request and we'll get back to you as soon as
43
+ possible. Open an issue to discuss anything big.
44
+
45
+ This project has a code of conduct, found in the root of this repo. Please read it
46
+ before contributing.
47
+
@@ -0,0 +1,12 @@
1
+ # @author Joe Corcoran <joe@corcoran.io>
2
+ # Namespace module.
3
+ module Well
4
+ end
5
+
6
+ require 'well/block'
7
+ require 'well/config'
8
+ require 'well/dsl'
9
+ require 'well/element'
10
+ require 'well/version'
11
+
12
+ ActionView::Base.send(:include, Well::DSL)
@@ -0,0 +1,30 @@
1
+ require 'well/component'
2
+ require 'well/config'
3
+
4
+ module Well
5
+ # Class representing a BEM block component.
6
+ class Block
7
+ include Component
8
+
9
+ # @param [Symbol] tag_name The name of the HTML element to build
10
+ # @param [String] identifier The main CSS class which represents
11
+ # the role of the BEM block
12
+ # @param [Hash] opts
13
+ def initialize(tag_name, identifier, opts = {})
14
+ @tag_name, @identifier, @opts = tag_name, identifier, opts
15
+ end
16
+
17
+ # Since the block is the base element in BEM, this can only return
18
+ # self.
19
+ # @return [Block]
20
+ def base
21
+ self
22
+ end
23
+
24
+ # Builds CSS class representing BEM block component.
25
+ # @return [String]
26
+ def compiled_identifier
27
+ identifier
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,68 @@
1
+ require 'action_view'
2
+
3
+ module Well
4
+ # @abstract
5
+ #
6
+ # Included by both {Block} and {Element}, this is the module
7
+ # which contains the shared fuctionality of all BEM components.
8
+ module Component
9
+ include ActionView::Helpers
10
+
11
+ attr_reader :identifier
12
+
13
+ # Must be defined since many methods from ActionView::Helpers.
14
+ # expect to be able to write to an ActionView::OutputBuffer.
15
+ # @return [ActionView::OutputBuffer]
16
+ def output_buffer
17
+ @output_buffer ||= ActionView::OutputBuffer.new
18
+ end
19
+
20
+ # Extracts BEM modifier from options.
21
+ # @return [String]
22
+ def modifier
23
+ @modifier ||= @opts[:modifier]
24
+ end
25
+
26
+ # Extracts other non-BEM CSS classes from options.
27
+ # @return [Array<String>]
28
+ def other_classes
29
+ @other_classes ||= @opts.fetch(:class) { [] }
30
+ end
31
+
32
+ # Combines all CSS classes for output.
33
+ # @return [Array<String>]
34
+ def output_classes
35
+ @output_classes ||= (
36
+ (other_classes << compiled_identifier << modified_identifier).compact
37
+ )
38
+ end
39
+
40
+ # Used by the DSL to build nested BEM components.
41
+ # @yield Content block is evaluated in the context of self.
42
+ # @return [String]
43
+ def evaluate(&content)
44
+ result = instance_eval(&content) if block_given?
45
+ content_tag(@tag_name, result, class: output_classes)
46
+ end
47
+
48
+ # Must be implemented by composing classes.
49
+ # @abstract
50
+ def compiled_identifier
51
+ raise NotImplementedError
52
+ end
53
+
54
+ # Adds BEM modifier if provided.
55
+ # @return [String]
56
+ def modified_identifier
57
+ return unless modifier
58
+ "#{compiled_identifier}#{Well.config.modifier_separator}#{modifier}"
59
+ end
60
+
61
+ # DSL method used to add a nested element in the context of self.
62
+ # Both block and element components may contain further nested elements.
63
+ # @return [Element]
64
+ def element(*args, &content)
65
+ Element.new(base, *args).evaluate(&content)
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,23 @@
1
+ require 'singleton'
2
+
3
+ module Well
4
+ # Singleton class which holds configuration.
5
+ class Config
6
+ include Singleton
7
+
8
+ attr_accessor :element_separator, :modifier_separator
9
+
10
+ # Initalizes default values for component separators
11
+ # based on recommendations from the BEM spec.
12
+ def initialize
13
+ @element_separator = '__'
14
+ @modifier_separator = '--'
15
+ end
16
+ end
17
+
18
+ # Allows access to the {Config} singleton object.
19
+ # @return [Config]
20
+ def self.config
21
+ Config.instance
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ module Well
2
+ # The module used as the entry point to the Well DSL.
3
+ # This is included in ActionView::Base.
4
+ module DSL
5
+ # Creates a new {Block}, passing all arguments through.
6
+ # @yield The content block is evaluated in the context of the new Block
7
+ def block(*args, &content)
8
+ Block.new(*args).evaluate(&content)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,33 @@
1
+ require 'well/component'
2
+ require 'well/config'
3
+
4
+ module Well
5
+ # Class representing BEM element component.
6
+ class Element
7
+ include Component
8
+
9
+ # @param [Block,Element] base The base element used as evaluation context
10
+ # @param [Symbol] tag_name The name of the HTML element to build
11
+ # @param [String] identifier The main CSS class which represents
12
+ # the role of the BEM block
13
+ # @param [Hash] opts
14
+ def initialize(base, tag_name, identifier, opts = {})
15
+ @base, @tag_name, @identifier, @opts = base, tag_name, identifier, opts
16
+ end
17
+
18
+ # The base evaludation context for a BEM element can be either a {Block}
19
+ # or an {Element}.
20
+ # @return [Block,Element]
21
+ def base
22
+ @base
23
+ end
24
+
25
+ # Builds CSS class representing BEM element component.
26
+ # @return [String]
27
+ def compiled_identifier
28
+ ids = [base.identifier]
29
+ ids << Well.config.element_separator << identifier
30
+ ids.join
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module Well
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+ require 'well'
3
+
4
+ describe Well do
5
+ include Well::DSL
6
+
7
+ specify 'block contains element' do
8
+ output = block(:div, 'container') do
9
+ element(:p, 'intro') { 'Hello' }
10
+ end
11
+
12
+ expect(output).to match_html(
13
+ %Q{
14
+ <div class="container">
15
+ <p class="container__intro">Hello</p>
16
+ </div>
17
+ }
18
+ )
19
+ end
20
+
21
+ specify 'block has modifier' do
22
+ output = block(:section, 'sidebar', modifier: 'left')
23
+
24
+ expect(output).to match_html(
25
+ %Q{<section class="sidebar sidebar--left"></section>}
26
+ )
27
+ end
28
+
29
+ specify 'element has modifier' do
30
+ output = block(:div, 'popup') do
31
+ element(:button, 'sign-up', modifier: 'green') { 'Sign up' }
32
+ end
33
+
34
+ expect(output).to match_html(
35
+ %Q{
36
+ <div class="popup">
37
+ <button class="popup__sign-up popup__sign-up--green">Sign up</button>
38
+ </div>
39
+ }
40
+ )
41
+ end
42
+
43
+ specify 'nested elements' do
44
+ output = block(:article, 'blog-post', modifier: 'old') do
45
+ element(:header, 'splash') do
46
+ element(:h1, 'splash-title') do
47
+ concat('My blog post')
48
+ concat(element(:span, 'splash-icon'))
49
+ end
50
+ end
51
+ end
52
+
53
+ expect(output).to match_html(
54
+ %Q{
55
+ <article class="blog-post blog-post--old">
56
+ <header class="blog-post__splash">
57
+ <h1 class="blog-post__splash-title">
58
+ My blog post
59
+ <span class="blog-post__splash-icon"></span>
60
+ </h1>
61
+ </header>
62
+ </article>
63
+ }
64
+ )
65
+ end
66
+
67
+ specify 'merged classes' do
68
+ output = block(:div, 'wrapper', class: ['foo', 'bar'])
69
+
70
+ expect(output).to match_html(
71
+ %Q{<div class="foo bar wrapper"></div>}
72
+ )
73
+ end
74
+
75
+ specify 'DSL is available inside views' do
76
+ view = ActionView::Base.new
77
+ output = view.render(inline: %Q{<%= block(:div, 'container') { 'Text' } %>})
78
+
79
+ expect(output).to eq(block(:div, 'container') { 'Text' })
80
+ end
81
+ end
@@ -0,0 +1,9 @@
1
+ require 'oga'
2
+
3
+ RSpec::Matchers.define :match_html do |expected|
4
+ match do |actual|
5
+ inspect_actual = Oga.parse_html(actual.strip).inspect
6
+ inspect_expected = Oga.parse_html(expected.gsub(/\n\s{2,}/, '').strip).inspect
7
+ inspect_actual == inspect_expected
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require 'matchers/match_html'
9
+
10
+ RSpec.configure do |config|
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: well
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joe Corcoran
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionview
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: oga
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.8'
69
+ description: Helpers for working with BEM CSS classes when building Rails views.
70
+ email:
71
+ - opensource@tape.tv
72
+ - joe@corcoran.io
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - LICENSE.txt
78
+ - README.md
79
+ - lib/well.rb
80
+ - lib/well/block.rb
81
+ - lib/well/component.rb
82
+ - lib/well/config.rb
83
+ - lib/well/dsl.rb
84
+ - lib/well/element.rb
85
+ - lib/well/version.rb
86
+ - spec/lib/well_spec.rb
87
+ - spec/matchers/match_html.rb
88
+ - spec/spec_helper.rb
89
+ homepage: https://github.com/tape-tv/well
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.4.5.1
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: BEM stuff for ActionView
113
+ test_files:
114
+ - spec/lib/well_spec.rb
115
+ - spec/matchers/match_html.rb
116
+ - spec/spec_helper.rb
117
+ has_rdoc: