hensel 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 08c2d34b1ceac0afa538c237967ef7ce7fd6e9d8
4
- data.tar.gz: 710a9707597f29eddc79e054bf4ce83b91277f9a
3
+ metadata.gz: acc9a789f685382612bac321c291e9cfe1d5b42d
4
+ data.tar.gz: 3e0e9e435e490ded71fc8d64ed87809b3d95d6e7
5
5
  SHA512:
6
- metadata.gz: d3bc09b7389bdefdc9f524bdd0f790bbde158629d057299f00991c226ea03fb2fc1e1040f4fc7701f22c1e252a4f3aa7ea7204fdf1027e3ebcfa24fe360d688b
7
- data.tar.gz: ced520e15fd2121ae7bb279d2cad07bb4970762ea1cc8ddf2171ed4601cdcc393444c6c08ec013d18d8b9eec9fbb19d40b4e2a6f1a90d174d761b43aee1ab365
6
+ metadata.gz: b007e8ef81416d5e682c50d848f71d87d24cefda9d0b4ca06998d8d9297acf6cb6765f9e5948796b101ec5431f54c2b8b8f51b3fdd23487c72e8e299f46eb143
7
+ data.tar.gz: 3077a490553b985be304c173a9a31552a564a5c457c0d9ac34ee340fc47f38c4f5e982d44589c8e2c9cbe55381d68fc68a29ace895b74d16cd4201312d809a3e
data/Gemfile CHANGED
@@ -2,3 +2,9 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in hensel.gemspec
4
4
  gemspec
5
+
6
+ group :test do
7
+ gem 'rspec-html-matchers'
8
+ gem 'rack-test', require: 'rack/test'
9
+ gem 'sinatra'
10
+ end
data/README.md CHANGED
@@ -47,6 +47,7 @@ Hensel.configure do |config|
47
47
  config.attr_wrapper = "'"
48
48
  config.whitespace = " "
49
49
  config.parent_element = :ul
50
+ config.before_load = nil
50
51
  end
51
52
  ```
52
53
 
@@ -189,6 +190,18 @@ It will be somthing below.
189
190
 
190
191
  **If `parent_element` is set to a name string, it will be used as a name of the parent element.**
191
192
 
193
+ **If `before_load` is set to Proc, it will be evaluated within the context of `Hensel::Builder`.**
194
+ It will be something below.
195
+
196
+ ```ruby
197
+ Hensel.configuration.before_load = proc { add("Home", "/") }
198
+ builder = Hensel::Builder.new
199
+ home = builder.items.first
200
+ home.text #=> "Home"
201
+ home.url #=> "/"
202
+ ```
203
+
204
+
192
205
  ### Builder
193
206
 
194
207
  #### `add(text, url, **options) -> Hensel::Builder::Item`
@@ -278,9 +291,15 @@ end
278
291
 
279
292
  ### Helpers
280
293
 
281
- The helper is prepared for use in Sinatra and Padrino.
294
+ The helper can be used in your web application.
282
295
 
283
- ### with Sinatra
296
+ #### Basic
297
+
298
+ ```ruby
299
+ include Hensel::Helpers
300
+ ```
301
+
302
+ #### with Sinatra
284
303
 
285
304
  ```ruby
286
305
  class Sample < Sinatra::Base
@@ -300,7 +319,7 @@ class Sample < Sinatra::Base
300
319
  end
301
320
  ```
302
321
 
303
- ### with Padrino
322
+ #### with Padrino
304
323
 
305
324
  ```ruby
306
325
  # config/boot.rb
@@ -323,6 +342,31 @@ class Sample < Padrino::Application
323
342
  end
324
343
  ```
325
344
 
345
+ ### Sinatra/Padrino Helpers
346
+
347
+ If you want to customize more, you should use SinatraHelpers.
348
+ It can be used both in Sinatra and in Padrino.
349
+
350
+ ```ruby
351
+ class Sample < Sinatra::Base
352
+ helpers Hensel::Helpers::SinatraHelpers
353
+ set :hensel, builder_options: { class: "this-is-parent-class-name" },
354
+ renderer: proc { node(:custom_element_name){ item.text }}
355
+
356
+ configure do
357
+ Hensel.configure do |config|
358
+ config.attr_wrapper = '"'
359
+ config.whitespace = ' '
360
+ end
361
+ end
362
+
363
+ get "/" do
364
+ breadcrumbs.add("home", "/")
365
+ breadcrumbs.render
366
+ end
367
+ end
368
+ ```
369
+
326
370
  ## TODO
327
371
 
328
372
  * Support Rails
data/hensel.gemspec CHANGED
@@ -22,7 +22,6 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec"
24
24
  spec.add_development_dependency "nyan-cat-formatter"
25
- spec.add_development_dependency "rspec-html-matchers"
26
25
  spec.add_development_dependency "guard"
27
26
  spec.add_development_dependency "guard-shell"
28
27
  spec.add_development_dependency "guard-rspec"
@@ -31,9 +31,7 @@ module Hensel
31
31
  begin
32
32
  return self if self.instance_of?(Hensel::Builder::Item)
33
33
  ancestor = parent
34
- while ancestor.instance_of?(Hensel::Builder::Node)
35
- ancestor = ancestor.parent
36
- end
34
+ ancestor = ancestor.parent while ancestor.instance_of?(Hensel::Builder::Node)
37
35
  ancestor
38
36
  end
39
37
  end
@@ -11,6 +11,7 @@ module Hensel
11
11
  def initialize(**options)
12
12
  @items = []
13
13
  @options = options
14
+ instance_eval(&Hensel.configuration.before_load) if Hensel.configuration.before_load
14
15
  end
15
16
 
16
17
  # Adds an item to items
@@ -53,8 +54,13 @@ module Hensel
53
54
  !!@processed
54
55
  end
55
56
 
57
+ def item_filters
58
+ @item_filters ||= []
59
+ end
60
+
56
61
  private
57
62
 
63
+ # @!visibility private
58
64
  def map_items
59
65
  items_length = items.length.pred
60
66
  items.map.with_index do |item, index|
@@ -70,10 +76,6 @@ module Hensel
70
76
  end
71
77
  end
72
78
 
73
- def item_filters
74
- @item_filters ||= []
75
- end
76
-
77
79
  # @!visibility private
78
80
  def process!
79
81
  configuration = Hensel.configuration
@@ -13,7 +13,7 @@ module Hensel
13
13
  attr_boolean_accessor :indentation
14
14
  attr_boolean_accessor :last_item_link
15
15
 
16
- attr_accessor :attr_wrapper, :whitespace, :parent_element, :richsnippet
16
+ attr_accessor :attr_wrapper, :whitespace, :parent_element, :richsnippet, :before_load
17
17
 
18
18
  def initialize
19
19
  @bootstrap = false
@@ -24,6 +24,7 @@ module Hensel
24
24
  @attr_wrapper = "'"
25
25
  @whitespace = " "
26
26
  @parent_element = :ul
27
+ @before_load = nil
27
28
  end
28
29
 
29
30
  def [](key)
@@ -0,0 +1,31 @@
1
+ module Hensel
2
+ module Helpers
3
+ module SinatraHelpers
4
+ def __hensel
5
+ @__hensel ||= settings.respond_to?(:hensel) ? settings.hensel : {}
6
+ end
7
+
8
+ def breadcrumbs
9
+ @__breadcrumbs ||=
10
+ begin
11
+ builder = Hensel::Builder.new(__hensel[:builder_options] || {})
12
+ renderer = __hensel[:renderer]
13
+ if renderer && renderer.instance_of?(Proc)
14
+ builder.singleton_class.send(:define_method, :render) do |&block|
15
+ if block.instance_of?(Proc)
16
+ super(&block)
17
+ else
18
+ concatenated_items =
19
+ map_items do |item|
20
+ renderer.arity.zero? ? item.instance_eval(&renderer) : renderer.call(item)
21
+ end.join(Hensel.configuration.indentation? ? "\n" : "")
22
+ content_tag(Hensel.configuration.parent_element, concatenated_items, options)
23
+ end
24
+ end
25
+ end
26
+ builder
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -3,7 +3,7 @@ require 'hensel/builder'
3
3
  module Hensel
4
4
  module Helpers
5
5
  def breadcrumbs
6
- @breadcrumbs ||= Builder.new
6
+ @__breadcrumbs ||= Builder.new
7
7
  end
8
8
  end
9
9
  end
@@ -0,0 +1,2 @@
1
+ require 'hensel'
2
+ require 'hensel/helpers/sinatra_helpers'
@@ -1,3 +1,3 @@
1
1
  module Hensel
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,8 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Hensel::Configuration do
4
+ let(:configuration){ Hensel::Configuration.new }
5
+ let(:variables){ configuration.instance_variables.map{|v| v[1..-1] } }
4
6
  describe ".attr_boolean_accessor" do
5
- let(:configuration){ Hensel::Configuration.new }
6
7
  it "can define an accessor and boolean methods" do
7
8
  expect(configuration.respond_to?(:sample)).to be_false
8
9
  Hensel::Configuration.attr_boolean_accessor :sample
@@ -11,8 +12,31 @@ describe Hensel::Configuration do
11
12
  end
12
13
 
13
14
  describe "#[]" do
15
+ it "can get value of instance variables" do
16
+ variables.each.with_index do |variable, index|
17
+ configuration.send("#{variable}=", index)
18
+ expect(configuration[variable]).to eq(index)
19
+ end
20
+ end
14
21
  end
15
22
 
16
23
  describe "#[]=" do
24
+ it "can set value to instance variables" do
25
+ variables.each.with_index do |variable, index|
26
+ configuration.send(:[]=, variable, index)
27
+ expect(configuration[variable]).to eq(index)
28
+ end
29
+ end
30
+ end
31
+
32
+ describe "before_load" do
33
+ before do
34
+ Hensel.configuration.before_load = proc{ add("Home", "/") }
35
+ end
36
+
37
+ it "should be evaluated within the context of `Hensel::Builder`" do
38
+ builder = Hensel::Builder.new
39
+ expect(builder.items.length).to eq(1)
40
+ end
17
41
  end
18
42
  end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+ require File.expand_path("../../lib/hensel/sinatra", __FILE__)
3
+
4
+ describe Hensel::Helpers::SinatraHelpers do
5
+ include Rack::Test::Methods
6
+
7
+ def app
8
+ Sinatra::Application
9
+ end
10
+
11
+ def mock_app(&block)
12
+ @app = Sinatra::Application
13
+ block_given? ? @app.instance_eval(&block) : @app
14
+ end
15
+
16
+ describe "basic usage" do
17
+ before do
18
+ mock_app do
19
+ helpers Hensel::Helpers::SinatraHelpers
20
+ set :hensel, builder_options: { class: 'custom-breadcrumbs' },
21
+ renderer: proc { node(:custom){ item.text }}
22
+
23
+ configure do
24
+ Hensel.configure do |config|
25
+ config.before_load = proc { add "Home", "/" }
26
+ config.indentation = false
27
+ end
28
+ end
29
+
30
+ get '/hey' do
31
+ breadcrumbs.add("Hey", "/hey")
32
+ breadcrumbs.render
33
+ end
34
+
35
+ get '/ho' do
36
+ breadcrumbs.add("Ho", "/ho")
37
+ breadcrumbs.render do
38
+ node(:awesome){ item.text }
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ it "can change the settings of hensel using `set` method" do
45
+ get '/hey'
46
+ expect(last_response.body).to have_tag(:ul, class: "custom-breadcrumb") do
47
+ with_tag(:custom, text: "Home")
48
+ with_tag(:custom, text: "Hey")
49
+ end
50
+ end
51
+
52
+ it "should respect it when render is passed block as an argument" do
53
+ get '/ho'
54
+ expect(last_response.body).to have_tag(:ul, class: "custom-breadcrumb") do
55
+ with_tag(:awesome, text: "Home")
56
+ with_tag(:awesome, text: "Ho")
57
+ end
58
+ end
59
+ end
60
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require File.expand_path('../../lib/hensel', __FILE__)
2
- require 'rspec-html-matchers'
2
+ require 'bundler' unless defined?(Bunlder)
3
+ Bundler.require(:test)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hensel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - namusyaka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-20 00:00:00.000000000 Z
11
+ date: 2014-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,20 +66,6 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: rspec-html-matchers
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
69
  - !ruby/object:Gem::Dependency
84
70
  name: guard
85
71
  requirement: !ruby/object:Gem::Requirement
@@ -144,13 +130,16 @@ files:
144
130
  - lib/hensel/configuration.rb
145
131
  - lib/hensel/filters.rb
146
132
  - lib/hensel/helpers.rb
133
+ - lib/hensel/helpers/sinatra_helpers.rb
147
134
  - lib/hensel/helpers/tag_helpers.rb
135
+ - lib/hensel/sinatra.rb
148
136
  - lib/hensel/version.rb
149
137
  - spec/builder_spec.rb
150
138
  - spec/configuration_spec.rb
151
139
  - spec/helpers_spec.rb
152
140
  - spec/item_spec.rb
153
141
  - spec/node_spec.rb
142
+ - spec/sinatra_helpers_spec.rb
154
143
  - spec/spec_helper.rb
155
144
  - spec/tag_helpers_spec.rb
156
145
  homepage: https://github.com/namusyaka/hensel
@@ -183,6 +172,7 @@ test_files:
183
172
  - spec/helpers_spec.rb
184
173
  - spec/item_spec.rb
185
174
  - spec/node_spec.rb
175
+ - spec/sinatra_helpers_spec.rb
186
176
  - spec/spec_helper.rb
187
177
  - spec/tag_helpers_spec.rb
188
178
  has_rdoc: