`

yml_merger 发布了

 
阅读更多

给大家推荐一个用ruby写的控制YML文件的工具

 

https://github.com/hakehuang/yml_merger

 

可以用这个工具来简单实现一个只读的本地文件数据库。

 

gem install yam_merger

gem install deep_merge

 

yml_merger

This is a tool that helps to organize the YML in different files.

source code

install

gem install yml_merger
gem install deep_merge

usage:

Please refer to the test.

create a yml file as below

records/test.yml

__load__:
  more.yml

<your yml content>

require 'yml_merger'

@entry_yml = "test.yml"
@search_path  = (Pathname.new(File.dirname(__FILE__)).realpath + 'records/').to_s
merge_unit      = YML_Merger.new(
    @entry_yml, @search_path
    )
merged_data     = merge_unit.process()
puts "creating './merged_data.yml'"
File.write('./merged_data.yml', YAML.dump(merged_data))

the test.yml and more.yml content are merged together

YML oraginzations:

you can using a tree toplogic to orgainize you application, the YML files can be catalogies to three types:

  1. the root node which is the entry point of a tree yml

  2. the branch node which is the branch pint of a tree yml

  3. the leaf node which is the leaf node of a tree.

we recommand that each leaf node shall be uniq feature. and branch node defines the branch shared feature with hierarchy.

merging rules:

  • load

indicated some other YML file will be added, this enalbes user to separate yml definitions to different file

__load__
  - a.yml
  - b.yml
  - http://yourhost/c.yml

my_definitions:
  ...
  • add

using to refer to a external hash like

a:
  __add__:
    - b
  ...

b:
  ...

means hash a will include hash be by deep_merge

  • common

this will be processed after all YML files are loaded and merged into a plain hash

a:
  __common__:
    common_settings:
    ...
  b:
  c:

then common settings will be processed and result as below

a:
  b:
    common_settings:
  c:
    common_settings:
  • hierarchy:

this will be processed during yml file loadding.

file a.yml

__load__:
  - b.yml

a:
  some_attr:
    __hierarchy__:
      hiearchy_attr:
        ...
    ta:
      ...

file b:

a:
  some_attr:
    other_attr:
       ...

then the merged result is:

a:
  some_attr:
    other_attr:
      hiearchy_attr:
        ...
      ...
    ta:
      hiearchy_attr:
        ...
      ...
  • remove

this will be processed after merged all yml files

file a.yml

__load__:
  - b.yml

a:
  some_attr:
    __remove__:
      special_attr:
        ...
    ta:
      ...

file b:

a:
  some_attr:
    special_attr:
      ...
    other_attr:
      ...

then the merged result is:

a:
  some_attr:
    other_attr:
      ...
    ta:
      ...
  • replace

this will be processed after merged all yml files

file a.yml

__load__:
  - b.yml

a:
  some_attr:
    __replace__:
      special_attr:
        settings_new:
          ...
    ta:
      ...

file b:

a:
  some_attr:
    special_attr:
      settings_old:
        ...
    other_attr:
      ...

then the merged result is:

a:
  some_attr:
    special_attr:
      settings_new:
        ...
    other_attr:
      ...
    ta:
      ...
  • pre-process-merge

this is deprecated feature, you can use common or hierarchy instead

a:
  node:
    mode: pre-process-merge
    attribute: required
    pre_process_node:
      msg: this is preprocessing parts
  pre_process_node:
    ...

the processing result will be

a:
  pre_process_node:
    msg: this is preprocessing parts
    ...
  • post-process-app
a:
  mode: post-process-app
  attribute: required
  ...

b:
  ...

c:
  mode: post-process-app
  attribute: required
  ...

process result

a:
  ...

c:
  ...

this is use to select one or more nodes from a pre-defined node list e.g

file a.yml

__load__
  - b.yml

a:
  attribute: required

c:
  attribute: required

file b.yml

__common__
  mode: post-process-app

a:
  ...

b:
  ...

c:
  ...

'''

the result will be

```yaml
a:
  ...

c:
  ...
  • post-process-lib
a:
  mode: post-process-lib
  node_1:
    attribute: required
    ...
  node_2:
    ...
  node_3:
    attribute: required

process result

a:
  node_1:
    ...
  node_3:
    ...

this is use to select one or more nodes from a pre-defined node list e.g

file a.yml

__load__
  - b.yml

a:
  f1:
    attribute: required
  f3:
    attribute: required

file b.yml

a: 
  mode: post-process-lib
  f1:
    ...
  f2:
    ...
  f3:
    ...
  f4:
    ...
  f5:
    ...
  ...
'''

the result will be

```yaml
a:
  f1:
  f3:

merge process

       @filestructure = process_file(@ENTRY_YML)
       merge_by_add(@filestructure)
       merge_by_common(@filestructure)
       delete_node(@filestructure,'__common__')
       delete_node(@filestructure,'__load__')
       #delete_node(@filestructure,'__add__')
       post_process(@filestructure)
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics