Class: Siding::ProjectKey

Inherits:
Object
  • Object
show all
Defined in:
lib/siding/project_key.rb

Constant Summary collapse

DIGEST_LENGTH =

Long enough that a collision is not a practical concern, short enough that the socket path built from it stays inside the platform's ~100-byte sockaddr_un limit.

12

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_root:, uid:, tool_version:, ruby_version:, app_env:) ⇒ ProjectKey

Returns a new instance of ProjectKey.



29
30
31
32
33
34
35
# File 'lib/siding/project_key.rb', line 29

def initialize(app_root:, uid:, tool_version:, ruby_version:, app_env:)
  @app_root = File.realpath(app_root)
  @uid = uid
  @tool_version = tool_version
  @ruby_version = ruby_version
  @app_env = app_env
end

Instance Attribute Details

#app_envObject (readonly)

Returns the value of attribute app_env.



12
13
14
# File 'lib/siding/project_key.rb', line 12

def app_env
  @app_env
end

#app_rootObject (readonly)

Returns the value of attribute app_root.



12
13
14
# File 'lib/siding/project_key.rb', line 12

def app_root
  @app_root
end

#ruby_versionObject (readonly)

Returns the value of attribute ruby_version.



12
13
14
# File 'lib/siding/project_key.rb', line 12

def ruby_version
  @ruby_version
end

#tool_versionObject (readonly)

Returns the value of attribute tool_version.



12
13
14
# File 'lib/siding/project_key.rb', line 12

def tool_version
  @tool_version
end

#uidObject (readonly)

Returns the value of attribute uid.



12
13
14
# File 'lib/siding/project_key.rb', line 12

def uid
  @uid
end

Class Method Details

.app_env_from(env) ⇒ Object



24
25
26
27
# File 'lib/siding/project_key.rb', line 24

def self.app_env_from(env)
  value = env["RAILS_ENV"] || env["RACK_ENV"]
  value.nil? || value.empty? ? "development" : value
end

.for(app_root, env: ENV) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/siding/project_key.rb', line 14

def self.for(app_root, env: ENV)
  new(
    app_root: app_root,
    uid: Process.uid,
    tool_version: Siding::VERSION,
    ruby_version: RUBY_VERSION,
    app_env: app_env_from(env)
  )
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



49
50
51
# File 'lib/siding/project_key.rb', line 49

def ==(other)
  other.is_a?(ProjectKey) && fields == other.fields
end

#digestObject



37
38
39
# File 'lib/siding/project_key.rb', line 37

def digest
  @digest ||= Digest::SHA256.hexdigest(fields.join("\0"))[0, DIGEST_LENGTH]
end

#fieldsObject



45
46
47
# File 'lib/siding/project_key.rb', line 45

def fields
  [app_root, uid.to_s, tool_version, ruby_version, app_env]
end

#hashObject



54
55
56
# File 'lib/siding/project_key.rb', line 54

def hash
  fields.hash
end

#inspectObject



60
61
62
# File 'lib/siding/project_key.rb', line 60

def inspect
  "#<#{self.class} #{label} root=#{app_root} uid=#{uid} tool=#{tool_version} ruby=#{ruby_version}>"
end

#labelObject



41
42
43
# File 'lib/siding/project_key.rb', line 41

def label
  "#{File.basename(app_root)}-#{app_env}-#{digest}"
end

#to_sObject



58
# File 'lib/siding/project_key.rb', line 58

def to_s = label