nested_array 1.1.0 → 1.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1226eabba471ff36c05d62793baa80196a9b5126182f43f0e8c17a1d381bbed0
4
- data.tar.gz: 904e12afa79e89cde37afad62e1fb3d71896927d21da1ee8e4232009d253200e
3
+ metadata.gz: 540363bb5ada651621c74257b8d1f79015f3916443ce58a328a040eb53770edf
4
+ data.tar.gz: 743c036b6644d16f66822ad083cb5d3d72ecad7e47976e30e4c058d313101ff2
5
5
  SHA512:
6
- metadata.gz: cd090bbf6513c6423df792a2bc1f9408e6dbc13d29e7ea1314320d87c6209585ba78c51144346f7a508e3db69abedc36bd9c043554cc1c49bdd22569ead700e3
7
- data.tar.gz: 59c94c994b131ef551e2f36c3dfde6a3442522a6dd9260c66d44fd78f9bf81e833113513319b64e81858f24ed0e59b000f54828eb2fc29c8d557a50055e4a08b
6
+ metadata.gz: 45d7dc6fdfebfed713c4c45172a10ede2b4fe5fc562ae4c2a3faf148ac42e44ae1d3823875f45667f3a894bd02ca78df7bee5de217deb8b8dece8817847d9ee5
7
+ data.tar.gz: a0b0e64c7c8fa384ae20381fbbb361154445935d80ae1da742428976bd0f6d27e0389854120203e83741290397eda133d41c7d9966dcb22d565096d7845560c0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nested_array (1.0.1)
4
+ nested_array (1.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -49,4 +49,4 @@ DEPENDENCIES
49
49
  rspec (~> 3.9)
50
50
 
51
51
  BUNDLED WITH
52
- 2.0.1
52
+ 2.1.4
data/README-ru.md CHANGED
@@ -18,12 +18,15 @@
18
18
  ```
19
19
 
20
20
 
21
+
22
+
21
23
  ## Установка
22
24
 
23
25
  Добавте строку в _Gemfile_ вашего приложения:
24
26
 
25
27
  ```ruby
26
- gem 'nested_array', '~> 1.1.0'
28
+ gem 'nested_array', '~> 1.0.0' # версия не расширяет базовый класс Array методами гема. Для использования необходимо преобразовать данные к новому типу, см ниже.
29
+ gem 'nested_array', '~> 2.0.0' # Версия с автоматическим расширением базового класса Array методами гема.
27
30
  ```
28
31
 
29
32
  И затем выполните `bundle install`.
@@ -31,24 +34,31 @@ gem 'nested_array', '~> 1.1.0'
31
34
  Или установите его как `gem install nested_array`
32
35
 
33
36
 
37
+
38
+
34
39
  ## Использование
35
40
 
41
+ <a name="methods"></a>
36
42
  __Список методов__
37
43
 
38
- * `to_nested` — преобразует плоскую структуро во вложенную;
39
- * `each_nested` — перебирает вложенную стуктуру;
40
- * `each_nested!` — перебирает вложенную стуктуру, предоставляя доступ к исходным данным;
41
- * `nested_to_html` — преобразует вложенную структуру в html вёрстку (многоуровневый список `<ul><li>…`);
42
- * `nested_to_options` — преобразует вложенную в массив для формирования опций html-тега `<select>` с псевдографикой;
43
- * `concat_nested` — скеивание вложенных структур, ноды склеиваются если путь к ним одинаков.
44
+ * [to_nested](#to_nested) — преобразует плоскую структуру во вложенную;
45
+ * [each_nested](#each_nested) — перебирает вложенную стуктуру;
46
+ * [each_nested!](#each_nested) — перебирает вложенную стуктуру, предоставляя доступ к исходным данным;
47
+ * [nested_to_html](#nested_to_html) — преобразует вложенную структуру в html вёрстку (многоуровневый список `<ul><li>…`);
48
+ * [nested_to_options](#nested_to_options) — преобразует вложенную структуру в массив для формирования опций html-тега `<select>` с псевдографикой;
49
+ * [concat_nested](#concat_nested) — скеивание вложенных структур, ноды склеиваются если путь к ним одинаков.
50
+
51
+
44
52
 
45
- ### to_nested
46
53
 
47
- Преобразует плоскую структуро во вложенную.
54
+ <a name="to_nested"></a>
55
+ ### to_nested [↑](#methods "К методам")
56
+
57
+ Преобразует плоскую структуру во вложенную.
48
58
 
49
59
  ```ruby
50
60
  a = [{'id' => 1, 'parent_id' => nil}]
51
- a = NestedArray::Array.new
61
+ a = NestedArray::Array.new a
52
62
  b = a.to_nested
53
63
  ```
54
64
 
@@ -77,7 +87,11 @@ b = a.to_nested({
77
87
  })
78
88
  ```
79
89
 
80
- ### each_nested
90
+
91
+
92
+
93
+ <a name="each_nested"></a>
94
+ ### each_nested [↑](#methods "К методам")
81
95
 
82
96
  Перебирает вложенную стуктуру.
83
97
 
@@ -90,21 +104,69 @@ nested.each_nested do |node, parents, level, is_last_children|
90
104
  end
91
105
  ```
92
106
 
93
- ### nested_to_html
107
+
108
+
109
+
110
+ <a name="nested_to_html"></a>
111
+ ### nested_to_html [↑](#methods "К методам")
112
+
113
+ Формирует _html_-код для вывода вложенных структур с использованием вложенных друг в друга списков `<ul>`.
114
+
115
+ __Пример__
116
+
117
+ ```ruby
118
+ [
119
+ {'id' => 1, 'parent_id' => nil, 'name' => 'first'},
120
+ {'id' => 2, 'parent_id' => 1, 'name' => 'second'},
121
+ {'id' => 3, 'parent_id' => 1, 'name' => 'third'}
122
+ ].to_nested.nested_to_html do |node|
123
+ node['name']
124
+ end
125
+ ```
126
+
127
+ Вернёт
128
+
129
+ ```html
130
+ <li>first
131
+ <ul>
132
+ <li>second</li>
133
+ <li>third</li>
134
+ </ul>
135
+ </li>
136
+ ```
137
+
138
+ __Расширенный пример__
139
+
140
+ ```ruby
141
+ .nested_to_html li: '<li class="my">', _ul: '<i></i></ul>' do |node, parents, level|
142
+ block_options = {}
143
+ block_options[:li] = '<li class="my current">' if node['id'] == 2
144
+ [
145
+ "id: #{node['id']}, #{node['name']}, parent name: #{parents[level]&.[]('name')}",
146
+ block_options
147
+ ]
148
+ end
149
+ ```
94
150
 
95
151
  __Опции__
96
152
 
153
+ Все опции могут быть аргументами метода, и только некоторые опции влияют на результат через блок — на лету (последняя строка блока).
154
+
97
155
  ```ruby
98
156
  tabulated: true,
99
157
  inline: false,
100
158
  tab: "\t",
101
- ul: '<ul>',
159
+ ul: '<ul>', # может задаваться блоком
102
160
  _ul: '</ul>',
103
- li: '<li>',
161
+ li: '<li>', # может задаваться блоком
104
162
  _li: '</li>',
105
163
  ```
106
164
 
107
- ### nested_to_options
165
+
166
+
167
+
168
+ <a name="nested_to_options"></a>
169
+ ### nested_to_options [↑](#methods "К методам")
108
170
 
109
171
  Формирования опций для html-тега &lt;select&gt;
110
172
 
@@ -122,7 +184,11 @@ option_value: 'id', # Что брать в качестве значений п
122
184
  option_text: 'name',
123
185
  ```
124
186
 
125
- ### concat_nested
187
+
188
+
189
+
190
+ <a name="concat_nested"></a>
191
+ ### concat_nested [↑](#methods "К методам")
126
192
 
127
193
  Скеивание вложенных структур.
128
194
 
@@ -136,16 +202,23 @@ path_separator: '-=path_separator=-',
136
202
  path_key: 'text',
137
203
  ```
138
204
 
139
- ## Development
140
205
 
141
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
142
206
 
143
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
144
207
 
145
- ## Contributing
208
+ ## Разработка
209
+
210
+
211
+
212
+
213
+
214
+
215
+ ## Содействие
216
+
217
+
218
+
219
+
146
220
 
147
- Bug reports and pull requests are welcome on GitHub at https://github.com/Zlatov/nested_array.
148
221
 
149
- ## License
222
+ ## Лицензия
150
223
 
151
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
224
+ В соответствии с условиями [лицензии MIT](https://opensource.org/licenses/MIT).
data/README.md CHANGED
@@ -24,6 +24,7 @@ Add this line to your application's Gemfile:
24
24
 
25
25
  ```ruby
26
26
  gem 'nested_array', '~> 1.0.0'
27
+ gem 'nested_array', '~> 2.0.0'
27
28
  ```
28
29
 
29
30
  And then execute: `bundle`.
@@ -252,15 +252,16 @@ module NestedArray::Nested
252
252
  i[level]+= 1
253
253
  if node != nil
254
254
 
255
+ node_html, node_options = yield(node.clone, parents.clone, level)
255
256
  html+= options[:tab] * (level * 2 + 1) if options[:tabulated]
256
- html+= options[:li]
257
- html+= yield(node.clone, parents.clone, level)
257
+ html+= node_options&.[](:li) || options[:li]
258
+ html+= node_html.to_s
258
259
 
259
260
  if !node[options[:children]].nil? && node[options[:children]].length > 0
260
261
  level+= 1
261
262
  html+= "\n" if !options[:inline]
262
263
  html+= options[:tab] * (level * 2) if options[:tabulated]
263
- html+= options[:ul]
264
+ html+= node_options&.[](:ul) || options[:ul]
264
265
  html+= "\n" if !options[:inline]
265
266
  parents[level] = node.clone
266
267
  cache[level] = node[options[:children]]
@@ -1,3 +1,3 @@
1
1
  module NestedArray
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
data/test.old/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem 'activesupport'
4
+ gem 'awesome_print'
5
+ gem 'nested_array', path: '../'
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ nested_array (1.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (6.0.2.1)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (>= 0.7, < 2)
12
+ minitest (~> 5.1)
13
+ tzinfo (~> 1.1)
14
+ zeitwerk (~> 2.2)
15
+ awesome_print (1.8.0)
16
+ concurrent-ruby (1.1.6)
17
+ i18n (1.8.2)
18
+ concurrent-ruby (~> 1.0)
19
+ minitest (5.14.0)
20
+ thread_safe (0.3.6)
21
+ tzinfo (1.2.6)
22
+ thread_safe (~> 0.1)
23
+ zeitwerk (2.2.2)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ activesupport
30
+ awesome_print
31
+ nested_array!
32
+
33
+ BUNDLED WITH
34
+ 2.0.1
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -eu
4
+
5
+ cd "$(dirname "${0}")"
6
+
7
+ cd ../..
8
+
9
+ bundle install
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -eu
4
+
5
+ cd "$(dirname "${0}")"
6
+
7
+ cd ../..
8
+
9
+ bundle exec ruby test.rb
data/test.old/test.rb ADDED
@@ -0,0 +1,42 @@
1
+ require 'active_support/all'
2
+ require 'awesome_print'
3
+ require 'nested_array'
4
+
5
+ puts 'Начато тестирование.'.blue
6
+ puts "Версия руби: #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
7
+
8
+ def to_nested_1
9
+ puts "Тестирование #{__method__}".blue
10
+ a = [
11
+ {'id' => 1, 'parent_id' => nil, 'name' => 'first'},
12
+ {'id' => 2, 'parent_id' => 1, 'name' => 'second'},
13
+ {'id' => 3, 'parent_id' => 1, 'name' => 'third'}
14
+ ]
15
+ a = NestedArray::Array.new a
16
+ b = a.to_nested
17
+ return false if b != [
18
+ {'id' => 1, 'parent_id' => nil, 'name' => 'first', 'children' => [
19
+ {'id' => 2, 'parent_id' => 1, 'name' => 'second'},
20
+ {'id' => 3, 'parent_id' => 1, 'name' => 'third'}
21
+ ]},
22
+ ]
23
+ b = a.to_nested add_level: true
24
+ return false if b != [
25
+ {'id' => 1, 'parent_id' => nil, 'name' => 'first', 'level' => 0, 'children' => [
26
+ {'id' => 2, 'parent_id' => 1, 'name' => 'second', 'level' => 1},
27
+ {'id' => 3, 'parent_id' => 1, 'name' => 'third', 'level' => 1}
28
+ ]},
29
+ ]
30
+ return true
31
+ end
32
+
33
+
34
+
35
+
36
+ begin
37
+ raise if !to_nested_1
38
+ rescue => e
39
+ puts 'Тестирование не пройдено.'.red
40
+ exit 1
41
+ end
42
+ puts 'Тестирование пройдено.'.green
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nested_array
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - zlatov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-02-14 00:00:00.000000000 Z
11
+ date: 2020-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -113,6 +113,11 @@ files:
113
113
  - lib/nested_array/nested.rb
114
114
  - lib/nested_array/version.rb
115
115
  - nested_array.gemspec
116
+ - test.old/Gemfile
117
+ - test.old/Gemfile.lock
118
+ - test.old/bash/run/bundle.sh
119
+ - test.old/bash/run/test.sh
120
+ - test.old/test.rb
116
121
  homepage: https://github.com/Zlatov/nested_array
117
122
  licenses:
118
123
  - MIT