kstarv 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MmMxNDMwY2YzNDM1MzZjOTUzZjgxMjY2ZjA3YTJlZWYzNTBjZmU5Yw==
5
+ data.tar.gz: !binary |-
6
+ MGI2NGVlODk2MDFiMzg5NzRmOGU2MzY5YTc0Y2Q3MTJjMGJjYTcwZg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NDFlNTM1YWEyMTVlZjRiMTdlNmZjOGJjYmE4Yzc4ZTIwNWVkNWM0M2EzZGNh
10
+ NjY0MzhkZDRjYjZmNmNmNDczZmE3MTJhMGNmODFjZDZlMWNmYzE5OTA2YTBl
11
+ OTRmN2Y1OWZkZDM4MzIyODdhMjc2MjZmYzZmOTk4YzgxMDRmMTk=
12
+ data.tar.gz: !binary |-
13
+ NGQ0N2EzNDA5NTJkYWJjMWNhZjU5NzZhOTI5ODgyZjdkODhhODY5N2UwNzQx
14
+ NGY4Mjc4MmE3ZDM2NjVmZjQxYjFkMDliYzhlYWNkZWRmN2FlMWM1YzFkMGFh
15
+ YmI1Njk0ZjQyNzg1ZTFiYzJlMGU3OGUxOWI0YjQwZjczNGZmOWM=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ Gemfile.lock
6
+ coverage
7
+ InstalledFiles
8
+ lib/bundler/man
9
+ pkg
10
+ rdoc
11
+ spec/reports
12
+ tmp
13
+
14
+ # YARD artifacts
15
+ .yardoc
16
+ _yardoc
17
+ doc/
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - 2.0.0
4
+
5
+ script: "bundle exec rspec spec/"
6
+
7
+ notifications:
8
+ email:
9
+ - simlegate@163.com
10
+
11
+ branches:
12
+ only:
13
+ - master
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kstarv.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,7 @@
1
+ guard :rspec do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { 'spec' }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ watch(%r{^spec/factories/(.+)_factory\.rb$}){ "factories" }
6
+ end
7
+
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 都是牛奶装什么特仑苏丶
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # Kstarv
2
+
3
+ Wrap a Ruby object by parsing file including key-values
4
+
5
+ [![Build Status](https://travis-ci.org/simlegate/kstarv.png?branch=master)](https://travis-ci.org/simlegate/kstarv)
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'kstarv'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install kstarv
19
+
20
+ ## Example text file
21
+ centos network config file
22
+ ```ruby
23
+ DEVICE=eth0
24
+ HWADDR=00:1E:67:24:E8:2D
25
+ TYPE=Ethernet
26
+ UUID=8dc70db4-9c80-4757-807b-6419d864f74d
27
+ ONBOOT=yes
28
+ NM_CONTROLLED=no
29
+ BOOTPROTO=static
30
+ IPADDR=192.168.0.201
31
+ NETMASK=255.255.255.0
32
+ GATEWAY=192.168.0.1
33
+ BORADCAST=192.168.0.255
34
+ ```
35
+ ## Usage
36
+ set decollator spliting key and vaule
37
+ ```ruby
38
+ # decollator is '=' by default
39
+ @kv = Kstarv.from(/path/to/config)
40
+ # set decollator to '*'
41
+ @kv = Kstarv.from(/path/to/config, '*')
42
+ ```
43
+
44
+ set key case
45
+ ```ruby
46
+ # 'upcase' or 'downcase'
47
+ @kv.case = 'upcase'
48
+ @kv.write
49
+ ```
50
+ read value by key
51
+ ```ruby
52
+ @kv.device # => eth0
53
+ @kv.ipaddr # => 192.168.0.201
54
+ ....
55
+ ```
56
+
57
+ write value
58
+ ```ruby
59
+ @kv.ipaddr # => 127.0.0.1
60
+ @kv.write
61
+ # output:
62
+ # ......
63
+ # BOOTPROTO=static
64
+ # IPADDR=127.0.0.1 # => changed
65
+ # NETMASK=255.255.255.0
66
+ # GATEWAY=192.168.0.1
67
+ # BORADCAST=192.168.0.255
68
+ # ....
69
+ ```
70
+ set key case
71
+ ```ruby
72
+ # you can set case of key
73
+ # downcase and @case is true by default
74
+ @kv.case = false
75
+ @kv.write
76
+ # output:
77
+ # ......
78
+ # bootproto=static
79
+ # ipaddr=192.168.0.201
80
+ # netmask=255.255.255.0
81
+ # gatewaY=192.168.0.1
82
+ # boradcast=192.168.0.255
83
+ # ....
84
+ ```
85
+
86
+ ## Contributing
87
+
88
+ 1. Fork it
89
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
90
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
91
+ 4. Push to the branch (`git push origin my-new-feature`)
92
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/kstarv.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kstarv/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kstarv"
8
+ spec.version = Kstarv::VERSION
9
+ spec.authors = ["都是牛奶装什么特仑苏丶"]
10
+ spec.email = ["simlegate@163.com"]
11
+ spec.description = %q{Wrap a Ruby object by parsing text file including key-values}
12
+ spec.summary = %q{Wrap a Ruby object by parsing text file including key-values}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency 'guard-rspec'
24
+ spec.add_development_dependency 'rspec'
25
+ end
data/lib/kstarv/k_v.rb ADDED
@@ -0,0 +1,88 @@
1
+ # encoding: utf-8
2
+ module Kstarv
3
+ class KV
4
+ # set key downcase or upcase
5
+ # @kv.case = true
6
+ # downcase and @case is true by default
7
+ # @kv.write
8
+ attr_accessor :case
9
+
10
+ def initialize file, join='='
11
+ @case ||= true
12
+ @file = file
13
+ @join = join
14
+ @instance_vars = []
15
+ load_attr
16
+ end
17
+
18
+ # @net_work_config.ipaddr = '127.0.0.1'
19
+ # @net_work_config.write
20
+ def write
21
+ kvs = @instance_vars.map do | var |
22
+ # TODO var upcase or downcase
23
+ # 调用case method
24
+ "#{convert_case(var)}=#{instance_var_get(var)}"
25
+ end
26
+ # kvs => [ "DEVICE=eth0", "HWADDR=00:1E:67:24:E8:2D", "TYPE=Ethernet" ...]
27
+
28
+ # r+是覆盖写,w+是清除后写,a+是追加写
29
+ File.open(@file, 'w+') do |f|
30
+ kvs.map do |kv|
31
+ f.write(kv)
32
+ f.puts
33
+ end
34
+ end
35
+ end
36
+
37
+
38
+ def load_attr
39
+ File.open(@file) do |f|
40
+ f.each do |line|
41
+ # remove blank line
42
+ # remove beginning and end space
43
+ create_attr(line.strip.split(@join)) unless line.gsub("\n",'').length == 0
44
+ end
45
+ end
46
+ end
47
+
48
+ def method_missing name, *arg
49
+ # name: _ipaddr=_
50
+ # if name =~ /[\w]+=$/
51
+ v = arg.first ? arg.first : ''
52
+ create_attr [name.to_s.split('=')[0], v]
53
+ end
54
+
55
+ # TODO
56
+ def to_s
57
+ 'convert kv to str'
58
+ end
59
+
60
+ private
61
+ # ["IPADDR", "192.168.0.201\n"]
62
+ def create_attr kv
63
+ save_instance_var kv[0].downcase.to_sym
64
+ singleton_class.class_eval { attr_accessor kv[0].downcase}
65
+ send("#{kv[0].downcase}=", kv[1].strip)
66
+ end
67
+
68
+ def save_instance_var var
69
+ @instance_vars << var
70
+ end
71
+
72
+ # output: @var
73
+ def prefix_at var
74
+ "@#{var}"
75
+ end
76
+
77
+ # var: symbol
78
+ def instance_var_get var
79
+ instance_variable_get(prefix_at(var))
80
+ end
81
+
82
+ # Ipaddr => ipaddr
83
+ # ipaddr => IPADDR
84
+ def convert_case key
85
+ @case ? key.downcase : key.upcase
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,3 @@
1
+ module Kstarv
2
+ VERSION = "0.0.1"
3
+ end
data/lib/kstarv.rb ADDED
@@ -0,0 +1,15 @@
1
+ require "kstarv/version"
2
+ require "kstarv/k_v"
3
+
4
+ module Kstarv
5
+ class NoSuchFile < StandardError
6
+ def message
7
+ # TODO should say file path to message
8
+ "can not find _such file_"
9
+ end
10
+ end
11
+
12
+ def self.from file,join='='
13
+ File.exists?(file) ? KV.new(file, join) : (raise NoSuchFile)
14
+ end
15
+ end
data/spec/fixtures/kv ADDED
@@ -0,0 +1,11 @@
1
+ DEVICE=eth0
2
+ HWADDR=00:1E:67:24:E8:2D
3
+ TYPE=Ethernet
4
+ UUID=8dc70db4-9c80-4757-807b-6419d864f74d
5
+ ONBOOT=yes
6
+ NM_CONTROLLED=no
7
+ BOOTPROTO=static
8
+ IPADDR=192.168.0.201
9
+ NETMASK=255.255.255.0
10
+ GATEWAY=192.168.0.1
11
+ BORADCAST=192.168.0.255
@@ -0,0 +1,11 @@
1
+ DEVICE=eth0
2
+ HWADDR=00:1E:67:24:E8:2D
3
+ TYPE=Ethernet
4
+ UUID=8dc70db4-9c80-4757-807b-6419d864f74d
5
+ ONBOOT=yes
6
+ NM_CONTROLLED=no
7
+ BOOTPROTO=static
8
+ IPADDR=192.168.0.201
9
+ NETMASK=255.255.255.0
10
+ GATEWAY=192.168.0.1
11
+ BORADCAST=192.168.0.255
@@ -0,0 +1,11 @@
1
+ DEVICE=eth0
2
+ HWADDR=00:1E:67:24:E8:2D
3
+ TYPE=Ethernet
4
+ UUID=8dc70db4-9c80-4757-807b-6419d864f74d
5
+ ONBOOT=yes
6
+ NM_CONTROLLED=no
7
+ BOOTPROTO=static
8
+ IPADDR=192.168.0.201
9
+ NETMASK=255.255.255.0
10
+ GATEWAY=192.168.0.1
11
+ BORADCAST=192.168.0.255
@@ -0,0 +1,11 @@
1
+ DEVICE=eth0
2
+ HWADDR=00:1E:67:24:E8:2D
3
+ TYPE=Ethernet
4
+ UUID=8dc70db4-9c80-4757-807b-6419d864f74d
5
+ ONBOOT=yes
6
+ NM_CONTROLLED=no
7
+ BOOTPROTO=static
8
+ IPADDR=192.168.0.201
9
+ NETMASK=255.255.255.0
10
+ GATEWAY=192.168.0.1
11
+ BORADCAST=192.168.0.255
@@ -0,0 +1,75 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ describe '#KV' do
4
+ before :each do
5
+ @file = File.expand_path('../../fixtures/kv_1', __FILE__)
6
+ @kv = Kstarv::KV.new @file
7
+ end
8
+
9
+ context 'dynamic create attr' do
10
+ it 'should not including other attr ' do
11
+ @kv.instance_variable_get(:@instance_vars).should == [:device, :hwaddr, :type, :uuid, :onboot, :nm_controlled, :bootproto, :ipaddr, :netmask, :gateway, :boradcast]
12
+ end
13
+
14
+ context 'attr do not exists' do
15
+ after :each do
16
+ FileUtils.copy File.expand_path('../../fixtures/kv', __FILE__),@file
17
+ end
18
+
19
+ it 'should be empty string' do
20
+ @kv.chinesename.should == ''
21
+ end
22
+
23
+ it 'should use _method_missing_ method to create attr_accessor' do
24
+ expect {@kv.chinesename}.not_to raise_error
25
+ end
26
+
27
+ it 'should save config changed' do
28
+ @kv.chinesename = 'simlegate'
29
+ @kv.write
30
+ Kstarv::KV.new(@file).chinesename.should == 'simlegate'
31
+ end
32
+ end
33
+ end
34
+
35
+ context 'instance file to object' do
36
+ it '@instance_vars should be instance of Array' do
37
+ @kv.instance_variable_get(:@instance_vars).should be_instance_of Array
38
+ end
39
+
40
+ it 'should have 11 attr in fixtures file' do
41
+ @kv.instance_variable_get(:@instance_vars).count.should == 11
42
+ end
43
+
44
+ it 'element of @instance_vars should be instance of Symbol' do
45
+ @kv.instance_variable_get(:@instance_vars).map {|var| var.should be_instance_of Symbol}
46
+ end
47
+ end
48
+
49
+ context 'read config file including blank line' do
50
+ before :each do
51
+ @blank_line_file = File.expand_path('../../fixtures/kv_blank_line', __FILE__)
52
+ end
53
+
54
+ it 'should not raise error when instaning' do
55
+ expect { Kstarv::KV.new @blank_line_file }.not_to raise_error
56
+ end
57
+
58
+ it 'should remove the space at the first of line' do
59
+ file = File.expand_path('../../fixtures/kv_space', __FILE__)
60
+ expect { Kstarv::KV.new file }.not_to raise_error
61
+ end
62
+ end
63
+
64
+ context 'set key case' do
65
+ it '____' do
66
+ pending('wait me....')
67
+ end
68
+ end
69
+
70
+ context 'private method' do
71
+ it '____' do
72
+ pending('wait me....')
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ describe '#Kstarv' do
4
+ before :each do
5
+ @file = File.expand_path('../fixtures/kv_1', __FILE__)
6
+ end
7
+
8
+ it 'should get instance of KV' do
9
+ Kstarv.from(@file,'=').should be_instance_of Kstarv::KV
10
+ end
11
+
12
+ it 'second param is optional of _from_ method ' do
13
+ Kstarv.from(@file).should be_instance_of Kstarv::KV
14
+ end
15
+
16
+ it 'should raise error when file do not exists' do
17
+ @file = File.expand_path('../../fixtures/kv', __FILE__)
18
+ expect {Kstarv.from(@file,'=')}.to raise_error(Kstarv::NoSuchFile)
19
+ end
20
+ end
@@ -0,0 +1,2 @@
1
+ require 'rspec'
2
+ require_relative "../lib/kstarv"
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kstarv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - 都是牛奶装什么特仑苏丶
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Wrap a Ruby object by parsing text file including key-values
70
+ email:
71
+ - simlegate@163.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .travis.yml
78
+ - Gemfile
79
+ - Guardfile
80
+ - LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - kstarv.gemspec
84
+ - lib/kstarv.rb
85
+ - lib/kstarv/k_v.rb
86
+ - lib/kstarv/version.rb
87
+ - spec/fixtures/kv
88
+ - spec/fixtures/kv_1
89
+ - spec/fixtures/kv_blank_line
90
+ - spec/fixtures/kv_space
91
+ - spec/kstarv/k_v_spec.rb
92
+ - spec/kstarv_spec.rb
93
+ - spec/spec_helper.rb
94
+ homepage: ''
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.0.6
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Wrap a Ruby object by parsing text file including key-values
118
+ test_files:
119
+ - spec/fixtures/kv
120
+ - spec/fixtures/kv_1
121
+ - spec/fixtures/kv_blank_line
122
+ - spec/fixtures/kv_space
123
+ - spec/kstarv/k_v_spec.rb
124
+ - spec/kstarv_spec.rb
125
+ - spec/spec_helper.rb