Class: Prick::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/prick/environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environments, name, assignments) ⇒ Environment

Returns a new instance of Environment.



65
66
67
68
69
70
71
72
73
74
# File 'lib/prick/environment.rb', line 65

def initialize(environments, name, assignments)
  constrain environments, Environments
  constrain name, String
  constrain assignments, { Symbol => Object }
  @environments = environments
  @name = name
  @assignments = assignments
  @assignments[:inherit] ||= []
  @effective_variables = assignments.transform_values { |v| v.dup } # Deep-dup
end

Instance Attribute Details

#ancestorsObject

Ancestors (array of Environment objects) sorted in dependency order



24
25
26
# File 'lib/prick/environment.rb', line 24

def ancestors
  @ancestors
end

#assignmentsObject (readonly)

Map from variable identifier (Symbol) to value. Inherited variables are not included in #assignments



55
56
57
# File 'lib/prick/environment.rb', line 55

def assignments
  @assignments
end

#effective_variablesObject

Map from variable identifier (Symbol) to effective value. Initially equal to #assignments but are later augmented or merged with the corresponding assignments from inherited environments



63
64
65
# File 'lib/prick/environment.rb', line 63

def effective_variables
  @effective_variables
end

#environmentsObject (readonly)

The enclosing Environments object. Used in #types



11
12
13
# File 'lib/prick/environment.rb', line 11

def environments
  @environments
end

#nameObject (readonly)

Environment name (String)



14
15
16
# File 'lib/prick/environment.rb', line 14

def name
  @name
end

#parentsObject

List of directly inherited environment objects. Assigned by the analyzer



21
22
23
# File 'lib/prick/environment.rb', line 21

def parents
  @parents
end

#super_environmentObject

The ancestor that defines the build attribute or nil if ambigous. Assigned by the analyzer



28
29
30
# File 'lib/prick/environment.rb', line 28

def super_environment
  @super_environment
end

Instance Method Details

#assign?(ident) ⇒ Boolean

Return true if the environment assigns an attribute

Returns:

  • (Boolean)


58
# File 'lib/prick/environment.rb', line 58

def assign?(ident) = @assignments.key?(ident)

#assignersObject

Map from variable identifier to the environment that defines the value. This assumes there are no ambigous assignments



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/prick/environment.rb', line 34

def assigners
  @assigners ||= begin
    effective_variables.keys.map { |ident|
      if assignments.key?(ident)
        [ident, self]
      else
        [ident, ancestors.reverse.find { |env| env.assignments.key?(ident) }]
      end
    }.to_h
  end
end

#bashObject

FIXME build has been out-commented



78
# File 'lib/prick/environment.rb', line 78

def bash() = "build_#{name}" # FIXME build has been out-commented

#bash_environmentObject



80
81
82
83
84
# File 'lib/prick/environment.rb', line 80

def bash_environment
  effective_variables.map { |ident, value|
    ["PRICK_ENVIRONMENT_#{ident.upcase}", value]
  }.to_h
end

#commentObject

Environment comment



47
# File 'lib/prick/environment.rb', line 47

def comment = assignments[:comment]

#dumpObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/prick/environment.rb', line 88

def dump
  puts "#{name}:"
  indent {
    self.each { |ident,val|
#         if ident == :build
#           puts "super: #{super_environment&.name || 'false'}"
#         end
      assigner = assigners[ident]&.name || name
      assigner_str = (assigner != name ? " (#{assigner})" : "")
      if types[ident] == "TEXT"
        puts "#{ident}#{assigner_str}:"
        indent { puts val }
      else
          puts "#{ident}#{assigner_str}: #{val.inspect}"
      end
      if ident == :inherit
        puts "ancestors: #{ancestors.map(&:name).inspect}"
      end
    }
  }
end

#has_super?Boolean

Returns:

  • (Boolean)


30
# File 'lib/prick/environment.rb', line 30

def has_super?() = !super_environment.nil?

#inheritObject

List of names of inherited environments



17
# File 'lib/prick/environment.rb', line 17

def inherit = assignments[:inherit]

#inspectObject



86
# File 'lib/prick/environment.rb', line 86

def inspect = "#<#{self.class} @name=#{@name}>"

#super_bashObject

FIXME same



79
# File 'lib/prick/environment.rb', line 79

def super_bash() = (has_super? ? "build_#{name}_super" : nil) # FIXME same

#typesObject

Map from variable identifier (Symbol) to type. Type is one of the strings listed in Environments::TYPES



51
# File 'lib/prick/environment.rb', line 51

def types() = @environments.types