Class: RailsConsolePro::Snippets::Snippet

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_console_pro/snippets/snippet.rb

Overview

Represents a single snippet record persisted to disk

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, body:, tags: nil, description: nil, created_at: nil, updated_at: nil, favorite: false, metadata: nil) ⇒ Snippet



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rails_console_pro/snippets/snippet.rb', line 11

def initialize(id:, body:, tags: nil, description: nil, created_at: nil, updated_at: nil, favorite: false, metadata: nil)
  @id = normalize_id(id)
  @body = normalize_body(body)
  @tags = normalize_tags(tags)
  @description = description&.strip
  @created_at = normalize_time(created_at)
  @updated_at = normalize_time(updated_at || created_at)
  @favorite = !!favorite
   = .is_a?(Hash) ? .transform_keys(&:to_sym).freeze : {}.freeze
  @tags_for_search = @tags.map(&:downcase).freeze
  @searchable_values = begin
    values = [@id, @description, @body].compact
    values.concat(@tags_for_search)
    values.map { |val| val.to_s.downcase }.freeze
  end
  freeze
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



9
10
11
# File 'lib/rails_console_pro/snippets/snippet.rb', line 9

def body
  @body
end

#created_atObject (readonly)

Returns the value of attribute created_at.



9
10
11
# File 'lib/rails_console_pro/snippets/snippet.rb', line 9

def created_at
  @created_at
end

#descriptionObject (readonly)

Returns the value of attribute description.



9
10
11
# File 'lib/rails_console_pro/snippets/snippet.rb', line 9

def description
  @description
end

#favoriteObject (readonly)

Returns the value of attribute favorite.



9
10
11
# File 'lib/rails_console_pro/snippets/snippet.rb', line 9

def favorite
  @favorite
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/rails_console_pro/snippets/snippet.rb', line 9

def id
  @id
end

#metadataObject (readonly)

Returns the value of attribute metadata.



9
10
11
# File 'lib/rails_console_pro/snippets/snippet.rb', line 9

def 
  
end

#tagsObject (readonly)

Returns the value of attribute tags.



9
10
11
# File 'lib/rails_console_pro/snippets/snippet.rb', line 9

def tags
  @tags
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



9
10
11
# File 'lib/rails_console_pro/snippets/snippet.rb', line 9

def updated_at
  @updated_at
end

Instance Method Details

#favorite?Boolean



29
30
31
# File 'lib/rails_console_pro/snippets/snippet.rb', line 29

def favorite?
  favorite
end

#matches?(term:, tags: nil) ⇒ Boolean



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rails_console_pro/snippets/snippet.rb', line 37

def matches?(term:, tags: nil)
  matches_term = term.nil? || term.empty? || searchable_values.any? { |value| value.include?(term.downcase) }
  matches_tags =
    if tags.nil? || tags.empty?
      true
    else
      desired_tags = Array(tags).compact.map { |tag| tag.to_s.downcase }.uniq
      snippet_tags = tags_for_search
      desired_tags.all? { |tag| snippet_tags.include?(tag) }
    end

  matches_term && matches_tags
end

#summaryObject



51
52
53
54
55
56
# File 'lib/rails_console_pro/snippets/snippet.rb', line 51

def summary
  first_line = body.lines.first&.strip || ''
  descriptor = description || first_line
  descriptor = descriptor[0..96] + '…' if descriptor && descriptor.length > 97
  descriptor || id
end

#to_hObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rails_console_pro/snippets/snippet.rb', line 58

def to_h
  {
    id: id,
    body: body,
    tags: tags.dup,
    description: description,
    created_at: created_at,
    updated_at: updated_at,
    favorite: favorite,
    metadata: .dup
  }
end

#with(**attributes) ⇒ Object



33
34
35
# File 'lib/rails_console_pro/snippets/snippet.rb', line 33

def with(**attributes)
  self.class.new(**to_h.merge(attributes))
end