figtree 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 63e43d9d640143445c0baa5ce76f59f481866607
4
- data.tar.gz: b4c3ccea33953ee1838e753143359692b4ffed8a
3
+ metadata.gz: 011a21ea2c8dfbe34b870ef195349235aa1effa3
4
+ data.tar.gz: 0835b757e33b0b33b30640fe95de3deaedae8f9d
5
5
  SHA512:
6
- metadata.gz: 2f32e3781ea27fd6dbe637585e2d5da867f0f7a3dfe7650c5a10321ead9637760f083ef58aeece560d2146d7ca4aeb8093b061df3fc3374e9030b97766e2d91a
7
- data.tar.gz: 91d3553cf4acbc18dfd38bdf687583cdfd77dc7ced185618c52d1ac04d87436059c63f792959a5a759a7468d3727d55f78d6344356c4fe6159e0d6c2c2c015d2
6
+ metadata.gz: 38e21385476ad11e06a20553893c11271086056959d4613022b123bc64ecdf44653f79824ff611305c89655a75e3cbf1d1ac83b3017fb962823db7c9ded00a80
7
+ data.tar.gz: 86199767fdb013c40f4e9c769e394e76c98234b5e32fec12800507fcbee22ab6fd2e0e093bdfdb774ea78866274140fc31ea556021c2ba29118e7f2122c64e89
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- figtree (0.0.1)
4
+ figtree (1.0.0)
5
5
  parslet (~> 1.7)
6
6
  wannabe_bool (~> 0.2)
7
7
 
@@ -46,7 +46,7 @@ PLATFORMS
46
46
 
47
47
  DEPENDENCIES
48
48
  figtree!
49
- pry
49
+ pry (~> 0.10)
50
50
  rspec (~> 3.0, >= 3.0.0)
51
51
  simplecov (~> 0.10)
52
52
 
data/README.md CHANGED
@@ -1,18 +1,32 @@
1
- # Figtree 🌳
1
+ %%%,%%%%%%%
2
+ ,'%% \\-*%%%%%%%
3
+ ;%%%%%*% _%%%%"
4
+ ,%%% \(_.*%%%%.
5
+ % *%%, ,%%%%*( '
6
+ %^ ,*%%% )\|,%%*%,_
7
+ *% \/ #).-"*%%*
8
+ _.) ,/ *%,
9
+ _________/)#(_____________
10
+ #### art by [b'ger](http://ascii.co.uk/art/tree)
11
+
12
+ # Figtree
2
13
  ## about
3
14
  A parser and transformer for loading `.ini` files into Ruby dot notation accessible objects. `.ini` is not a standardized format. But the parser and transformer are easy to extend, unlike regex. :) And it's at 100% LOC coverage.
4
15
 
5
16
  If the `.ini` file is invalid, an error will be raised, with the line and char position of the error. If you extend this gem to have more rules, and one of those rules fails to transform, you will have an error raised.
6
17
 
18
+ ## disambiguation
19
+ Looking for the graphical viewer of phyllogenic trees? You want this other [Figtree](http://tree.bio.ed.ac.uk/software/figtree/).
20
+
7
21
  ## performance
8
- A typical `.ini` file takes slightly less than 0.02s to be parsed, transformed, and loaded.
22
+ A typical `.ini` file takes slightly less than 0.02s to be parsed, transformed, and loaded. Currently, the whole `.ini` file is read into memory at once. The assumption being these files should not typically be too big. But future minor versions might move to line by line ingestion.
9
23
 
10
24
  ## installation
11
25
  `gem install figtree`
12
26
 
13
27
  ## usage
14
28
  require 'figtree'
15
- config = Figtree.load_config('spec/support/settings.conf')
29
+ config = Figtree::IniConfig.new('spec/support/settings.conf')
16
30
  config.common.basic_size_limit
17
31
  => 26214400
18
32
  # also good
@@ -22,7 +36,7 @@ A typical `.ini` file takes slightly less than 0.02s to be parsed, transformed,
22
36
  config.common[:paid_users_size_limit]
23
37
  => 2147483648
24
38
  # and overrides? we got overrides
25
- overridden_config = Figtree.load_config('spec/support/settings.conf', [:production])
39
+ overridden_config = Figtree::IniConfig.new('spec/support/settings.conf', :production)
26
40
  config.ftp.path
27
41
  => "/tmp/"
28
42
  overridden_config.ftp.path
@@ -36,7 +50,6 @@ A typical `.ini` file takes slightly less than 0.02s to be parsed, transformed,
36
50
  `rspec spec/`
37
51
 
38
52
  #### TODO
53
+ - move way from needing to parse whole file at once? (move to `IO.foreach` ?)
39
54
  - give char/line position of transformer failures
40
- - change method signature from Module.class_method to just IniConfig.new(IOObject) ?
41
- - change override to be single symbol rather than array (do we ever need multiples?)
42
55
  - refactor marked TODO listings in files (mostly refactoring to generic in Transformer)
@@ -1,39 +1,3 @@
1
1
  require 'figtree/parser'
2
2
  require 'figtree/transformer'
3
3
  require 'figtree/ini_config'
4
-
5
- module Figtree
6
- def self.load_config(file_path, overrides=[])
7
- parsed_subgroups = figgy_transform(
8
- figgy_parse(
9
- File.read(file_path)
10
- ),
11
- overrides
12
- )
13
- IniConfig.new(
14
- parsed_subgroups.reduce({}, :merge!)
15
- )
16
- end
17
-
18
- private
19
- def self.figgy_parse(str)
20
- Parser.new.parse(str)
21
- rescue Parslet::ParseFailed => failure
22
- STDERR.puts "\nInvalid ini file.\n" +
23
- "Error: #{failure.cause.ascii_tree}" +
24
- "Please correct the file and retry."
25
- raise
26
- end
27
-
28
- def self.figgy_transform(tree, overrides = [])
29
- Transformer.new.apply(tree, overrides: overrides)
30
- rescue => e
31
- STDERR.puts "\nInvalid transformation rule.\n" +
32
- "Error: #{e}" +
33
- "Please correct your transformer rule and retry."
34
- raise TransformFailed
35
- end
36
-
37
- class TransformFailed < Exception
38
- end
39
- end
@@ -1,7 +1,46 @@
1
1
  require 'ostruct'
2
2
  module Figtree
3
3
  class IniConfig < OpenStruct
4
+ def initialize(ini, override = :none)
5
+ # cheat to allow a parsed hash in
6
+ if ini.is_a?(Hash)
7
+ parsed_subgroups = ini
8
+ else
9
+ parsed_subgroups = figgy_transform(
10
+ figgy_parse(
11
+ File.read(ini)
12
+ ),
13
+ override
14
+ ).reduce({}, :merge!)
15
+ end
16
+ super(
17
+ parsed_subgroups
18
+ )
19
+ end
20
+
21
+ private
22
+ def figgy_parse(str)
23
+ Parser.new.parse(str)
24
+ rescue Parslet::ParseFailed => failure
25
+ STDERR.puts "\nInvalid ini file.\n" +
26
+ "Error: #{failure.cause.ascii_tree}" +
27
+ "Please correct the file and retry."
28
+ raise
29
+ end
30
+
31
+ def figgy_transform(tree, override)
32
+ Transformer.new.apply(tree, override: override)
33
+ rescue => e
34
+ STDERR.puts "\nInvalid transformation rule.\n" +
35
+ "Error: #{e}" +
36
+ "Please correct your transformer rule and retry."
37
+ raise TransformFailed
38
+ end
4
39
  end
40
+
5
41
  class Subgroup < OpenStruct
6
42
  end
43
+
44
+ class TransformFailed < Exception
45
+ end
7
46
  end
@@ -53,7 +53,7 @@ module Figtree
53
53
  :optional_key => subtree(:overriding_key),
54
54
  :file_path => subtree(:new_file_path),
55
55
  ) do
56
- if overrides.include?(overriding_key[:snake_case_key].to_sym)
56
+ if override.to_sym == overriding_key[:snake_case_key].to_sym
57
57
  {
58
58
  overridden_key[:snake_case_key] => String(new_file_path)
59
59
  }
@@ -1,3 +1,3 @@
1
1
  module Figtree
2
- VERSION = "0.0.2"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -45,7 +45,7 @@ module Figtree
45
45
  Parser.new.parse("[http]\npath = /srv/\npath<production> = /srv/var/tmp/\n")
46
46
  end
47
47
  it 'can apply an override' do
48
- expect(Transformer.new.apply(override_tree, overrides: [:production])).to eq(
48
+ expect(Transformer.new.apply(override_tree, override: :production)).to eq(
49
49
  [
50
50
  {
51
51
  http: Subgroup.new(path: '/srv/var/tmp/')
@@ -59,21 +59,21 @@ describe Figtree do
59
59
  end
60
60
 
61
61
  it 'can parse a group and provide dot notation access' do
62
- expect(Figtree.load_config(settings_path).common).to eq(common)
62
+ expect(Figtree::IniConfig.new(settings_path).common).to eq(common)
63
63
  end
64
64
  it 'can parse the overrides correctly' do
65
- expect(Figtree.load_config(settings_path, [:itscript]).common).
65
+ expect(Figtree::IniConfig.new(settings_path, :itscript).common).
66
66
  to eq(common_with_override)
67
67
  end
68
68
  it 'can parse the whole Kebab without any misunderstandings' do
69
- expect(Figtree.load_config(settings_path)).to eq(the_whole_kebab)
69
+ expect(Figtree::IniConfig.new(settings_path)).to eq(the_whole_kebab)
70
70
  end
71
71
 
72
72
  context "performance" do
73
73
  it 'can parse the whole ini file quickly' do
74
74
  expect(
75
75
  Benchmark.realtime do
76
- Figtree.load_config(settings_path)
76
+ Figtree::IniConfig.new(settings_path)
77
77
  end
78
78
  ).to be < 0.014
79
79
  end
@@ -83,13 +83,13 @@ describe Figtree do
83
83
  let(:unparseable_config) { 'spec/support/unparseable_settings.conf' }
84
84
  let(:untransformable_config) { 'spec/support/untransformable_settings.conf' }
85
85
  it 'throws ParseFailed if unparseable' do
86
- expect { Figtree.load_config(unparseable_config) }.
86
+ expect { Figtree::IniConfig.new(unparseable_config) }.
87
87
  to raise_error(Parslet::ParseFailed)
88
88
  end
89
89
  it 'throws TransformFailed if untransformable' do
90
90
  allow_any_instance_of(String).to receive(:to_b).
91
91
  and_raise(StandardError)
92
- expect { Figtree.load_config(untransformable_config) }.
92
+ expect { Figtree::IniConfig.new(untransformable_config) }.
93
93
  to raise_error(Figtree::TransformFailed)
94
94
  end
95
95
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: figtree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Moore-Niemi