From 9abbe7ab50a0f34db574bccbefe3f9843e3d8960 Mon Sep 17 00:00:00 2001 From: Erhard Date: Thu, 22 May 2014 17:38:01 +0200 Subject: [PATCH] initial commit --- .gitignore | 44 ++++++++++++++ Gemfile | 9 +++ Gemfile.lock | 69 ++++++++++++++++++++++ Guardfile | 12 ++++ features/env.rb | 56 ++++++++++++++++++ features/mixer.feature | 13 +++++ features/step_definitions/mixer_steps.rb | 18 ++++++ features/support/hooks.rb | 5 ++ features/testDir/data.json | 0 features/testDir/templates/test.txt.erb | 0 lib/mixer.rb | 73 ++++++++++++++++++++++++ lib/template.rb | 16 ++++++ readme.md | 60 +++++++++++++++++++ spec/lib/mixer_spec.rb | 23 ++++++++ spec/spec_helper.rb | 7 +++ templates/adress.erb | 3 + templates/item.erb | 2 + test.json | 1 + 18 files changed, 411 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 Guardfile create mode 100644 features/env.rb create mode 100644 features/mixer.feature create mode 100644 features/step_definitions/mixer_steps.rb create mode 100644 features/support/hooks.rb create mode 100644 features/testDir/data.json create mode 100644 features/testDir/templates/test.txt.erb create mode 100644 lib/mixer.rb create mode 100644 lib/template.rb create mode 100644 readme.md create mode 100644 spec/lib/mixer_spec.rb create mode 100644 spec/spec_helper.rb create mode 100644 templates/adress.erb create mode 100644 templates/item.erb create mode 100644 test.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fd93f9d --- /dev/null +++ b/.gitignore @@ -0,0 +1,44 @@ +./log/* +log/*.log +log/development.log +.DS_Store +doc/api +doc/app +coverage +db/*.sqlite3 +db/development_structure.sql +public/uploads +spec/dummy/log +spec/dummy/tmp +spec/dummy/public/system/images +spec/dummy/puplic/assets/* +spec/dummy/public/system/uploads/* +spec/dummy/public/system/* +spec/dummy/public/uploads +spec/dummy/log* +spec/dummy/tmp* +index/* +*.swp +*.swo +.idea/ +.idea/**/* +nbproject/* +*.war +db/schema.rb +tmp/* +wMake.bat +etc/web.xml +build/ +build/* +.bundle/ +~/ +~/* +*.knob +tmp +mysession.vi* +ruby-uuid +localhistory +test +prod +*.png +site/dev/images/galleries diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..00ea18c --- /dev/null +++ b/Gemfile @@ -0,0 +1,9 @@ +source 'http://rubygems.org' +gem 'recursive-open-struct' + group :test do + gem 'guard-rspec' + gem 'guard' + gem 'rspec' + gem 'cucumber' + end + diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..eff2314 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,69 @@ +GEM + remote: http://rubygems.org/ + specs: + builder (3.2.2) + celluloid (0.15.2) + timers (~> 1.1.0) + celluloid-io (0.15.0) + celluloid (>= 0.15.0) + nio4r (>= 0.5.0) + coderay (1.1.0) + cucumber (1.3.15) + builder (>= 2.1.2) + diff-lcs (>= 1.1.3) + gherkin (~> 2.12) + multi_json (>= 1.7.5, < 2.0) + multi_test (>= 0.1.1) + diff-lcs (1.2.5) + ffi (1.9.3) + formatador (0.2.4) + gherkin (2.12.2) + multi_json (~> 1.3) + guard (2.6.0) + formatador (>= 0.2.4) + listen (~> 2.7) + lumberjack (~> 1.0) + pry (>= 0.9.12) + thor (>= 0.18.1) + guard-rspec (4.2.8) + guard (~> 2.1) + rspec (>= 2.14, < 4.0) + listen (2.7.1) + celluloid (>= 0.15.2) + celluloid-io (>= 0.15.0) + rb-fsevent (>= 0.9.3) + rb-inotify (>= 0.9) + lumberjack (1.0.5) + method_source (0.8.2) + multi_json (1.10.1) + multi_test (0.1.1) + nio4r (1.0.0) + pry (0.9.12.6) + coderay (~> 1.0) + method_source (~> 0.8) + slop (~> 3.4) + rb-fsevent (0.9.4) + rb-inotify (0.9.3) + ffi (>= 0.5.0) + recursive-open-struct (0.4.5) + rspec (2.14.1) + rspec-core (~> 2.14.0) + rspec-expectations (~> 2.14.0) + rspec-mocks (~> 2.14.0) + rspec-core (2.14.8) + rspec-expectations (2.14.5) + diff-lcs (>= 1.1.3, < 2.0) + rspec-mocks (2.14.6) + slop (3.5.0) + thor (0.19.1) + timers (1.1.0) + +PLATFORMS + ruby + +DEPENDENCIES + cucumber + guard + guard-rspec + recursive-open-struct + rspec diff --git a/Guardfile b/Guardfile new file mode 100644 index 0000000..782fb78 --- /dev/null +++ b/Guardfile @@ -0,0 +1,12 @@ +# A sample Guardfile +# More info at https://github.com/guard/guard#readme +notification :tmux, display_message: true +guard :rspec do + watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } + watch('spec/spec_helper.rb') { "spec" } + watch(%r{^spec/.+_spec\.rb$}) + watch(%r{^spec/spec_helper\.rb}) +end + + + diff --git a/features/env.rb b/features/env.rb new file mode 100644 index 0000000..97d0682 --- /dev/null +++ b/features/env.rb @@ -0,0 +1,56 @@ +require 'capybara/cucumber' +require 'capybara/poltergeist' + @processingWorkingDir=File.dirname(__FILE__) + @processingWorkDir=@processingWorkingDir+"/../processing/vert/" +#Start the two servers : +# +#Processing Server : + # @t1 = fork do + # system("cd #{@processingWorkDir} && rvm use jruby-1.7.1@masha_gems && vertx run #{@processingWorkDir}server_app.js -conf test.json") + # end + # sleep(5) + # @siteServerPath=File.dirname(__FILE__) + "/../site/bin/" # This is a Serverinstance which is not deployed. It simulates the server of the provider + # @t2 = fork do + # system("cd #{@siteServerPath} && ruby pageServer.rb -p 4568") + # end + # sleep(5) +if ENV['IN_BROWSER'] + # On demand: non-headless tests via Selenium/WebDriver + # To run the scenarios in browser (default: Firefox), use the following command line: + # IN_BROWSER=true bundle exec cucumber + # or (to have a pause of 1 second between each step): + # IN_BROWSER=true PAUSE=1 bundle exec cucumber + Capybara.default_driver = :selenium + AfterStep do + sleep (ENV['PAUSE'] || 0).to_i + end +else + # DEFAULT: headless tests with poltergeist/PhantomJS + Capybara.register_driver :poltergeist do |app| + Capybara::Poltergeist::Driver.new( + app, + window_size: [1280, 1024]#, + #debug: true + ) + end + Capybara.default_driver = :poltergeist + Capybara.javascript_driver = :poltergeist +end +at_exit do + + puts @t1 + puts @t2 + + + if @t1 + puts "processserver killed" + Process.kill("HUP",@t1) + sleep(10) + end + if @t2 + puts "pageServer killed" + Process.kill("HUP",@t2) + sleep(10) + end + puts "bye" +end diff --git a/features/mixer.feature b/features/mixer.feature new file mode 100644 index 0000000..8869781 --- /dev/null +++ b/features/mixer.feature @@ -0,0 +1,13 @@ +Feature:Commandline functionality + + A User should be able to call a command to get a mixed outputfile + +Scenario: CommandlineCall + Given a empty testenvironment with one erb file and one data file + When I enter "mix dataInput dataoutput templateDir" + Then I should get a file with the name dataInput and the suffix mixed + + + + + diff --git a/features/step_definitions/mixer_steps.rb b/features/step_definitions/mixer_steps.rb new file mode 100644 index 0000000..e4833c0 --- /dev/null +++ b/features/step_definitions/mixer_steps.rb @@ -0,0 +1,18 @@ + + + + +Given(/^a empty testenvironment with one erb file and one data file$/) do + ###just do something + if File.exists?("features/testDir/output/test.txt") + system("rm /features/testDir/output/*") + end + end + +When(/^I enter "(.*?)"$/) do |arg1| + system(arg1) +end + +Then(/^I should get a file with the name dataInput and the suffix mixed$/) do + File.exists?("features/testDir/output/test.txt").should==true +end diff --git a/features/support/hooks.rb b/features/support/hooks.rb new file mode 100644 index 0000000..0a017ed --- /dev/null +++ b/features/support/hooks.rb @@ -0,0 +1,5 @@ +After do |scenario| + if scenario.failed? + # save_page + end +end diff --git a/features/testDir/data.json b/features/testDir/data.json new file mode 100644 index 0000000..e69de29 diff --git a/features/testDir/templates/test.txt.erb b/features/testDir/templates/test.txt.erb new file mode 100644 index 0000000..e69de29 diff --git a/lib/mixer.rb b/lib/mixer.rb new file mode 100644 index 0000000..1729318 --- /dev/null +++ b/lib/mixer.rb @@ -0,0 +1,73 @@ +require 'recursive-open-struct' + + + +module MIX + + class Mixer + + def initialize(data) + @data = data + @result = [] + @deeps = 0 + end + + def mixItBaby(data) + ###putting an Index to the elements to find the sequence which has been in the json### + @deeps=@deeps + 1 + ros = RecursiveOpenStruct.new(data,recurse_over_array: true) + puts ros.inspect + bind_hash={} + data.each do |k,v| + if v.kind_of?(Hash) + mixItBaby(v) + elsif v.kind_of?(Array) + ar = v + ar.each do |element| + mixItBaby(element) + end + else + bind_hash[k] = v + end + end + puts bind_hash.inspect + et = ErbIT.new( bind_hash) + template = getTemplate(bind_hash[:id]) + filled = et.render(template) + puts filled.inspect + @result.push(filled) + puts @deeps.inspect + @deeps = @deeps -1 + end + + + + + +def getResult + @result +end + + + + def getTemplate(id) + path = File.join(File.dirname(__FILE__),'../templates') + filename = File.join(path,id) + template = IO.read(filename) + template + end + + + + end + + + + + class ErbIT < OpenStruct + def render(template) + ERB.new(template).result(binding) + end + end + +end diff --git a/lib/template.rb b/lib/template.rb new file mode 100644 index 0000000..a7a874f --- /dev/null +++ b/lib/template.rb @@ -0,0 +1,16 @@ +class Template + attr_accessor :file_path,:contents + + def self.all + Dir.glob("lib/templates/*").collect{|t| Template.new(t)} + end + + def initialize(file_path) + @file_path = file_path + @contents = File.open(@file_path, "rb").read + end + + def variables + self.contents.scan(/(\@[a-z]+[0-9a-z_]*)/i).uniq + end +end diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..62a58b3 --- /dev/null +++ b/readme.md @@ -0,0 +1,60 @@ +MIX +=== + +What is MIX ? +------------- +Mix is a gem for mixing data with tamplatefiles. + +Example Use Case +---------------- +You have to print bills for a list of items/products. +What you do : +Transform the data to a hash which lasts a specific form +Run mix with a erb template. Viola the output is (depending on the template) +ready to be formatted in latex of whatever. + +But whatever...... + + + +Install +------- +gem mix + +run (form commandline) +----------------------- +mix inputfile template outputfile + + +run withing a programm +---------------------- + + +current project status +---------------------- +Work in progress just started + + +You want to contributes ? +========================== +Contact erhard@kargers.org + + + + +Structure of the data +===================== + +json Format recursive, selfcontained. + +recursive : substructures possible (nodes) +selfcontained : The name of the erb (id, for DB or File or whatever) is within the data + + + + + + + + + diff --git a/spec/lib/mixer_spec.rb b/spec/lib/mixer_spec.rb new file mode 100644 index 0000000..31229ad --- /dev/null +++ b/spec/lib/mixer_spec.rb @@ -0,0 +1,23 @@ + +require File.join(File.dirname(__FILE__),'../../lib/mixer.rb') + +describe "Mixer" do + +it "should traverse the data tree" do + mix = MIX::Mixer.new(data) + mix.mixItBaby(data) + puts "------------------------------------------_" + result = mix.getResult + puts result + result.should include("Bond") + puts "..........................................." + +end + +def data + {id: "adress.erb", name: "Bond", prename: "James", items: [{id: "item.erb", product: "car1", prize: "10"},{id: "item.erb" , product: "car2", prize: "11"}]} +end + + + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..03d3fbc --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,7 @@ + +puts "im Helper ----------------<<<<<<<<<<<<<>>>>>>>>>>>>>>" + + +# spec/spec_helper.rb +#Dir[File.join(File.dirname(__FILE__), '../lib/**/*.rb')].each {|f| require f } +require File.join(File.dirname(__FILE__),'../lib/mixer.rb') diff --git a/templates/adress.erb b/templates/adress.erb new file mode 100644 index 0000000..0f2814f --- /dev/null +++ b/templates/adress.erb @@ -0,0 +1,3 @@ +<%="adress.erb"%> +<%=name%> , <%=prename%> + diff --git a/templates/item.erb b/templates/item.erb new file mode 100644 index 0000000..ec9cfc3 --- /dev/null +++ b/templates/item.erb @@ -0,0 +1,2 @@ +<%="item.erb"%> +<%=product%> <%=prize%> diff --git a/test.json b/test.json new file mode 100644 index 0000000..d5801ee --- /dev/null +++ b/test.json @@ -0,0 +1 @@ +{"id":"test.txt.erb","data":{"name":"karger","pername":"erhard","price":10}} \ No newline at end of file