formaticon 0.1.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.
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ # v0.1.0
2
+ * Initial release
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,25 @@
1
+ # Contributing
2
+
3
+ ## Branches
4
+
5
+ ### `master` branch
6
+
7
+ The master branch is the current stable released version.
8
+
9
+ ### `develop` branch
10
+
11
+ The develop branch is the current edge of development.
12
+
13
+ ## Pull requests
14
+
15
+ * https://github.com/chrisroberts/formaticon/pulls
16
+
17
+ Please base all pull requests of the `develop` branch. Merges to
18
+ `master` only occur through the `develop` branch. Pull requests
19
+ based on `master` will likely be cherry picked.
20
+
21
+ ## Issues
22
+
23
+ Need to report an issue? Use the github issues:
24
+
25
+ * https://github.com/chrisroberts/formaticon/issues
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,16 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ formaticon (0.1.0)
5
+ xml-simple
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ xml-simple (1.1.4)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ formaticon!
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2014 Chris Roberts
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ ## Formaticon
2
+
3
+ Stop re-inventing the configuration file wheel.
4
+
5
+ ### Ini
6
+
7
+ * `Formaticon::Helpers.ruby_to_ini(hash)`
8
+
9
+ Original ruby:
10
+
11
+ ```ruby
12
+ {
13
+ 'default' => {
14
+ 'fubar' => true,
15
+ 'foobar' => false,
16
+ 'fauxbar' => OHAI,
17
+ 'foobars' => 3
18
+ 'fubars' => 'Lots of them'
19
+ },
20
+ 'custom' => {
21
+ 'bing' => 'bang'
22
+ }
23
+ }
24
+ ```
25
+
26
+ Formatted output:
27
+
28
+ ```
29
+ [default]
30
+ fubar = yes
31
+ foobar = no
32
+ fauxbar = OHAI
33
+ foobars = 3
34
+ fubars = "Lots of them"
35
+ [custom]
36
+ bing = "bang"
37
+ ```
38
+
39
+ ### Xml
40
+
41
+ * `Formaticon::Helpers.ruby_to_xml(hash)`
42
+
43
+ Original ruby:
44
+
45
+ ```ruby
46
+ {
47
+ :fubar => {
48
+ :foobar => {
49
+ :@name => 'Fubar',
50
+ :@class => 'So Classy'
51
+ :fauxbar => 'BANG'
52
+ }
53
+ },
54
+ :bing => [
55
+ {
56
+ :@name => 'boom'
57
+ },
58
+ {
59
+ :@name => 'bam'
60
+ }
61
+ ]
62
+ }
63
+ ```
64
+
65
+ Formatted output:
66
+
67
+ ```
68
+ <fubar>
69
+ <foobar name="Fubar" class="So Classy">
70
+ <fauxbar>BANG</fauxbar>
71
+ </foobar>
72
+ <bing name="boom"/>
73
+ <bing name="bam"/>
74
+ </fubar>
75
+ ```
76
+
77
+ ### Erlang
78
+
79
+ * `Formaticon::Helpers.ruby_to_erl(hash)`
80
+
81
+ Original ruby:
82
+
83
+ ```ruby
84
+
85
+ ```
86
+
87
+ # Info
88
+
89
+ * Repository: https://github.com/chrisroberts/formaticon
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__)) + '/lib/'
2
+ require 'formaticon/version'
3
+ Gem::Specification.new do |s|
4
+ s.name = 'formaticon'
5
+ s.version = Formaticon::VERSION.version
6
+ s.summary = 'Configuration format helper'
7
+ s.author = 'Chris Roberts'
8
+ s.email = 'code@chrisroberts.org'
9
+ s.homepage = 'http://github.com/chrisroberts/formaticon'
10
+ s.description = 'Helpers for generating configurations'
11
+ s.require_path = 'lib'
12
+ s.add_dependency 'xml-simple'
13
+ s.files = Dir['**/*']
14
+ end
@@ -0,0 +1,19 @@
1
+ require 'formaticon'
2
+
3
+ module Formaticon
4
+ class BaseConverter
5
+ attr_reader :base
6
+
7
+ def initialize(obj)
8
+ @base = obj
9
+ end
10
+
11
+ def from_ruby
12
+ raise NotImplementedError.new
13
+ end
14
+
15
+ def to_ruby
16
+ raise NotImplementedError.new
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,66 @@
1
+ require 'formaticon'
2
+
3
+ module Formaticon
4
+ module Converters
5
+
6
+ module Erlang
7
+
8
+ class Converter < BaseConverter
9
+
10
+ def to_ruby
11
+ Erlang2Ruby.new(base).ruby
12
+ end
13
+
14
+ def from_ruby
15
+ convert_type(base)
16
+ end
17
+
18
+ private
19
+
20
+ def convert_type(obj)
21
+ case obj
22
+ when Hash
23
+ when Array
24
+ else
25
+ end
26
+ end
27
+
28
+ class Erlang2Ruby
29
+
30
+ attr_reader :erl_string
31
+
32
+ def initialize(erl_string)
33
+ @erl_string = erl_string
34
+ end
35
+
36
+ def ruby
37
+ data = erl_string.dup
38
+ data.scan(/('?[A-Za-z0-9_\-@\.]+'?)/).flatten.sort_by(&:length).reverse.each do |string|
39
+ unless(string.include?("'"))
40
+ data.gsub!(/([^A-Za-z0-9_\-@\.])#{Regexp.escape(string)}/, "\\1'#{string}'")
41
+ end
42
+ end
43
+ data.gsub!('{', 'Hash[')
44
+ data.gsub!('}', ']')
45
+ self.instance_eval(data.gsub(/\s/, ''))
46
+ end
47
+
48
+ end
49
+
50
+ private
51
+
52
+ def method_missing(sym, *args)
53
+ sym.to_s
54
+ end
55
+
56
+ def convert_to_ruby
57
+ conv_str = string
58
+ Chef::Log.debug "Manipulated erl string: #{conv_str}"
59
+ conv_str
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,41 @@
1
+ require 'formaticon'
2
+
3
+ module Formaticon
4
+ module Converters
5
+
6
+ module Ini
7
+
8
+ class Converter < BaseConverter
9
+
10
+ def from_ruby
11
+ base.map do |section, settings|
12
+ next if settings.nil?
13
+ content = settings.map do |key, value|
14
+ next if value.nil?
15
+ "#{key} = #{parse_value(value)}"
16
+ end.compact
17
+ content.unshift("[#{section}]").push('').join("\n")
18
+ end.compact.join("\n")
19
+ end
20
+
21
+ private
22
+
23
+ def parse_value(val)
24
+ case val
25
+ when Numeric
26
+ val
27
+ when TrueClass
28
+ 'On'
29
+ when FalseClass
30
+ 'off'
31
+ else
32
+ val.to_s.upcase == val ? val : "'#{val}'"
33
+ end
34
+ end
35
+ end
36
+
37
+ end
38
+
39
+
40
+ end
41
+ end
@@ -0,0 +1,25 @@
1
+ require 'formaticon'
2
+
3
+ module Formaticon
4
+ module Converters
5
+
6
+ module Xml
7
+
8
+ class Converter < BaseConverter
9
+
10
+ def initialize(*args)
11
+ super
12
+ unless(defined?(XmlSimple))
13
+ require 'xmlsimple'
14
+ end
15
+ end
16
+
17
+ def from_ruby
18
+ XmlSimple.xml_out(base, 'AttrPrefix' => true, 'KeepRoot' => true)
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ require 'formaticon'
2
+
3
+ module Formaticon
4
+ module Converters
5
+
6
+ autoload :Ini, 'formaticon/converters/ini'
7
+ autoload :Erlang, 'formaticon/converters/erlang'
8
+ autoload :Xml, 'formaticon/converters/xml'
9
+
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ require 'formaticon'
2
+
3
+ module Formaticon
4
+ module Helpers
5
+
6
+ def erl_to_ruby(string)
7
+ Erlang::Converter.new(string).to_ruby
8
+ end
9
+
10
+ def ruby_to_erl(obj)
11
+ Erlang::Converter.new(obj).from_ruby
12
+ end
13
+
14
+ def ruby_to_xml(obj)
15
+ Xml::Converter.new(obj).from_ruby
16
+ end
17
+
18
+ def ruby_to_ini(obj)
19
+ Ini::Converter.new(obj).from_ruby
20
+ end
21
+
22
+ end
23
+
24
+ extend Helpers
25
+ end
@@ -0,0 +1,4 @@
1
+ module Formaticon
2
+ # Current library version
3
+ VERSION = Gem::Version.new('0.1.0')
4
+ end
data/lib/formaticon.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'formaticon/version'
2
+
3
+ module Formaticon
4
+
5
+ autoload :BaseConverter, 'formaticon/base_converter'
6
+ autoload :Converters, 'formaticon/converters'
7
+ autoload :Helpers, 'formaticon/helpers'
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: formaticon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Roberts
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-08-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: xml-simple
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Helpers for generating configurations
31
+ email: code@chrisroberts.org
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - lib/formaticon.rb
37
+ - lib/formaticon/converters.rb
38
+ - lib/formaticon/converters/xml.rb
39
+ - lib/formaticon/converters/ini.rb
40
+ - lib/formaticon/converters/erlang.rb
41
+ - lib/formaticon/version.rb
42
+ - lib/formaticon/helpers.rb
43
+ - lib/formaticon/base_converter.rb
44
+ - formaticon.gemspec
45
+ - Gemfile
46
+ - README.md
47
+ - LICENSE
48
+ - CHANGELOG.md
49
+ - CONTRIBUTING.md
50
+ - Gemfile.lock
51
+ homepage: http://github.com/chrisroberts/formaticon
52
+ licenses: []
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 1.8.24
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Configuration format helper
75
+ test_files: []
76
+ has_rdoc: