crumpet 0.1.1 → 0.1.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: afd1d70cd4361b4fd1f0742d1d19e92317d63ad3
4
- data.tar.gz: 5b084bd6cdbcc268b3bbbc6c86b5a096133eb4d1
3
+ metadata.gz: 46c020bb8b8fd33ae4e359306880ddf0c37978df
4
+ data.tar.gz: 39dfcf96365be7747864c1718d66b018d1666799
5
5
  SHA512:
6
- metadata.gz: c445f571cc7faa94e6e32383e48faa304f2adce1d5d5c13bffeb1fcb8159cf99ab1e9f63d60a16385d9e2a4194b48636c680f35d406729c5f7ab686bf73856e6
7
- data.tar.gz: bee8cd6b5b023d780cb914675373a767b70988e5dd30c78fe1a57ad5f39a0cdb1bb3616a7d30dce4039387df78a2193629e2f6d2906f39104d90d2ddd47bb911
6
+ metadata.gz: 5e57311ee26d89332c8c4d12b5a5b1d63c73c36df1ac501e96b63d3d9a26e56600392f4ec1013c2b53de4e49541d8001ab096b707ade824d52ec89571633226c
7
+ data.tar.gz: b6366304158073f442de67cdbec8a48001ca2fda0a274a11db2a8d1765d4ecefa4936c1cb67e8b2c873df59aec6c1f6012cf1cb0b2b5834e3c95db973c2d820a
@@ -5,11 +5,16 @@ module Crumpet
5
5
  end
6
6
 
7
7
  def add_crumb(*args)
8
- Crumpet.repository.add_crumb(*args)
8
+ Crumpet.crumbs.add_crumb(*args)
9
9
  end
10
10
 
11
11
  def clear_crumbs
12
- Crumpet.repository.clear
12
+ Crumpet.crumbs.clear
13
+ end
14
+
15
+ def crumbs(&block)
16
+ yield if block_given?
17
+ Crumpet.crumbs
13
18
  end
14
19
 
15
20
  module ClassMethods
@@ -18,11 +23,13 @@ module Crumpet
18
23
  end
19
24
 
20
25
  def crumbs_for(*args, &block)
21
- options = {}
22
- options[:only] = args if args.present?
23
- before_action(options) do |instance|
24
- instance.instance_exec(&block) if block_given?
26
+ if block_given?
27
+ options = args.present? ? { only: args } : {}
28
+ before_action(options) do |instance|
29
+ instance.instance_exec(:crumbs, &block)
30
+ end
25
31
  end
32
+ Crumpet.crumbs
26
33
  end
27
34
  end
28
35
  end
data/lib/crumpet/crumb.rb CHANGED
@@ -1,49 +1,13 @@
1
1
  module Crumpet
2
2
  class Crumb
3
- attr_reader :name, :url, :options
3
+ attr_reader :name, :url, :options, :item_options, :wrapper_options
4
4
 
5
5
  def initialize(name, *args)
6
6
  @options = args.extract_options!
7
+ @item_options = @options.delete(:item_options) || {}
8
+ @wrapper_options = @options.delete(:wrapper_options) || {}
7
9
  @name = name
8
10
  @url = args.first
9
11
  end
10
-
11
- def link?
12
- url.present? && option_or_default(:link).present?
13
- end
14
-
15
- def escape?
16
- option_or_default(:escape)
17
- end
18
-
19
- def truncate?
20
- truncate.present?
21
- end
22
-
23
- def truncate
24
- option_or_default(:truncate)
25
- end
26
-
27
- def wrap?
28
- wrapper.present?
29
- end
30
-
31
- def wrapper
32
- option_or_default(:wrapper)
33
- end
34
-
35
- def item_options
36
- options.fetch(:item_options, {})
37
- end
38
-
39
- def wrapper_options
40
- options.fetch(:wrapper_options, {})
41
- end
42
-
43
- private
44
-
45
- def option_or_default(option)
46
- options.fetch(option, Crumpet.config.send(option.to_sym).clone)
47
- end
48
12
  end
49
13
  end
@@ -12,15 +12,15 @@ module Crumpet
12
12
  def render
13
13
  case option_or_default(:format)
14
14
  when :html
15
- output = repository.map{ |crumb| render_html(crumb) }.join(option_or_default(:separator)).html_safe
15
+ output = crumbs.map{ |crumb| render_html(crumb) }.join(option_or_default(:separator)).html_safe
16
16
  output = content_tag(option_or_default(:container).to_sym, output, build_container_options) if option_or_default(:container).present?
17
17
  output
18
18
  when :xml
19
- output = repository.map{ |crumb| render_xml(crumb) }.join
19
+ output = crumbs.map{ |crumb| render_xml(crumb) }.join
20
20
  output = content_tag(:crumbs, output)
21
21
  output
22
22
  when :json
23
- repository.map{ |crumb| render_json(crumb) }.to_json
23
+ crumbs.map{ |crumb| render_json(crumb) }.to_json
24
24
  else
25
25
  raise NotImplementedError, "unsupported format: #{option_or_default(:format)}"
26
26
  end
@@ -28,8 +28,8 @@ module Crumpet
28
28
 
29
29
  private
30
30
 
31
- def repository
32
- Crumpet.repository
31
+ def crumbs
32
+ Crumpet.crumbs
33
33
  end
34
34
 
35
35
  def render_html(crumb)
@@ -38,7 +38,7 @@ module Crumpet
38
38
  wrapper_options = build_wrapper_options(crumb)
39
39
 
40
40
  output = link?(crumb) ? link_to(name, crumb.url, item_options) : content_tag(:span, name, item_options)
41
- output = content_tag(crumb.wrapper, output, wrapper_options) if crumb.wrap?
41
+ output = content_tag(crumb_option_or_default(crumb, :wrapper), output, wrapper_options) if wrap?(crumb)
42
42
  output
43
43
  end
44
44
 
@@ -57,8 +57,8 @@ module Crumpet
57
57
 
58
58
  def render_name(crumb)
59
59
  name = crumb.name
60
- name = name.truncate(crumb.truncate) if crumb.truncate?
61
- name = h(name) if crumb.escape?
60
+ name = name.truncate(crumb_option_or_default(crumb, :truncate)) if truncate?(crumb)
61
+ name = h(name) if escape?(crumb)
62
62
  name.html_safe
63
63
  end
64
64
 
@@ -71,8 +71,8 @@ module Crumpet
71
71
 
72
72
  item_options[:class] = Array(item_options.fetch(:class, []))
73
73
  item_options[:class] << option_or_default(:default_crumb_class).presence
74
- item_options[:class] << option_or_default(:first_crumb_class).presence if crumb == repository.first
75
- item_options[:class] << option_or_default(:last_crumb_class).presence if crumb == repository.last
74
+ item_options[:class] << option_or_default(:first_crumb_class).presence if crumb == crumbs.first
75
+ item_options[:class] << option_or_default(:last_crumb_class).presence if crumb == crumbs.last
76
76
  item_options[:class].compact!
77
77
  item_options[:class].uniq!
78
78
  item_options.delete(:class) if item_options[:class].blank?
@@ -109,11 +109,27 @@ module Crumpet
109
109
  end
110
110
 
111
111
  def link?(crumb)
112
- option_or_default(:link) && crumb.link? && ( option_or_default(:link_last_crumb) || crumb != repository.last )
112
+ crumb.url.present? && ( crumb_option_or_default(crumb, :link) && ( option_or_default(:link_last_crumb) || crumb != crumbs.last ) )
113
+ end
114
+
115
+ def truncate?(crumb)
116
+ crumb_option_or_default(crumb, :truncate)
117
+ end
118
+
119
+ def wrap?(crumb)
120
+ crumb_option_or_default(crumb, :wrapper).present?
121
+ end
122
+
123
+ def escape?(crumb)
124
+ crumb_option_or_default(crumb, :escape).present?
113
125
  end
114
126
 
115
127
  def option_or_default(option)
116
128
  options.fetch(option, Crumpet.config.send(option.to_sym)).clone
117
129
  end
130
+
131
+ def crumb_option_or_default(crumb, option)
132
+ crumb.options.fetch(option, option_or_default(option))
133
+ end
118
134
  end
119
135
  end
@@ -1,3 +1,3 @@
1
1
  module Crumpet
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -5,7 +5,7 @@ module Crumpet
5
5
  end
6
6
 
7
7
  def crumbs
8
- Crumpet.repository
8
+ Crumpet.crumbs
9
9
  end
10
10
  end
11
11
  end
data/lib/crumpet.rb CHANGED
@@ -12,8 +12,8 @@ module Crumpet
12
12
  @config ||= Crumpet::Configuration.new
13
13
  end
14
14
 
15
- module_function def repository
16
- @repository ||= Crumpet::Repository.new
15
+ module_function def crumbs
16
+ @crumbs ||= Crumpet::Repository.new
17
17
  end
18
18
 
19
19
  def self.render(options = {})
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crumpet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grant Colegate
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-10 00:00:00.000000000 Z
11
+ date: 2017-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails