structured-data 0.1.0 → 0.2.0

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: 0c7033a829e9a0a17f4d55d068910ff78ddb0d87
4
- data.tar.gz: 8b62bed97d79ee42de7e6190aa02627c609ff4c0
3
+ metadata.gz: 2a491959be0ade618bd52abbe1041163259835b8
4
+ data.tar.gz: d4f95162ec89f43777ff7267f73025b8835435cd
5
5
  SHA512:
6
- metadata.gz: 5989b843b4fedd18aa5d6fbe07ab2c88f1f4609ba2193f3e6febeff35972efe3299dcff0384838544caec6397530089de7657fc40db13196bf1a6584c3f3d547
7
- data.tar.gz: fa0c05c53f95e18de9ecc8d4f9770fc66f03cc1eb0de7cf0bb35459a596cbb91e2f9b4b166051f4aa6835e8ef9d339f0f4ab9b3259e4ef999f272cd16a152dce
6
+ metadata.gz: b991c3ed54fe25b2313a373c8d1ed092fab8ba8dae7c591302c56113bb79caf09601178aef979308522229d1706f89ea8084d031b49c52793addf400952659d9
7
+ data.tar.gz: 974caba471771dbc0cd7d2fc27c3168b2468cf1cd4c8bd8dac20f5e37ebc9e9351a1e7ded7a0faf06dc5a9f3955262649fde327e42a9b4a8aee8689394bda84f
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  .DS_Store
2
2
  Gemfile.lock
3
+ pkg
data/.hound.yml ADDED
@@ -0,0 +1,2 @@
1
+ ruby:
2
+ config_file: .rubocop.yml
data/.rubocop.yml ADDED
@@ -0,0 +1,142 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ DisplayCopNames: true
4
+ DisplayStyleGuide: true
5
+ Exclude: # 自動生成されるものや外部ライブラリは除外
6
+ - 'vendor/**/*'
7
+ - 'db/schema.rb'
8
+
9
+ #
10
+ # Style
11
+ #
12
+
13
+ # 日本人なので日本語で書きたいときもある
14
+ Style/AsciiComments:
15
+ Enabled: false
16
+
17
+ # case
18
+ # when foo?
19
+ # when bar?
20
+ # end
21
+ # のようなものはcaseで書いたほうが見やすい
22
+ Style/EmptyCaseCondition:
23
+ Enabled: false
24
+
25
+ # 明示的に else で nil を返すのはわかりやすいので許可
26
+ Style/EmptyElse:
27
+ EnforcedStyle: empty
28
+
29
+ # 複数行でも -> のほうがきれいに見えることが多い
30
+ Style/Lambda:
31
+ EnforcedStyle: literal
32
+
33
+ # 区切り文字が2個以上必須になる場合のみ_区切りを必須
34
+ Style/NumericLiterals:
35
+ MinDigits: 7
36
+
37
+ # ローカル変数とメソッド呼び出しの区別をつけたほうがわかりやすいことが多い
38
+ Style/RedundantSelf:
39
+ Enabled: false
40
+
41
+ # クォートはお好みで
42
+ Style/StringLiterals:
43
+ Enabled: false
44
+
45
+ # 意味的に先に否定のロジックを書いた方がわかりやすいケースもある
46
+ Style/UnlessElse:
47
+ Enabled: false
48
+
49
+ # 3.0で文字列がデフォルトでfrozenなのを信じてない
50
+ Style/FrozenStringLiteralComment:
51
+ Enabled: false
52
+
53
+ Style/BlockDelimiters:
54
+ # メソッドチェインしている場合は複数行でも{}を使いたいので
55
+ # rubocop -a で変換されないよう除外
56
+ AutoCorrect: false
57
+ # expect { }.to で複数行xメソッドチェインが多発するので
58
+ # specを対象から除外する
59
+ Exclude:
60
+ - "spec/**/*.rb"
61
+
62
+ # 公開ライブラリでもない限り、ドキュメントを書くことはほぼないと考える
63
+ Style/Documentation:
64
+ Enabled: false
65
+
66
+ # 代入の後改行したほうが横に長くなりづらく見やすい
67
+ Style/MultilineAssignmentLayout:
68
+ Enabled: true
69
+
70
+ # メソッドチェインで複数行になったときにインデントが長く横にのびるため
71
+ # 出来る限り短くなるようにする
72
+ Style/AlignParameters:
73
+ EnforcedStyle: with_fixed_indentation
74
+
75
+ # メソッドチェインで複数行になったときにインデントが長く横にのびるため
76
+ # 出来る限り短くなるようにする
77
+ Style/MultilineMethodCallIndentation:
78
+ EnforcedStyle: indented
79
+
80
+ #
81
+ # Lint
82
+ #
83
+
84
+ # 使っていない変数を見つけたい
85
+ Lint/UselessAssignment:
86
+ Enabled: true
87
+
88
+ #
89
+ # Metrics
90
+ #
91
+
92
+ Metrics/AbcSize:
93
+ Max: 30
94
+
95
+ Metrics/BlockNesting:
96
+ Max: 3
97
+
98
+ Metrics/ClassLength:
99
+ CountComments: false
100
+ Max: 100
101
+
102
+ Metrics/ModuleLength:
103
+ CountComments: false
104
+ Max: 100
105
+
106
+ Metrics/CyclomaticComplexity:
107
+ Max: 10
108
+
109
+ Metrics/LineLength:
110
+ Max: 120
111
+ AllowHeredoc: true
112
+ AllowURI: true
113
+ URISchemes:
114
+ - http
115
+ - https
116
+
117
+ Metrics/MethodLength:
118
+ CountComments: false
119
+ Max: 30
120
+
121
+ Metrics/BlockLength:
122
+ # rspec, routesは巨大なブロック不可避なので除外
123
+ Exclude:
124
+ - "spec/**/*.rb"
125
+ - "config/routes.rb"
126
+
127
+
128
+ Metrics/ParameterLists:
129
+ Max: 5
130
+ CountKeywordArgs: true
131
+
132
+ Metrics/PerceivedComplexity:
133
+ Max: 7
134
+
135
+ #
136
+ # Performance
137
+ #
138
+
139
+ # パフォーマンスのメリットよりcasecmpを使った時のわかりづらさのデメリットのほうが大きい
140
+ Performance/Casecmp:
141
+ Enabled: false
142
+
data/README.md CHANGED
@@ -1,13 +1,11 @@
1
1
  # StructuredData
2
2
 
3
- [![CircleCI](https://circleci.com/gh/ukstudio/structured_data/tree/master.svg?style=svg)](https://circleci.com/gh/ukstudio/structured_data/tree/master)
4
-
5
3
  ## Installation
6
4
 
7
5
  Add this line to your application's Gemfile:
8
6
 
9
7
  ```ruby
10
- gem 'structured_data'
8
+ gem 'structured-data'
11
9
  ```
12
10
 
13
11
  And then execute:
@@ -16,7 +14,7 @@ And then execute:
16
14
 
17
15
  Or install it yourself as:
18
16
 
19
- $ gem install structured_data
17
+ $ gem install structured-data
20
18
 
21
19
  ## Usage
22
20
 
@@ -58,10 +56,16 @@ repo.dump
58
56
 
59
57
  ### Rails support
60
58
 
61
- Call the helper method in Controller or View
59
+ ```ruby
60
+ # in controller or view
61
+ breadcrumb_list do
62
+ add url: '/products', name: 'Product'
63
+ add url: '/products/tshirts, name: 'Tshirt'
64
+ end
65
+ ```
62
66
 
63
67
  ```slim
64
- = set_breadcrumb_item(url: '/products', name: 'Products')
68
+ = display_structured_data
65
69
  ```
66
70
 
67
71
  ## Contributing
@@ -1,55 +1,6 @@
1
- require 'action_controller'
2
- require 'action_view'
3
1
  require 'json'
4
2
 
5
3
  module StructuredData
6
- module ViewHelper
7
- def repository
8
- @structured_data_repository ||= StructuredData::Repository.new
9
- end
10
-
11
- def breadcrumb_list
12
- @breadcrumb_list ||= StructuredData::BreadcrumbList.new
13
- end
14
-
15
- def set_breadcrumb_item(url:, name:)
16
- breadcrumb_list << { url: url, name: name }
17
- end
18
-
19
- def site_navigation_element
20
- @site_navigation_element ||= StructuredData::SiteNavigationElement.new
21
- end
22
-
23
- def set_site_navigation_element(url:, name:)
24
- site_navigation_element << { url: url, name: name }
25
- end
26
-
27
- def display_strctured_data
28
- repository << breadcrumb_list unless breadcrumb_list.empty?
29
- repository << site_navigation_element unless site_navigation_element.empty?
30
-
31
- self.content_tag(:script, repository.dump.html_safe, type: 'application/ld+json')
32
- end
33
- end
34
-
35
- module ControllerHelper
36
- def breadcrumb_list
37
- @breadcrumb_list ||= StructuredData::BreadcrumbList.new
38
- end
39
-
40
- def set_breadcrumb_item(url:, name:)
41
- breadcrumb_list << { url: url, name: name }
42
- end
43
-
44
- def site_navigation_element
45
- @site_navigation_element ||= StructuredData::SiteNavigationElement.new
46
- end
47
-
48
- def set_site_navigation_element(url:, name:)
49
- site_navigation_element << { url: url, name: name }
50
- end
51
- end
52
-
53
4
  class Repository
54
5
  def initialize
55
6
  @items = []
@@ -65,14 +16,20 @@ module StructuredData
65
16
  end
66
17
 
67
18
  class BreadcrumbList
19
+ Item = Struct.new('Item', :url, :name)
20
+
68
21
  def initialize
69
22
  @items = []
70
23
  end
71
24
 
72
- def <<(item)
25
+ def add(item)
73
26
  @items << item
74
27
  end
75
28
 
29
+ def <<(item)
30
+ add(item)
31
+ end
32
+
76
33
  def empty?
77
34
  @items.empty?
78
35
  end
@@ -87,6 +44,14 @@ module StructuredData
87
44
  JSON.pretty_generate(hash)
88
45
  end
89
46
 
47
+ def each(&block)
48
+ @items.map { |i| Item.new(i[:url], i[:name]) }.each(&block)
49
+ end
50
+
51
+ def size
52
+ @items.size
53
+ end
54
+
90
55
  private
91
56
 
92
57
  def items_to_hash
@@ -134,5 +99,4 @@ module StructuredData
134
99
  end
135
100
  end
136
101
 
137
- ActionView::Base.send :include, StructuredData::ViewHelper
138
- ActionController::Base.send :include, StructuredData::ControllerHelper
102
+ require 'structured_data/rails_extentions'
@@ -0,0 +1,25 @@
1
+ module StructuredData
2
+ module RailsExtentions
3
+ def self.included(controller)
4
+ controller.helper_method :repository
5
+ controller.helper_method :display_structured_data
6
+
7
+ controller.include StructuredData::RailsExtentions::BreadclumbList
8
+ controller.include StructuredData::RailsExtentions::SiteNavigationElement
9
+ end
10
+
11
+ def repository
12
+ @structured_data_repository ||= StructuredData::Repository.new
13
+ end
14
+
15
+ def display_structured_data
16
+ repository << breadcrumb_list unless breadcrumb_list.empty?
17
+ repository << site_navigation_element unless site_navigation_element.empty?
18
+
19
+ view_context.content_tag(:script, repository.dump.html_safe, type: 'application/ld+json')
20
+ end
21
+ end
22
+ end
23
+
24
+ require 'structured_data/rails_extentions/breadcrumb_list'
25
+ require 'structured_data/rails_extentions/site_navigation_element'
@@ -0,0 +1,24 @@
1
+ module StructuredData
2
+ module RailsExtentions
3
+ module BreadclumbList
4
+ def self.included(controller)
5
+ controller.helper_method :breadcrumb_list
6
+ controller.helper_method :set_breadcrumb_list_item
7
+ end
8
+
9
+ def breadcrumb_list(&block)
10
+ @breadcrumb_list ||= StructuredData::BreadcrumbList.new
11
+
12
+ if block_given?
13
+ @breadcrumb_list.instance_eval(&block)
14
+ else
15
+ @breadcrumb_list
16
+ end
17
+ end
18
+
19
+ def set_breadcrumb_list_item(url:, name:)
20
+ breadcrumb_list << { url: url, name: name }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ module StructuredData
2
+ module RailsExtentions
3
+ module SiteNavigationElement
4
+ def self.included(controller)
5
+ controller.helper_method :site_navigation_element
6
+ controller.helper_method :set_site_navigation_element_item
7
+ end
8
+
9
+ def site_navigation_element
10
+ @site_navigation_element ||= StructuredData::SiteNavigationElement.new
11
+
12
+ if block_given?
13
+ yield @@site_navigation_element
14
+ else
15
+ @site_navigation_element
16
+ end
17
+ end
18
+
19
+ def set_site_navigation_element_item(url:, name:)
20
+ site_navigation_element << { url: url, name: name }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module StructuredData
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: structured-data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - AKAMATSU Yuki
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-21 00:00:00.000000000 Z
11
+ date: 2017-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -74,6 +74,8 @@ extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
76
  - ".gitignore"
77
+ - ".hound.yml"
78
+ - ".rubocop.yml"
77
79
  - Gemfile
78
80
  - LICENSE
79
81
  - README.md
@@ -82,6 +84,9 @@ files:
82
84
  - bin/setup
83
85
  - lib/structured-data.rb
84
86
  - lib/structured_data.rb
87
+ - lib/structured_data/rails_extentions.rb
88
+ - lib/structured_data/rails_extentions/breadcrumb_list.rb
89
+ - lib/structured_data/rails_extentions/site_navigation_element.rb
85
90
  - lib/structured_data/version.rb
86
91
  - structured_data.gemspec
87
92
  homepage: https://github.com/ukstudio/structured-data