Class: Com::Yaml
- Inherits:
-
Object
- Object
- Com::Yaml
- Defined in:
- app/stores/com/yaml.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
Instance Method Summary collapse
- #add(env = 'default', key, value) ⇒ Object
- #append(env = 'default', key, value) ⇒ Object
- #dump ⇒ Object
-
#initialize(file_name) ⇒ Yaml
constructor
uses config/viter_template.yml in rails_ui engine as default, config/viter_template.yml in Rails project will override this.
- #set(key, value) ⇒ Object
- #template(template) ⇒ Object
- #xx(env, key) ⇒ Object
Constructor Details
#initialize(file_name) ⇒ Yaml
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 |
# File 'app/stores/com/yaml.rb', line 6 def initialize(file_name) export_path = Rails.root + file_name @yaml = Psych::Nodes::Stream.new @content = @yaml.children @io = File.new(export_path, 'w+') end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
3 4 5 |
# File 'app/stores/com/yaml.rb', line 3 def content @content end |
Instance Method Details
#add(env = 'default', key, value) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/stores/com/yaml.rb', line 48 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
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/stores/com/yaml.rb', line 30 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 |
#dump ⇒ Object
19 20 21 22 23 |
# File 'app/stores/com/yaml.rb', line 19 def dump @yaml.yaml @io @io.fsync @io.close end |
#set(key, value) ⇒ Object
25 26 27 28 |
# File 'app/stores/com/yaml.rb', line 25 def set(key, value) @content << Psych::Nodes::Scalar.new(key) @content << Psych::Nodes::Scalar.new(value) end |
#template(template) ⇒ Object
14 15 16 17 |
# File 'app/stores/com/yaml.rb', line 14 def template(template) template_path = (Rails.root + template).existence File.read(template_path) end |
#xx(env, key) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'app/stores/com/yaml.rb', line 75 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 |