Class: Appydave::Tools::Jump::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/appydave/tools/jump/location.rb

Overview

Location represents a single development folder location

Examples:

Creating a location

location = Location.new(
  key: 'ad-tools',
  path: '~/dev/ad/appydave-tools',
  jump: 'jad-tools',
  brand: 'appydave',
  type: 'tool',
  tags: ['ruby', 'cli'],
  description: 'AppyDave CLI tools'
)

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

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Location

Returns a new instance of Location.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/appydave/tools/jump/location.rb', line 25

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]
  @tags = Array(attrs[:tags])
  @description = attrs[:description]
end

Instance Attribute Details

#brandObject (readonly)

Returns the value of attribute brand.



23
24
25
# File 'lib/appydave/tools/jump/location.rb', line 23

def brand
  @brand
end

#clientObject (readonly)

Returns the value of attribute client.



23
24
25
# File 'lib/appydave/tools/jump/location.rb', line 23

def client
  @client
end

#descriptionObject (readonly)

Returns the value of attribute description.



23
24
25
# File 'lib/appydave/tools/jump/location.rb', line 23

def description
  @description
end

#jumpObject (readonly)

Returns the value of attribute jump.



23
24
25
# File 'lib/appydave/tools/jump/location.rb', line 23

def jump
  @jump
end

#keyObject (readonly)

Returns the value of attribute key.



23
24
25
# File 'lib/appydave/tools/jump/location.rb', line 23

def key
  @key
end

#pathObject (readonly)

Returns the value of attribute path.



23
24
25
# File 'lib/appydave/tools/jump/location.rb', line 23

def path
  @path
end

#tagsObject (readonly)

Returns the value of attribute tags.



23
24
25
# File 'lib/appydave/tools/jump/location.rb', line 23

def tags
  @tags
end

#typeObject (readonly)

Returns the value of attribute type.



23
24
25
# File 'lib/appydave/tools/jump/location.rb', line 23

def type
  @type
end

Instance Method Details

#searchable_terms(brands: {}, clients: {}) ⇒ Array<String>

Get all searchable text for this location

Parameters:

  • brands (Hash) (defaults to: {})

    Brand definitions with aliases

  • clients (Hash) (defaults to: {})

    Client definitions with aliases

Returns:

  • (Array<String>)

    All searchable terms



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/appydave/tools/jump/location.rb', line 79

def searchable_terms(brands: {}, clients: {})
  terms = [key, path, type, description].compact
  terms.concat(tags)

  # 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_hHash

Convert to hash for JSON serialization

Returns:

  • (Hash)


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/appydave/tools/jump/location.rb', line 61

def to_h
  {
    key: key,
    path: path,
    jump: jump,
    brand: brand,
    client: client,
    type: type,
    tags: tags,
    description: description
  }.compact
end

#valid?Boolean

Check if location is valid

Returns:

  • (Boolean)


54
55
56
# File 'lib/appydave/tools/jump/location.rb', line 54

def valid?
  validate.empty?
end

#validateArray<String>

Validate the location

Returns:

  • (Array<String>)

    List of validation errors (empty if valid)



41
42
43
44
45
46
47
48
49
# File 'lib/appydave/tools/jump/location.rb', line 41

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(validate_tags)
  errors
end