mixItRuby 0.0.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/.gitignore ADDED
@@ -0,0 +1,45 @@
1
+ ./log/*
2
+ log/*.log
3
+ log/development.log
4
+ .DS_Store
5
+ doc/api
6
+ doc/app
7
+ coverage
8
+ db/*.sqlite3
9
+ db/development_structure.sql
10
+ public/uploads
11
+ spec/dummy/log
12
+ spec/dummy/tmp
13
+ spec/dummy/public/system/images
14
+ spec/dummy/puplic/assets/*
15
+ spec/dummy/public/system/uploads/*
16
+ spec/dummy/public/system/*
17
+ spec/dummy/public/uploads
18
+ spec/dummy/log*
19
+ spec/dummy/tmp*
20
+ index/*
21
+ *.swp
22
+ *.swo
23
+ .idea/
24
+ .idea/**/*
25
+ nbproject/*
26
+ *.war
27
+ db/schema.rb
28
+ tmp/*
29
+ wMake.bat
30
+ etc/web.xml
31
+ build/
32
+ build/*
33
+ .bundle/
34
+ ~/
35
+ ~/*
36
+ *.knob
37
+ tmp
38
+ mysession.vi*
39
+ ruby-uuid
40
+ localhistory
41
+ test
42
+ prod
43
+ *.png
44
+ site/dev/images/galleries
45
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'http://rubygems.org'
2
+ group :test do
3
+ gem "mixItRuby" , path: "."
4
+ gem 'guard-rspec'
5
+ gem 'guard-cucumber'
6
+ gem 'guard'
7
+ gem 'rspec'
8
+ gem 'cucumber'
9
+ gem 'aruba'
10
+ end
11
+
data/Gemfile.lock ADDED
@@ -0,0 +1,84 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mixItRuby (0.0.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ aruba (0.5.4)
10
+ childprocess (>= 0.3.6)
11
+ cucumber (>= 1.1.1)
12
+ rspec-expectations (>= 2.7.0)
13
+ builder (3.2.2)
14
+ celluloid (0.15.2)
15
+ timers (~> 1.1.0)
16
+ celluloid-io (0.15.0)
17
+ celluloid (>= 0.15.0)
18
+ nio4r (>= 0.5.0)
19
+ childprocess (0.5.3)
20
+ ffi (~> 1.0, >= 1.0.11)
21
+ coderay (1.1.0)
22
+ cucumber (1.3.15)
23
+ builder (>= 2.1.2)
24
+ diff-lcs (>= 1.1.3)
25
+ gherkin (~> 2.12)
26
+ multi_json (>= 1.7.5, < 2.0)
27
+ multi_test (>= 0.1.1)
28
+ diff-lcs (1.2.5)
29
+ ffi (1.9.3)
30
+ formatador (0.2.4)
31
+ gherkin (2.12.2)
32
+ multi_json (~> 1.3)
33
+ guard (2.6.0)
34
+ formatador (>= 0.2.4)
35
+ listen (~> 2.7)
36
+ lumberjack (~> 1.0)
37
+ pry (>= 0.9.12)
38
+ thor (>= 0.18.1)
39
+ guard-cucumber (1.4.1)
40
+ cucumber (>= 1.2.0)
41
+ guard (>= 1.1.0)
42
+ guard-rspec (4.2.8)
43
+ guard (~> 2.1)
44
+ rspec (>= 2.14, < 4.0)
45
+ listen (2.7.1)
46
+ celluloid (>= 0.15.2)
47
+ celluloid-io (>= 0.15.0)
48
+ rb-fsevent (>= 0.9.3)
49
+ rb-inotify (>= 0.9)
50
+ lumberjack (1.0.5)
51
+ method_source (0.8.2)
52
+ multi_json (1.10.1)
53
+ multi_test (0.1.1)
54
+ nio4r (1.0.0)
55
+ pry (0.9.12.6)
56
+ coderay (~> 1.0)
57
+ method_source (~> 0.8)
58
+ slop (~> 3.4)
59
+ rb-fsevent (0.9.4)
60
+ rb-inotify (0.9.3)
61
+ ffi (>= 0.5.0)
62
+ rspec (2.14.1)
63
+ rspec-core (~> 2.14.0)
64
+ rspec-expectations (~> 2.14.0)
65
+ rspec-mocks (~> 2.14.0)
66
+ rspec-core (2.14.8)
67
+ rspec-expectations (2.14.5)
68
+ diff-lcs (>= 1.1.3, < 2.0)
69
+ rspec-mocks (2.14.6)
70
+ slop (3.5.0)
71
+ thor (0.19.1)
72
+ timers (1.1.0)
73
+
74
+ PLATFORMS
75
+ ruby
76
+
77
+ DEPENDENCIES
78
+ aruba
79
+ cucumber
80
+ guard
81
+ guard-cucumber
82
+ guard-rspec
83
+ mixItRuby!
84
+ rspec
data/Guardfile ADDED
@@ -0,0 +1,13 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+ notification :tmux, display_message: true
4
+ guard :rspec do
5
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
6
+ watch('spec/spec_helper.rb') { "spec" }
7
+ watch('bin/mixit') { "spec" }
8
+ watch(%r{^spec/.+_spec\.rb$})
9
+ watch(%r{^spec/spec_helper\.rb})
10
+ end
11
+ guard :cucumber do
12
+ watch(%r{^features/(.+)\.feature$}) { "bundle exec cucumber" }
13
+ end
data/bin/mixit ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+ require "mixer"
4
+ require "json"
5
+ options = {}
6
+ optparse = OptionParser.new do|opts|
7
+ opts.banner = "Usage: mixit [-o outputfile -i inputfile -t templatefile]"
8
+ opts.on( '-i', '--input FILE', 'json inputfile' ) do|p|
9
+ options[:inputfile] = p
10
+ end
11
+
12
+ opts.on( '-o', '--output FILE', 'mixed outputfile' ) do|p|
13
+ options[:outputfile] = p
14
+ end
15
+
16
+ opts.on( '-t', '--templates DIR', 'place where the templates are' ) do|p|
17
+ options[:templateDir] = p
18
+ end
19
+
20
+ end.parse!
21
+
22
+ mixer = MIX::Mixer.new
23
+ input_filename= options[:inputfile]
24
+ if !input_filename
25
+ STDOUT.puts "no inputfile given"
26
+ exit(1)
27
+ end
28
+
29
+ inputfile = options[:inputfile]
30
+ path = File.dirname(inputfile)
31
+ input_string = IO.read(inputfile)
32
+ outputfile = options[:outputfile] || File.basename(inputfile,".*") + ".out"
33
+ outputfile = File.join(path,outputfile)
34
+ inputdata = JSON.parse(input_string, {:symbolize_names => true})
35
+ templateDir = options[:templateDir]
36
+ mixer.mixItRuby(inputdata, templateDir)
37
+ mixer.writeResult2File(outputfile)
38
+
39
+
40
+
data/features/env.rb ADDED
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__),'../lib')
2
+ require "aruba/cucumber"
@@ -0,0 +1,32 @@
1
+ Feature:Commandline functionality
2
+
3
+ A User should be able to call a command to get a mixed outputfile
4
+ @announce
5
+ Scenario: CommandlineCall should give an error, when no inputfile
6
+ When I run `mixit`
7
+ Then the output should contain "no inputfile given"
8
+
9
+
10
+
11
+
12
+ Scenario: exit status of 0
13
+ When I run `mixit`
14
+ Then the exit status should be 1
15
+
16
+
17
+
18
+ @announce
19
+ Scenario: Generatining an outputfile
20
+ Given the testdatafile is in the aruba filespace
21
+ When I run `mixit -i testdata/testdata.json`
22
+ When I run `cat testdata/testdata.out`
23
+ Then the stdout should contain "Bond"
24
+
25
+
26
+ @announce
27
+ Scenario: mixIt should also run from another templatepath
28
+ Given the testdatafile is in the aruba filespace
29
+ Given the templatefiles are in the aruby filespace
30
+ When I run `mixit -i testdata/testdata.json -t tmp/aruba`
31
+ And I run `cat testdata/testdata.out`
32
+ Then the stdout should contain "Bond"
@@ -0,0 +1,13 @@
1
+
2
+
3
+ Given(/^the testdatafile is in the aruba filespace$/) do
4
+ `mkdir tmp/aruba/testdata && cp testdata/testdata.json tmp/aruba/testdata`
5
+ end
6
+
7
+
8
+ Given(/^the templatefiles are in the aruby filespace$/) do
9
+ `mkdir tmp/aruba/templates && cp templates/* tmp/aruba/templates`
10
+ end
11
+
12
+
13
+
@@ -0,0 +1,5 @@
1
+ After do |scenario|
2
+ if scenario.failed?
3
+ # save_page
4
+ end
5
+ end
data/lib/mixer.rb ADDED
@@ -0,0 +1,82 @@
1
+ require 'ostruct'
2
+ require 'erb'
3
+
4
+ module MIX
5
+ class Mixer
6
+ def initialize()
7
+ @result = []
8
+ @deeps = 0
9
+ end
10
+ #get all variables if the level, binds it and templates it
11
+ #this is important to maintain the order of buildingblock for the input
12
+
13
+
14
+ def premix(data,templatePath)
15
+ bind_hash={}
16
+ data.each do |k,v|
17
+ if v.kind_of?(Hash)
18
+ elsif v.kind_of?(Array)
19
+ else
20
+ bind_hash[k] = v
21
+ end
22
+ end
23
+ et = ErbIT.new( bind_hash)
24
+ template = getTemplate(bind_hash[:id])
25
+ filled = et.render(template)
26
+ @result.push(filled)
27
+ end
28
+
29
+
30
+ def mixItRuby(data,templatePath=nil)
31
+ ###putting an Index to the elements to find the sequence which has been in the json###
32
+ @deeps=@deeps + 1
33
+ premix(data,templatePath)
34
+ bind_hash={}
35
+ data.each do |k,v|
36
+ if v.kind_of?(Hash)
37
+ mixItRuby(v)
38
+ elsif v.kind_of?(Array)
39
+ ar = v
40
+ ar.each do |element|
41
+ mixItRuby(element)
42
+ end
43
+ else
44
+ #done in premix
45
+ end
46
+ end
47
+ puts @deeps.inspect
48
+ @deeps = @deeps -1
49
+ end
50
+
51
+
52
+ def writeResult2File(filename)
53
+ File.write(filename,@result)
54
+ end
55
+
56
+ def getResult
57
+ @result
58
+ end
59
+
60
+
61
+
62
+ def getTemplate(id,path = File.dirname(__FILE__))
63
+ path = File.join(path, '../templates')
64
+ filename = File.join(path,id)
65
+ template = IO.read(filename)
66
+ template
67
+ end
68
+
69
+
70
+
71
+ end
72
+
73
+
74
+
75
+
76
+ class ErbIT < OpenStruct
77
+ def render(template)
78
+ ERB.new(template).result(binding)
79
+ end
80
+ end
81
+
82
+ end
data/lib/template.rb ADDED
@@ -0,0 +1,20 @@
1
+
2
+ #This file is for future use to extract the variables from a erb template
3
+
4
+
5
+ class Template
6
+ attr_accessor :file_path,:contents
7
+
8
+ def self.all
9
+ Dir.glob("lib/templates/*").collect{|t| Template.new(t)}
10
+ end
11
+
12
+ def initialize(file_path)
13
+ @file_path = file_path
14
+ @contents = File.open(@file_path, "rb").read
15
+ end
16
+
17
+ def variables
18
+ self.contents.scan(/(\@[a-z]+[0-9a-z_]*)/i).uniq
19
+ end
20
+ end
data/mixItRuby.gemspec ADDED
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'mixItRuby'
3
+ s.version = '0.0.0'
4
+ s.date = '2014-05-25'
5
+ s.summary = "MixItRuby! mixes json in erb + CLI"
6
+ s.description = "A gem for mixing a nested json structure in erb files"
7
+ s.authors = ["Erhard Karger"]
8
+ s.email = 'erhard@kargers.org'
9
+ s.homepage = 'http://rubygems.org/gems/mixItRuby'
10
+ s.license = 'MIT'
11
+ git_files = `git ls-files`.split("\n") rescue ''
12
+ s.files = git_files
13
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ s.executables = %w(mixit)
15
+ s.require_paths = ["lib"]
16
+ end
data/readme.md ADDED
@@ -0,0 +1,89 @@
1
+ MIX
2
+ ===
3
+
4
+ What is MIX ?
5
+ -------------
6
+ Mix is a gem for mixing data with tamplatefiles.
7
+
8
+ Example Use Case
9
+ ----------------
10
+ You have to print bills for a list of items/products.
11
+ What you do :
12
+ Transform the data to a hash which lasts a specific form
13
+ Run mix with a erb template. Viola the output is (depending on the template)
14
+ ready to be formatted in latex of whatever.
15
+
16
+ But whatever......
17
+
18
+
19
+
20
+ Install
21
+ -------
22
+ gem mix
23
+
24
+ run (form commandline)
25
+ -----------------------
26
+ mix inputfile template outputfile
27
+
28
+
29
+ run withing a programm
30
+ ----------------------
31
+
32
+
33
+ current project status
34
+ ----------------------
35
+ Work in progress just started
36
+
37
+
38
+ You want to contributes ?
39
+ ==========================
40
+ Contact erhard@kargers.org
41
+
42
+
43
+
44
+
45
+ Structure of the data
46
+ =====================
47
+
48
+ json Format recursive, selfcontained.
49
+
50
+ recursive : substructures possible (nodes)
51
+ selfcontained : The name of the erb (id, for DB or File or whatever) is within the data
52
+
53
+ Here an example:
54
+
55
+ {"id":"adress.erb","name":"Bond","prename":"James",
56
+ "items":
57
+ [
58
+ {"id":"item.erb","product":"car1","prize":"10"},
59
+ {"id":"item.erb","product":"car2","prize":"11"}
60
+ ]
61
+ }
62
+
63
+ A building block consists of an id and other key value pair.
64
+
65
+ id: is a must. It represents the Identifier under which the template is accesible.
66
+ At the moment it is a filename. The other attributes on the same level, which are no array or hash are
67
+ the key value pais which fill the erb.
68
+ The adress.erb looks like this
69
+ <%=name%>, <%=prename%>
70
+ Then an array follow. An Array must be build of hashes, which can have subarrays or like in the example building blocks again.
71
+
72
+
73
+ What is mixItRuby needed for ?
74
+ ==============================
75
+ With that little gem you can build up personalized letters or emails or html pages.
76
+ Just imagine the erb file are tex files or xml-fo or html files or they are files with are ruby file again, which produce some output.
77
+
78
+ Another possibility is create other json files. Imagine the template erbs are templates for json. Then for example in SOA business objects can be chained and enriched from services.
79
+
80
+ There are a lot of posibilities.
81
+
82
+
83
+
84
+
85
+ Licence:
86
+ MIT
87
+
88
+ Author:
89
+ Erhard Karger
@@ -0,0 +1,39 @@
1
+ require 'stringio'
2
+ require 'spec_helper'
3
+ require File.join(File.dirname(__FILE__),'../../lib/mixer.rb')
4
+ require "json"
5
+ describe "Mixer" do
6
+
7
+ it "should traverse the data tree and contain the data in the result" do
8
+ mix = MIX::Mixer.new()
9
+ mix.mixItRuby(data)
10
+ result = mix.getResult
11
+ array_contains?(result,"Bond").should be_true
12
+ end
13
+
14
+
15
+ it "should preserver the sequence of building blocks of the json in the result" do
16
+ mix = MIX::Mixer.new()
17
+ mix.mixItRuby(data)
18
+ result = mix.getResult
19
+ result[0].should include("Bond")
20
+ end
21
+
22
+
23
+
24
+ end
25
+
26
+
27
+ def data
28
+ dat={id: "adress.erb", name: "Bond", prename: "James", items: [{id: "item.erb", product: "car1", prize: "10"},{id: "item.erb" , product: "car2", prize: "11"}]}
29
+ end
30
+
31
+
32
+ def array_contains?(array, token)
33
+ array.each do |element|
34
+ if element =~ /#{token}/
35
+ return true
36
+ end
37
+ end
38
+ return false
39
+ end
@@ -0,0 +1,6 @@
1
+ # spec/spec_helper.rb
2
+ Dir[File.join(File.dirname(__FILE__), '../lib/**/*.rb')].each {|f| require f }
3
+
4
+
5
+
6
+ #require File.join(File.dirname(__FILE__),'../lib')
@@ -0,0 +1,3 @@
1
+ <%="adress.erb"%>
2
+ <%=name%> , <%=prename%>
3
+
@@ -0,0 +1,2 @@
1
+ <%="item.erb"%>
2
+ <%=product%> <%=prize%>
@@ -0,0 +1 @@
1
+ {"id":"adress.erb","name":"Bond","prename":"James","items":[{"id":"item.erb","product":"car1","prize":"10"},{"id":"item.erb","product":"car2","prize":"11"}]}
@@ -0,0 +1 @@
1
+ ["adress.erb\nBond , James\n\n", "item.erb\ncar1 10\n", "item.erb\ncar2 11\n"]
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mixItRuby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Erhard Karger
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-05-25 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A gem for mixing a nested json structure in erb files
15
+ email: erhard@kargers.org
16
+ executables:
17
+ - mixit
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - Guardfile
25
+ - bin/mixit
26
+ - features/env.rb
27
+ - features/mixer.feature
28
+ - features/step_definitions/mixer_steps.rb
29
+ - features/support/hooks.rb
30
+ - lib/mixer.rb
31
+ - lib/template.rb
32
+ - mixItRuby.gemspec
33
+ - readme.md
34
+ - spec/lib/mixer_spec.rb
35
+ - spec/spec_helper.rb
36
+ - templates/adress.erb
37
+ - templates/item.erb
38
+ - testdata/testdata.json
39
+ - testdata/testdata.out
40
+ homepage: http://rubygems.org/gems/mixItRuby
41
+ licenses:
42
+ - MIT
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.24
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: MixItRuby! mixes json in erb + CLI
65
+ test_files: []