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.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/mongoid/slug/unique_slug.rb', line 13 def initialize(slug, documents, pattern) @slug = slug @documents = documents @pattern = pattern @last_entered_slug = [] @existing_slugs = [] @existing_history_slugs = [] @sorted_existing = [] regexp_pattern = Regexp.new(@pattern) @documents.each do |doc| history_slugs = doc._slugs next if history_slugs.nil? existing_slugs.push(*history_slugs.grep(regexp_pattern)) last_entered_slug.push(*history_slugs.last) if history_slugs.last =~ regexp_pattern existing_history_slugs.push(*history_slugs.first(history_slugs.length - 1).grep(regexp_pattern)) end end |
Instance Attribute Details
#existing_history_slugs ⇒ Object (readonly)
Returns the value of attribute existing_history_slugs.
11 12 13 |
# File 'lib/mongoid/slug/unique_slug.rb', line 11 def existing_history_slugs @existing_history_slugs end |
#existing_slugs ⇒ Object (readonly)
Returns the value of attribute existing_slugs.
11 12 13 |
# File 'lib/mongoid/slug/unique_slug.rb', line 11 def existing_slugs @existing_slugs end |
#last_entered_slug ⇒ Object (readonly)
Returns the value of attribute last_entered_slug.
11 12 13 |
# File 'lib/mongoid/slug/unique_slug.rb', line 11 def last_entered_slug @last_entered_slug end |
#sorted_existing ⇒ Object (readonly)
Returns the value of attribute sorted_existing.
11 12 13 |
# File 'lib/mongoid/slug/unique_slug.rb', line 11 def sorted_existing @sorted_existing end |
Instance Method Details
#highest_existing_counter ⇒ Object
40 41 42 43 |
# File 'lib/mongoid/slug/unique_slug.rb', line 40 def highest_existing_counter sort_existing_slugs @sorted_existing.last || 0 end |
#include_slug ⇒ Object
36 37 38 |
# File 'lib/mongoid/slug/unique_slug.rb', line 36 def include_slug existing_slugs << @slug end |
#inspect ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/mongoid/slug/unique_slug.rb', line 53 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
32 33 34 |
# File 'lib/mongoid/slug/unique_slug.rb', line 32 def slug_included? existing_slugs.include? @slug end |
#sort_existing_slugs ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/mongoid/slug/unique_slug.rb', line 45 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 |