Class: Com::EnvYaml

Inherits:
Object
  • Object
show all
Defined in:
app/stores/com/env_yaml.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ EnvYaml

uses config/viter_template.yml in rails_ui engine as default, config/viter_template.yml in Rails project will override this.



6
7
8
9
10
11
12
13
# File 'app/stores/com/env_yaml.rb', line 6

def initialize(file_name)
  export_path = Rails.root + file_name

  @yaml = Psych::Nodes::Stream.new
  @content = @yaml.children[0].children[0].children
  @parsed = @yaml.to_ruby[0]
  @io = File.new(export_path, 'w+')
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



3
4
5
# File 'app/stores/com/env_yaml.rb', line 3

def content
  @content
end

Instance Method Details

#add(env = 'default', key, value) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/stores/com/env_yaml.rb', line 44

def add(env = 'default', key, value)
  env_content, index = xx(env, key)
  if index
    value_content = env_content[index]
  else
    return
  end

  if value_content.is_a?(Psych::Nodes::Sequence)  # 数组
    value_content.style = 1  # block style
    value_content.children << Psych::Nodes::Scalar.new(value)
  elsif value_content.is_a?(Psych::Nodes::Mapping) && value.is_a?(Hash)  # 有下级内容
    value.each do |item, val|
      value_content.children << Psych::Nodes::Scalar.new(item)
      value_content.children << Psych::Nodes::Scalar.new(val)
    end
  elsif value_content.is_a?(Psych::Nodes::Scalar) && value.is_a?(Hash)
    value_content.children = []
    value.each do |item, val|
      value_content.children << Psych::Nodes::Scalar.new(item)
      value_content.children << Psych::Nodes::Scalar.new(val)
    end
  end

  value_content
end

#append(env = 'default', key, value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/stores/com/env_yaml.rb', line 26

def append(env = 'default', key, value)
  env_content, index = xx(env, key)
  if index
    value_content = env_content[index]
  else
    return
  end

  return if value_content.children.find { |i| i.value == value }

  if value_content.is_a?(Psych::Nodes::Sequence)
    value_content.style = 1  # block style
    value_content.children << Psych::Nodes::Scalar.new(value)
  end

  value_content
end

#dumpObject



20
21
22
23
24
# File 'app/stores/com/env_yaml.rb', line 20

def dump
  @yaml.yaml @io
  @io.fsync
  @io.close
end

#set(env = 'default', key, value) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'app/stores/com/env_yaml.rb', line 71

def set(env = 'default', key, value)
  env_content, index = xx(env, key)
  if index
    value_content = env_content[index]
    value_content.value = value
  elsif key && value
    env_content << Psych::Nodes::Scalar.new(key)
    env_content << Psych::Nodes::Scalar.new(value)
  end
end

#template(template) ⇒ Object



15
16
17
18
# File 'app/stores/com/env_yaml.rb', line 15

def template(template)
  template_path = (Rails.root + template).existence
  File.read(template_path)
end

#xx(env, key) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/stores/com/env_yaml.rb', line 82

def xx(env, key)
  env_index = @content.find_index { |i| i.is_a?(Psych::Nodes::Scalar) && i.value == env }
  env_content = @content[env_index + 1].children
  value_index = env_content.find_index { |i| i.is_a?(Psych::Nodes::Scalar) && i.value == key }
  if value_index
    index = value_index + 1
  else
    index = nil
  end

  [env_content, index]
end