nested_array 2.0.0 → 2.0.1

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: 6d681422cfda9dbfc86841b49ced1e215bf6dbb0a35676df690748922b4aac7b
4
- data.tar.gz: 151c06bb5ebbf793bd6b90fee9fd3d8dcea0485fda834fa65cceb2e1edc38928
3
+ metadata.gz: 3369503cce207dd479b18992e18ee749342afa1ade5c48a68c4a9edf20615578
4
+ data.tar.gz: be213153cda8406132bcc51a4126a7574ac3ef0067be105b2a3a06a25d2579ea
5
5
  SHA512:
6
- metadata.gz: da25eac83649dcd5ddcaf537be626c682192137bce781dd5e35062970929a262b3cab604624feb4e4cc06a420f1ab5e03f7bc817c661dc0082342adf54666813
7
- data.tar.gz: caeb434ad4c64d90c25d9440ce38fa5173b52c8b6776b071a7ba3c514535a3aed179d85fe83ea70ee8a4bda105d0686c828be1f7e2780cccb3432176c69f8e96
6
+ metadata.gz: 83f15e5a071bbe101818b9e54818c17e51f566a8da9e50a38874e3eb1811cdcdf2fba84bc1357b4e7c585a376f020ac69251ba73860b03908d87f2d42945d4d6
7
+ data.tar.gz: 4e9bfe8749a33e71c6de44c165d0299d29e3695934cf365f9bb77d0b22cce8775dd3c194e4bf13e940110e18d602741426b7342cffba6f1c411a7fd704d70f79
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nested_array (2.0.0)
4
+ nested_array (2.0.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README-ru.md CHANGED
@@ -1,37 +1,45 @@
1
1
  # NestedArray
2
2
 
3
- Предназначен для преобразования в древовидную структуру плоских данных описанных по паттерну «Список смежности» (Adjacency List), то есть в нодах указа предок `parent_id`.
3
+ Предназначен для преобразования в древовидную структуру плоских данных описанных по паттерну «Список смежности» (Adjacency List), то есть в нодах указа предок `parent_id`. Например:
4
4
 
5
5
  ```ruby
6
6
  [
7
7
  {id: 1, parent_id: nil, name: 'first', …},
8
8
  {id: 2, parent_id: 1, name: 'second', …},
9
-
9
+ {id: 3, parent_id: 1, name: 'third', }
10
10
  ]
11
11
  # ↓ ↓ ↓
12
12
  [
13
+ {id: 1, parent_id: nil, name: 'first', children: [
14
+ {id: 2, parent_id: 1, name: 'second', …},
15
+ {id: 3, parent_id: 1, name: 'third', …}
16
+ ], …}
13
17
  ]
14
18
  ```
15
19
 
16
- ## Installation
17
20
 
18
- Add this line to your application's Gemfile:
21
+ ## Установка
22
+
23
+ Добавте строку в _Gemfile_ вашего приложения:
19
24
 
20
25
  ```ruby
21
- gem 'nested_array'
26
+ gem 'nested_array', '~> 2.0.0'
22
27
  ```
23
28
 
24
- And then execute:
25
-
26
- $ bundle
29
+ И затем выполните `bundle`.
27
30
 
28
- Or install it yourself as:
31
+ Или установите его как `gem install nested_array`
29
32
 
30
- $ gem install nested_array
31
33
 
32
- ## Usage
34
+ ## Использование
33
35
 
34
- TODO: Write usage instructions here
36
+ ```html
37
+ <ul>
38
+ <%= Catalogs.all.to_a.to_nested.nested_to_html do |node| %>
39
+ <% link_to "#{node['name']}", catalog_view_path(node['slug']) %>
40
+ <% end %>
41
+ </ul>
42
+ ```
35
43
 
36
44
  ## Development
37
45
 
@@ -41,7 +49,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
41
49
 
42
50
  ## Contributing
43
51
 
44
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/nested_array.
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Zlatov/nested_array.
45
53
 
46
54
  ## License
47
55
 
data/README.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # NestedArray
2
2
 
3
+ It is intended for transformation into a tree structure of flat data described by the “Adjacency List” pattern, that is, the ancestor is specified in the nodes by the field `parent_id`. For instance:
4
+
5
+ ```ruby
6
+ [
7
+ {id: 1, parent_id: nil, name: 'first', …},
8
+ {id: 2, parent_id: 1, name: 'second', …},
9
+ {id: 3, parent_id: 1, name: 'third', …}
10
+ ]
11
+ # ↓ ↓ ↓
12
+ [
13
+ {id: 1, parent_id: nil, name: 'first', children: [
14
+ {id: 2, parent_id: 1, name: 'second', …},
15
+ {id: 3, parent_id: 1, name: 'third', …}
16
+ ], …}
17
+ ]
18
+ ```
3
19
 
4
20
 
5
21
  ## Installation
@@ -7,20 +23,22 @@
7
23
  Add this line to your application's Gemfile:
8
24
 
9
25
  ```ruby
10
- gem 'nested_array'
26
+ gem 'nested_array', '~> 2.0.0'
11
27
  ```
12
28
 
13
- And then execute:
29
+ And then execute: `bundle`.
14
30
 
15
- $ bundle
16
-
17
- Or install it yourself as:
18
-
19
- $ gem install nested_array
31
+ Or install it yourself as `gem install nested_array`.
20
32
 
21
33
  ## Usage
22
34
 
23
- TODO: Write usage instructions here
35
+ ```html
36
+ <ul>
37
+ <%= Catalogs.all.to_a.to_nested.nested_to_html do |node| %>
38
+ <% link_to "#{node['name']}", catalog_view_path(node['slug']) %>
39
+ <% end %>
40
+ </ul>
41
+ ```
24
42
 
25
43
  ## Development
26
44
 
@@ -30,7 +48,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
30
48
 
31
49
  ## Contributing
32
50
 
33
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/nested_array.
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Zlatov/nested_array.
34
52
 
35
53
  ## License
36
54
 
data/bash/push/gem.sh ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -eu
4
+
5
+ cd "$(dirname "${0}")"
6
+
7
+ cd ../..
8
+
9
+ if [[ -z ${1-} ]]
10
+ then
11
+ echo "Не указан путь к скомпилированному гему" 1>&2;
12
+ exit 0
13
+ fi
14
+
15
+ gem push $1
data/bin/bundle ADDED
@@ -0,0 +1,105 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'bundle' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "rubygems"
12
+
13
+ m = Module.new do
14
+ module_function
15
+
16
+ def invoked_as_script?
17
+ File.expand_path($0) == File.expand_path(__FILE__)
18
+ end
19
+
20
+ def env_var_version
21
+ ENV["BUNDLER_VERSION"]
22
+ end
23
+
24
+ def cli_arg_version
25
+ return unless invoked_as_script? # don't want to hijack other binstubs
26
+ return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update`
27
+ bundler_version = nil
28
+ update_index = nil
29
+ ARGV.each_with_index do |a, i|
30
+ if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN
31
+ bundler_version = a
32
+ end
33
+ next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/
34
+ bundler_version = $1 || ">= 0.a"
35
+ update_index = i
36
+ end
37
+ bundler_version
38
+ end
39
+
40
+ def gemfile
41
+ gemfile = ENV["BUNDLE_GEMFILE"]
42
+ return gemfile if gemfile && !gemfile.empty?
43
+
44
+ File.expand_path("../../Gemfile", __FILE__)
45
+ end
46
+
47
+ def lockfile
48
+ lockfile =
49
+ case File.basename(gemfile)
50
+ when "gems.rb" then gemfile.sub(/\.rb$/, gemfile)
51
+ else "#{gemfile}.lock"
52
+ end
53
+ File.expand_path(lockfile)
54
+ end
55
+
56
+ def lockfile_version
57
+ return unless File.file?(lockfile)
58
+ lockfile_contents = File.read(lockfile)
59
+ return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/
60
+ Regexp.last_match(1)
61
+ end
62
+
63
+ def bundler_version
64
+ @bundler_version ||= begin
65
+ env_var_version || cli_arg_version ||
66
+ lockfile_version || "#{Gem::Requirement.default}.a"
67
+ end
68
+ end
69
+
70
+ def load_bundler!
71
+ ENV["BUNDLE_GEMFILE"] ||= gemfile
72
+
73
+ # must dup string for RG < 1.8 compatibility
74
+ activate_bundler(bundler_version.dup)
75
+ end
76
+
77
+ def activate_bundler(bundler_version)
78
+ if Gem::Version.correct?(bundler_version) && Gem::Version.new(bundler_version).release < Gem::Version.new("2.0")
79
+ bundler_version = "< 2"
80
+ end
81
+ gem_error = activation_error_handling do
82
+ gem "bundler", bundler_version
83
+ end
84
+ return if gem_error.nil?
85
+ require_error = activation_error_handling do
86
+ require "bundler/version"
87
+ end
88
+ return if require_error.nil? && Gem::Requirement.new(bundler_version).satisfied_by?(Gem::Version.new(Bundler::VERSION))
89
+ warn "Activating bundler (#{bundler_version}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_version}'`"
90
+ exit 42
91
+ end
92
+
93
+ def activation_error_handling
94
+ yield
95
+ nil
96
+ rescue StandardError, LoadError => e
97
+ e
98
+ end
99
+ end
100
+
101
+ m.load_bundler!
102
+
103
+ if m.invoked_as_script?
104
+ load Gem.bin_path("bundler", "bundle")
105
+ end
data/bin/byebug ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'byebug' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("byebug", "byebug")
data/bin/htmldiff ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'htmldiff' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("diff-lcs", "htmldiff")
data/bin/ldiff ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'ldiff' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("diff-lcs", "ldiff")
data/bin/rake ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rake' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rake", "rake")
data/bin/rspec ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rspec' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rspec-core", "rspec")
@@ -31,6 +31,10 @@ module NestedArray::Nested
31
31
  # Параматры для "склеивания" вложенных структур
32
32
  path_separator: '-=path_separator=-',
33
33
  path_key: 'text',
34
+
35
+ # Настройки формирования массива для опций тега <select>
36
+ option_value: 'id', # Что брать в качестве значений при формировании опций селекта.
37
+ option_text: 'name',
34
38
  }
35
39
  end
36
40
 
@@ -47,8 +51,9 @@ module NestedArray::Nested
47
51
  node = cache[level][i[level]]
48
52
  i[level]+= 1
49
53
  if node != nil
54
+ is_last_children = cache[level][i[level]].blank?
50
55
 
51
- yield(node.clone, parents.clone, level)
56
+ yield(node.clone, parents.clone, level, is_last_children)
52
57
 
53
58
  if !node[options[:children]].nil? && node[options[:children]].length > 0
54
59
  level+= 1
@@ -277,6 +282,29 @@ module NestedArray::Nested
277
282
  html.html_safe
278
283
  end
279
284
 
285
+ #
286
+ # Возвращает массив для формирования опций html-тега <select>
287
+ # с псевдографикой, позволяющей вывести древовидную структуру.
288
+ # ```
289
+ # [['option_text1', 'option_value1'],['option_text2', 'option_value2'],…]
290
+ # ```
291
+ def nested_to_options options={}
292
+ options = NESTED_OPTIONS.merge options
293
+ ret = []
294
+ last = []
295
+ each_nested do |node, parents, level, is_last|
296
+ last[level+1] = is_last
297
+ node_text = node[options[:option_text]]
298
+ node_level = (1..level).map{|l| last[l] == true ? ' ' : '┃'}.join
299
+ node_last = is_last ? '┗' : '┣'
300
+ node_children = node[options[:children]].present? && node[options[:children]].length > 0 ? '┳' : '━'
301
+ option_text = "#{node_level}#{node_last}#{node_children}╸#{node_text}"
302
+ option_value = node[options[:option_value]]
303
+ ret.push [option_text, option_value]
304
+ end
305
+ ret
306
+ end
307
+
280
308
  # "Скеивание" вложенных структур
281
309
  # ноды склеиваются если путь к ним одинаков;
282
310
  # путь определяется из сложения Текстов (конфигурируемо через :path_key);
@@ -1,3 +1,3 @@
1
1
  module NestedArray
2
- VERSION = "2.0.0"
2
+ VERSION = "2.0.1"
3
3
  end
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: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - zlatov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-06 00:00:00.000000000 Z
11
+ date: 2020-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -98,8 +98,15 @@ files:
98
98
  - README.md
99
99
  - Rakefile
100
100
  - bash/build/gem.sh
101
+ - bash/push/gem.sh
101
102
  - bash/test/gem.sh
103
+ - bin/bundle
104
+ - bin/byebug
102
105
  - bin/console
106
+ - bin/htmldiff
107
+ - bin/ldiff
108
+ - bin/rake
109
+ - bin/rspec
103
110
  - bin/setup
104
111
  - lib/.byebug_history
105
112
  - lib/nested_array.rb