Class: RailsConsolePro::Snippets::Snippet
- Inherits:
-
Object
- Object
- RailsConsolePro::Snippets::Snippet
- Defined in:
- lib/rails_console_pro/snippets/snippet.rb
Overview
Represents a single snippet record persisted to disk
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#favorite ⇒ Object
readonly
Returns the value of attribute favorite.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#updated_at ⇒ Object
readonly
Returns the value of attribute updated_at.
Instance Method Summary collapse
- #favorite? ⇒ Boolean
-
#initialize(id:, body:, tags: nil, description: nil, created_at: nil, updated_at: nil, favorite: false, metadata: nil) ⇒ Snippet
constructor
A new instance of Snippet.
- #matches?(term:, tags: nil) ⇒ Boolean
- #summary ⇒ Object
- #to_h ⇒ Object
- #with(**attributes) ⇒ Object
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) = () @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 = .map(&:downcase).freeze @searchable_values = begin values = [@id, @description, @body].compact values.concat() values.map { |val| val.to_s.downcase }.freeze end freeze end |
Instance Attribute Details
#body ⇒ Object (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_at ⇒ Object (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 |
#description ⇒ Object (readonly)
Returns the value of attribute description.
9 10 11 |
# File 'lib/rails_console_pro/snippets/snippet.rb', line 9 def description @description end |
#favorite ⇒ Object (readonly)
Returns the value of attribute favorite.
9 10 11 |
# File 'lib/rails_console_pro/snippets/snippet.rb', line 9 def favorite @favorite end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
9 10 11 |
# File 'lib/rails_console_pro/snippets/snippet.rb', line 9 def id @id end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
9 10 11 |
# File 'lib/rails_console_pro/snippets/snippet.rb', line 9 def end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
9 10 11 |
# File 'lib/rails_console_pro/snippets/snippet.rb', line 9 def end |
#updated_at ⇒ Object (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) } = if .nil? || .empty? true else = Array().compact.map { |tag| tag.to_s.downcase }.uniq = .all? { |tag| .include?(tag) } end matches_term && end |
#summary ⇒ Object
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_h ⇒ Object
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: .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 |