Skip to content

Commit

Permalink
commit for building a gem
Browse files Browse the repository at this point in the history
  • Loading branch information
erhard committed May 23, 2014
1 parent 9abbe7a commit 0b65a51
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 51 deletions.
35 changes: 35 additions & 0 deletions bin/mixit
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env ruby
require 'mixer'
require 'optparse'


options = {}
optparse = OptionParser.new do|opts|
opts.banner = "Usage: mixit [-o outputfile -i inputfile -t templatefile]"
opts.on( '-i', '--input FILE', 'json Inputfile' ) do|p|
options[:inputfile] = p
end

opts.on( '-o', '--output FILE', 'mixed outputfile' ) do|p|
options[:outputfile] = p
end


opts.on( '-t', '--template directory', 'place where the templates are' ) do|p|
options[:templateDir] = p
end

end.parse!


mixer = MIX::Mixer.new

input_string = IO.read(options["inputfile"])
outputfile = File.new(options["outputfile"])
inputdata = JSON.pars(input_string)






41 changes: 25 additions & 16 deletions lib/mixer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
require 'recursive-open-struct'



module MIX

class Mixer
Expand All @@ -11,39 +7,52 @@ def initialize(data)
@result = []
@deeps = 0
end
#get all variables if the level, binds it and templates it
#this is important to maintain the order of buildingblock for the input

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

def premix(data)
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)
end


def mixItRuby(data)
###putting an Index to the elements to find the sequence which has been in the json###
@deeps=@deeps + 1
premix(data)
bind_hash={}
data.each do |k,v|
if v.kind_of?(Hash)
mixItRuby(v)
elsif v.kind_of?(Array)
ar = v
ar.each do |element|
mixItRuby(element)
end
else
#done in premix
end
end

puts @deeps.inspect
@deeps = @deeps -1
end





def getResult
@result
end
Expand Down
18 changes: 18 additions & 0 deletions mixItRuby.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Gem::Specification.new do |s|
s.name = 'mixItRuby'
s.version = '0.0.0'
s.date = '2014-05-29'
s.summary = "MitItRuby!"
s.description = "A gem for mixing a nested json structure in erb files"
s.authors = ["Erhard Karger"]
s.email = '[email protected]'
s.files = ["lib/mixer.rb"]
s.homepage = "www.kargers.org"
'http://rubygems.org/gems/mixItRuby'
s.license = 'MIT'
git_files = `git ls-files`.split("\n") rescue ''
s.files = git_files
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = %w(mixit)
s.require_paths = ["lib"]
end
66 changes: 46 additions & 20 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,78 @@ 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
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.
ready to be formatted in latex of whatever.

But whatever......
But whatever......



Install
-------
gem mix
Install
-------
gem mix

run (form commandline)
-----------------------
mix inputfile template outputfile
-----------------------
mix inputfile template outputfile


run withing a programm
----------------------
run withing a programm
----------------------


current project status
----------------------
Work in progress just started
current project status
----------------------
Work in progress just started


You want to contributes ?
==========================
Contact [email protected]
You want to contributes ?
==========================
Contact [email protected]




Structure of the data
=====================
Structure of the data
=====================

json Format recursive, selfcontained.
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
selfcontained : The name of the erb (id, for DB or File or whatever) is within the data

Here an example:

{"id":"adress.erb","name":"Bond","prename":"James",
"items":
[
{"id":"item.erb","product":"car1","prize":"10"},
{"id":"item.erb","product":"car2","prize":"11"}
]
}

A building block consists of an id and other key value pair.

id: is a must. It represents the Identifier under which the template is accesible.
At the moment it is a filename. The other attributes on the same level, which are no array or hash are
the key value pais which fill the erb.
The adress.erb looks like this
<%=name%>, <%=prename%>
Then an array follow. An Array must be build of hashes, which can have subarrays or like in the example building blocks again.


What is mixItRuby needed for ?
==============================
With that little gem you can build up personalized letters or emails or html pages.
Just imagine the erb file are tex files or xmlfo or html files or they are files with are ruby file again, which produce some output.
There are a lot of posibilities.




Licence:
MIT

Author:
Erhard Karger
48 changes: 34 additions & 14 deletions spec/lib/mixer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@

require File.join(File.dirname(__FILE__),'../../lib/mixer.rb')

require "json"
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
it "should traverse the data tree and contain the data in the result" do
mix = MIX::Mixer.new(data)
mix.mixItRuby(data)
result = mix.getResult
puts result.inspect
array_contains?(result,"Bond").should be_true

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


it "should preserver the sequence of building blocks of the json in the result" do
mix = MIX::Mixer.new(data)
mix.mixItRuby(data)
result = mix.getResult
result[0].should include("Bond")
end


def array_contains?(array, token)
array.each do |element|
if element =~ /#{token}/
return true
end
end
return false
end

def data
dat={id: "adress.erb", name: "Bond", prename: "James", items: [{id: "item.erb", product: "car1", prize: "10"},{id: "item.erb" , product: "car2", prize: "11"}]}
f=File.new("spec/testdata.json","w")
f.write(dat.to_json)
f.close
dat
end



Expand Down
1 change: 1 addition & 0 deletions spec/testdata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"adress.erb","name":"Bond","prename":"James","items":[{"id":"item.erb","product":"car1","prize":"10"},{"id":"item.erb","product":"car2","prize":"11"}]}
1 change: 0 additions & 1 deletion test.json

This file was deleted.

0 comments on commit 0b65a51

Please sign in to comment.