inventoryfile 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aeb5b7814e848ad36c358a688feffd4c4ec9f250
4
- data.tar.gz: 110dee3bda192c09b6c516cb981b2be78b21eeb9
3
+ metadata.gz: fe09538b8337b4a27cc85f206fc4e038333f38fb
4
+ data.tar.gz: f85a08a8c290f534a4b257ffd9d2048010893025
5
5
  SHA512:
6
- metadata.gz: 20b33e1d5d2524951232b03d2844d38eedd7c5fd04e3b73c627b4d621234d33478c227e04d0a1b2054c6ede3044b59abe90acee9432617cfc6cffd9c62e016a9
7
- data.tar.gz: e9fac1a8a238fed9953e50b19f919615176923730c21238a822ad00c757d5ccda9bd649236dab5af215a3fcfb4c16886b90832a138e7fa09067127efce3ae85c
6
+ metadata.gz: 081d45124d23d3aad54a67caf2a162b6a8695bc3b39b680173bebe3c76701c7d205e2ebfc84f5c5e6c1f977be8e9e651213b5d1827b5633b6f76c9210be57577
7
+ data.tar.gz: 414b60548a390e422590608d8a2508da13380aa9d6a3c7b86e275d5eb241df777d5753c8d85ca113f8d6d6a380674704e911d8617a3c128e85f1a113d66b5178
data/README.md CHANGED
@@ -19,16 +19,27 @@ Or install it yourself as:
19
19
 
20
20
  ## Usage
21
21
 
22
+ * get items on specified section
23
+
24
+ ```
25
+ require 'inventoryfile'
26
+
27
+ parser = Inventoryfile::Parser.new("PATH_TO_FILE")
28
+ parser.items("SECTION_NAME")
29
+ ```
30
+
31
+ * get sections
32
+
22
33
  ```
23
34
  require 'inventoryfile'
24
35
 
25
36
  parser = Inventoryfile::Parser.new("PATH_TO_FILE")
26
- parser.parse("SECTION_NAME")
37
+ parser.sections
27
38
  ```
28
39
 
29
40
  ## Contributing
30
41
 
31
- 1. Fork it ( https://github.com/[my-github-username]/inventoryfile/fork )
42
+ 1. Fork it ( https://github.com/hiroakis/inventoryfile/fork )
32
43
  2. Create your feature branch (`git checkout -b my-new-feature`)
33
44
  3. Commit your changes (`git commit -am 'Add some feature'`)
34
45
  4. Push to the branch (`git push origin my-new-feature`)
@@ -1,40 +1,64 @@
1
1
  module Inventoryfile
2
2
  class Parser
3
- def initialize(file)
4
- @file = file
3
+
4
+ IGNORE_CASE = /(^\s*#)|(^\s*$)/
5
+ SECTION_CASE = /\s*\[.*\]/
6
+
7
+ def initialize(filename)
8
+ f = open(filename)
9
+ @file = f.read.split("\n")
10
+ f.close
5
11
  end
6
12
 
13
+ # duplicated
7
14
  def parse(section)
15
+ items(section)
16
+ end
17
+
18
+ def items(section)
19
+ results = []
8
20
 
9
- items = []
10
-
11
- ignore_case = /(^\s*#)|(^\s*$)/
12
21
  find_case = /\s*\[#{section}\]/
13
22
 
14
23
  find = false
15
- f = open(@file)
16
- f.each do |i|
24
+ @file.each do |i|
17
25
  line = i.chomp
18
26
 
19
27
  case line
20
- when ignore_case
28
+ when IGNORE_CASE
21
29
  next
22
30
  when find_case
23
31
  find = true
24
32
  next
25
33
  else
26
- if line =~ /\s*\[.*\]/
34
+ if line =~ SECTION_CASE
27
35
  find = false
28
36
  end
29
37
  next if find == false
30
38
 
31
39
  line.gsub!(/#.*/, "")
32
40
  line.strip!
33
- items << line
41
+ results << line
34
42
  end
35
43
  end
36
- f.close
37
- items
44
+ results
45
+ end
46
+
47
+ def sections
48
+ results = []
49
+
50
+ @file.each do |i|
51
+ line = i.chomp
52
+ case line
53
+ when IGNORE_CASE
54
+ next
55
+ when SECTION_CASE
56
+ line.gsub!(/#.*|\[|\]/, "")
57
+ line.strip!
58
+ results << line
59
+ end
60
+ end
61
+ results
38
62
  end
39
63
  end
40
64
  end
@@ -1,3 +1,3 @@
1
1
  module Inventoryfile
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inventoryfile
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroaki Sano
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-08-16 00:00:00.000000000 Z
11
+ date: 2015-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler