Class: Strobe::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/strobe/config.rb

Defined Under Namespace

Classes: InPlaceEditException

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Config

Returns a new instance of Config.



41
42
43
44
# File 'lib/strobe/config.rb', line 41

def initialize(path)
  @path, @hash = path, nil
  update!
end

Class Method Details

.new_or_stub(application_root) ⇒ Object

This function is specifically to migrate legacy applications still using .strobe/config to track the application ID



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/strobe/config.rb', line 9

def self.new_or_stub(application_root)
  application_root = File.expand_path(application_root.to_s)

  IdentityMap.identify self, application_root do
    config_path     = "#{application_root}/strobe/config.json"
    old_config_path = "#{application_root}/.strobe/config"

    if File.exist?(config_path)
      new(config_path)
    elsif File.exist?(old_config_path)
      c = YAML.load_file(old_config_path)
      if Hash === c && application_id = c['STROBE_APPLICATION_ID']
        c.delete('STROBE_APPLICATION_ID')
        inst = stub!(application_root, application_id.to_i)
        inst
      end
    end
  end
end

.stub!(path, application_id = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/strobe/config.rb', line 29

def self.stub!(path, application_id = nil)
  config_path = "#{path}/strobe/config.json"

  if File.exist?(config_path)
    raise "There already is a config file, cannot stub"
  end

  inst = new(config_path)
  inst.set_application_id!(application_id || "[PENDING DEPLOY]")
  inst
end

Instance Method Details

#[](key) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/strobe/config.rb', line 50

def [](key)
  key = key.to_s

  if key.blank?
    raise ArgumentError, "Key cannot be blank"
  end

  if key == "application_id"
    if @hash["application_id"] =~ /^\s*\d+\s*$/
      return @hash["application_id"].to_i
    end

    return
  end

  key.split('.').inject(@hash) do |val, key|
    val && val[key]
  end
end

#application_pathObject



46
47
48
# File 'lib/strobe/config.rb', line 46

def application_path
  File.expand_path('../..', @path)
end

#set_application_id!(id) ⇒ Object

MEGA HACK WARNING! We need to be able to update the application_id in the json file after the first deploy. It would be nice to do this without modifying anything else, so we find the spot where the old value is, swap in the new value



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/strobe/config.rb', line 79

def set_application_id!(id)
  id     = id.to_s
  old_id = @hash["application_id"]
  self[:application_id] = id

  # First make sure that there already is an application_id value
  if old_id
    first, rest = "", raw_json
    raw_id      = old_id.to_json

    while m = /#{Regexp.escape(raw_id)}/.match(rest)
      new_str  = "#{first}#{m.pre_match}#{id.to_json}#{m.post_match}"
      new_hash = JSON.parse(new_str) rescue nil

      if @hash == new_hash
        write_raw_json(new_str)
        return
      end

      rest = m.post_match
      first << m.pre_match << m[0]
    end

    raise InPlaceEditException, "Was unable to set application_id to #{id}"

  # check if there are any other keys, if there are, then we need
  # to inject the application_id key at the very beginning
  elsif @hash.keys.length > 1
    json = raw_json
    json.sub!(/^(\s*)\{\s*(\})?/m) do |m|
      lines = "#{$1}{\n" \
              "#{$1}  // The Strobe Platform ID for the application. This is how the local\n" \
              "#{$1}  // application is mapped to one on the server.\n" \
              "#{$1}  \"application_id\": #{id.to_json},\n\n"

      if $3
        lines << "#{$1}}"
      else
        lines << "#{$1}  "
      end

      lines
    end

    write_raw_json(json)

  # Otherwise, the json file is basically empty, so we probably can just
  # fully reset it
  else

    stub!(id)

  end
end

#unset_application_id!Object



70
71
72
# File 'lib/strobe/config.rb', line 70

def unset_application_id!
  set_application_id! "[PENDING DEPLOY]"
end

#update!Object



134
135
136
137
# File 'lib/strobe/config.rb', line 134

def update!
  parsed = parsed_json
  @hash = parsed
end