Class: Appydave::Tools::Jump::Location
- Inherits:
-
Object
- Object
- Appydave::Tools::Jump::Location
- Defined in:
- lib/appydave/tools/jump/location.rb
Overview
git_remote is TRI-STATE and #to_h preserves all three cases:
a URL string (has a remote), an explicit nil (a git repo with no
remote backup), and the key being absent entirely (not a git repo).
Do not collapse nil into absent — they mean different things.
Location represents a single development folder location
Constant Summary collapse
- VALID_KEY_PATTERN =
/\A[a-z0-9][a-z0-9-]*[a-z0-9]\z|\A[a-z0-9]\z/.freeze
- VALID_PATH_PATTERN =
%r{\A[~/]}.freeze
- VALID_TAG_PATTERN =
/\A[a-z0-9][a-z0-9-]*[a-z0-9]\z|\A[a-z0-9]\z/.freeze
Instance Attribute Summary collapse
-
#brand ⇒ Object
readonly
Returns the value of attribute brand.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#git_remote ⇒ Object
readonly
Returns the value of attribute git_remote.
-
#jump ⇒ Object
readonly
Returns the value of attribute jump.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#notes ⇒ Object
readonly
Returns the value of attribute notes.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#git_remote_given? ⇒ Boolean
Was git_remote supplied at all (even as nil)?.
-
#initialize(attrs = {}) ⇒ Location
constructor
A new instance of Location.
-
#searchable_terms(brands: {}, clients: {}) ⇒ Array<String>
Get all searchable text for this location.
-
#to_h ⇒ Hash
Convert to hash for JSON serialization.
-
#valid? ⇒ Boolean
Check if location is valid.
-
#validate ⇒ Array<String>
Validate the location.
Constructor Details
#initialize(attrs = {}) ⇒ Location
Returns a new instance of Location.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/appydave/tools/jump/location.rb', line 31 def initialize(attrs = {}) attrs = normalize_attrs(attrs) @key = attrs[:key] @path = attrs[:path] @jump = attrs[:jump] || default_jump @brand = attrs[:brand] @client = attrs[:client] @type = attrs[:type] @status = attrs[:status] @tags = Array(attrs[:tags]) @description = attrs[:description] @git_remote = attrs[:git_remote] @context = attrs[:context] @notes = attrs[:notes] # git_remote distinguishes "explicit nil" from "not supplied" @git_remote_given = attrs.key?(:git_remote) end |
Instance Attribute Details
#brand ⇒ Object (readonly)
Returns the value of attribute brand.
28 29 30 |
# File 'lib/appydave/tools/jump/location.rb', line 28 def brand @brand end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
28 29 30 |
# File 'lib/appydave/tools/jump/location.rb', line 28 def client @client end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
28 29 30 |
# File 'lib/appydave/tools/jump/location.rb', line 28 def context @context end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
28 29 30 |
# File 'lib/appydave/tools/jump/location.rb', line 28 def description @description end |
#git_remote ⇒ Object (readonly)
Returns the value of attribute git_remote.
28 29 30 |
# File 'lib/appydave/tools/jump/location.rb', line 28 def git_remote @git_remote end |
#jump ⇒ Object (readonly)
Returns the value of attribute jump.
28 29 30 |
# File 'lib/appydave/tools/jump/location.rb', line 28 def jump @jump end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
28 29 30 |
# File 'lib/appydave/tools/jump/location.rb', line 28 def key @key end |
#notes ⇒ Object (readonly)
Returns the value of attribute notes.
28 29 30 |
# File 'lib/appydave/tools/jump/location.rb', line 28 def notes @notes end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
28 29 30 |
# File 'lib/appydave/tools/jump/location.rb', line 28 def path @path end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
28 29 30 |
# File 'lib/appydave/tools/jump/location.rb', line 28 def status @status end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
28 29 30 |
# File 'lib/appydave/tools/jump/location.rb', line 28 def @tags end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
28 29 30 |
# File 'lib/appydave/tools/jump/location.rb', line 28 def type @type end |
Instance Method Details
#git_remote_given? ⇒ Boolean
Was git_remote supplied at all (even as nil)?
54 55 56 |
# File 'lib/appydave/tools/jump/location.rb', line 54 def git_remote_given? @git_remote_given end |
#searchable_terms(brands: {}, clients: {}) ⇒ Array<String>
Get all searchable text for this location
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/appydave/tools/jump/location.rb', line 111 def searchable_terms(brands: {}, clients: {}) terms = [key, path, type, description].compact terms.concat() # Add brand and its aliases if brand && brands[brand] terms << brand terms.concat(Array(brands[brand]['aliases'] || brands[brand][:aliases])) elsif brand terms << brand end # Add client and its aliases if client && clients[client] terms << client terms.concat(Array(clients[client]['aliases'] || clients[client][:aliases])) elsif client terms << client end terms.compact.map(&:to_s).map(&:downcase) end |
#to_h ⇒ Hash
Convert to hash for JSON serialization
Key order matches the on-disk locations.json convention so round-tripping an entry produces a minimal diff.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/appydave/tools/jump/location.rb', line 84 def to_h hash = { key: key, path: path, jump: jump, brand: brand, client: client, type: type, status: status, tags: , description: description }.compact # Preserve the tri-state: an explicit nil means "git repo, no remote" # and must survive; an absent key means "not a git repo". hash[:git_remote] = git_remote if git_remote_given? hash[:context] = context if context hash[:notes] = notes if notes hash end |
#valid? ⇒ Boolean
Check if location is valid
74 75 76 |
# File 'lib/appydave/tools/jump/location.rb', line 74 def valid? validate.empty? end |
#validate ⇒ Array<String>
Validate the location
61 62 63 64 65 66 67 68 69 |
# File 'lib/appydave/tools/jump/location.rb', line 61 def validate errors = [] errors << 'Key is required' if key.nil? || key.empty? errors << "Key '#{key}' is invalid (must be lowercase alphanumeric with hyphens)" if key && !valid_key? errors << 'Path is required' if path.nil? || path.empty? errors << "Path '#{path}' is invalid (must start with ~ or /)" if path && !valid_path? errors.concat() errors end |