Class: Mongoid::Slug::UniqueSlug::SlugState
- Inherits:
-
Object
- Object
- Mongoid::Slug::UniqueSlug::SlugState
- Defined in:
- lib/mongoid/slug/unique_slug.rb
Instance Attribute Summary collapse
-
#existing_history_slugs ⇒ Object
readonly
Returns the value of attribute existing_history_slugs.
-
#existing_slugs ⇒ Object
readonly
Returns the value of attribute existing_slugs.
-
#last_entered_slug ⇒ Object
readonly
Returns the value of attribute last_entered_slug.
-
#sorted_existing ⇒ Object
readonly
Returns the value of attribute sorted_existing.
Instance Method Summary collapse
- #highest_existing_counter ⇒ Object
- #include_slug ⇒ Object
-
#initialize(slug, documents, pattern) ⇒ SlugState
constructor
A new instance of SlugState.
- #inspect ⇒ Object
- #slug_included? ⇒ Boolean
- #sort_existing_slugs ⇒ Object
Constructor Details
#initialize(slug, documents, pattern) ⇒ SlugState
Returns a new instance of SlugState.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/mongoid/slug/unique_slug.rb', line 11 def initialize(slug, documents, pattern) @slug = slug @documents = documents @pattern = pattern @last_entered_slug = [] @existing_slugs = [] @existing_history_slugs = [] @sorted_existing = [] @documents.each do |doc| history_slugs = doc._slugs next if history_slugs.nil? existing_slugs.push(*history_slugs.find_all { |cur_slug| cur_slug =~ @pattern }) last_entered_slug.push(*history_slugs.last) if history_slugs.last =~ @pattern existing_history_slugs.push(*history_slugs.first(history_slugs.length - 1).find_all { |cur_slug| cur_slug =~ @pattern }) end end |
Instance Attribute Details
#existing_history_slugs ⇒ Object (readonly)
Returns the value of attribute existing_history_slugs.
9 10 11 |
# File 'lib/mongoid/slug/unique_slug.rb', line 9 def existing_history_slugs @existing_history_slugs end |
#existing_slugs ⇒ Object (readonly)
Returns the value of attribute existing_slugs.
9 10 11 |
# File 'lib/mongoid/slug/unique_slug.rb', line 9 def existing_slugs @existing_slugs end |
#last_entered_slug ⇒ Object (readonly)
Returns the value of attribute last_entered_slug.
9 10 11 |
# File 'lib/mongoid/slug/unique_slug.rb', line 9 def last_entered_slug @last_entered_slug end |
#sorted_existing ⇒ Object (readonly)
Returns the value of attribute sorted_existing.
9 10 11 |
# File 'lib/mongoid/slug/unique_slug.rb', line 9 def sorted_existing @sorted_existing end |
Instance Method Details
#highest_existing_counter ⇒ Object
36 37 38 39 |
# File 'lib/mongoid/slug/unique_slug.rb', line 36 def highest_existing_counter sort_existing_slugs @sorted_existing.last || 0 end |
#include_slug ⇒ Object
32 33 34 |
# File 'lib/mongoid/slug/unique_slug.rb', line 32 def include_slug existing_slugs << @slug end |
#inspect ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/mongoid/slug/unique_slug.rb', line 49 def inspect { slug: @slug, existing_slugs: existing_slugs, last_entered_slug: last_entered_slug, existing_history_slugs: existing_history_slugs, sorted_existing: sorted_existing } end |
#slug_included? ⇒ Boolean
28 29 30 |
# File 'lib/mongoid/slug/unique_slug.rb', line 28 def slug_included? existing_slugs.include? @slug end |
#sort_existing_slugs ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/mongoid/slug/unique_slug.rb', line 41 def sort_existing_slugs # remove the slug part and leave the absolute integer part and sort re = /^#{Regexp.escape(@slug)}/ @sorted_existing = existing_slugs.map do |s| s.sub(re, '').to_i.abs end.sort end |