crayfish 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/MIT-LICENSE +21 -0
  2. data/README.md +115 -0
  3. data/Rakefile +35 -0
  4. data/lib/crayfish.rb +53 -0
  5. data/lib/crayfish/action_controller.rb +35 -0
  6. data/lib/crayfish/action_view.rb +110 -0
  7. data/lib/crayfish/elements/cell_helper.rb +70 -0
  8. data/lib/crayfish/elements/container.rb +64 -0
  9. data/lib/crayfish/elements/form.rb +161 -0
  10. data/lib/crayfish/elements/row.rb +70 -0
  11. data/lib/crayfish/elements/table.rb +51 -0
  12. data/lib/crayfish/rails/base.rb +36 -0
  13. data/lib/crayfish/rails/pdf.rb +47 -0
  14. data/lib/crayfish/version.rb +3 -0
  15. data/lib/tasks/crayfish_tasks.rake +4 -0
  16. data/test/crayfish_test.rb +227 -0
  17. data/test/dummy/README.rdoc +261 -0
  18. data/test/dummy/Rakefile +7 -0
  19. data/test/dummy/app/assets/javascripts/application.js +15 -0
  20. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  21. data/test/dummy/app/controllers/application_controller.rb +3 -0
  22. data/test/dummy/app/helpers/application_helper.rb +2 -0
  23. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  24. data/test/dummy/config.ru +4 -0
  25. data/test/dummy/config/application.rb +56 -0
  26. data/test/dummy/config/boot.rb +10 -0
  27. data/test/dummy/config/database.yml +25 -0
  28. data/test/dummy/config/environment.rb +5 -0
  29. data/test/dummy/config/environments/development.rb +37 -0
  30. data/test/dummy/config/environments/production.rb +67 -0
  31. data/test/dummy/config/environments/test.rb +37 -0
  32. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  33. data/test/dummy/config/initializers/inflections.rb +15 -0
  34. data/test/dummy/config/initializers/mime_types.rb +5 -0
  35. data/test/dummy/config/initializers/secret_token.rb +7 -0
  36. data/test/dummy/config/initializers/session_store.rb +8 -0
  37. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  38. data/test/dummy/config/locales/en.yml +5 -0
  39. data/test/dummy/config/routes.rb +58 -0
  40. data/test/dummy/db/test.sqlite3 +0 -0
  41. data/test/dummy/log/test.log +2 -0
  42. data/test/dummy/public/404.html +26 -0
  43. data/test/dummy/public/422.html +26 -0
  44. data/test/dummy/public/500.html +25 -0
  45. data/test/dummy/public/favicon.ico +0 -0
  46. data/test/dummy/script/rails +6 -0
  47. data/test/test_helper.rb +32 -0
  48. metadata +174 -0
@@ -0,0 +1,64 @@
1
+ # Copyright (c) 2012 Bingoentrepenøren AS
2
+ # Copyright (c) 2012 Patrick Hanevold
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ module Crayfish
24
+ class CrayContainer
25
+
26
+ attr_accessor :raw
27
+ attr_reader :options,:tokens,:pdf
28
+
29
+ def initialize fish, pdf, options = {}
30
+ @fish = fish
31
+ @pdf = pdf
32
+ @raw = []
33
+ @options = options
34
+ @tokens = { :span => options[:span] || /%\|/, :element => options[:element] || /%c{[^}]*}/ }
35
+ end
36
+
37
+ def append stuff, options={}
38
+ @raw << stuff
39
+ end
40
+
41
+ def field str
42
+ "%c{#{str}}"
43
+ end
44
+
45
+ def span
46
+ '%|'
47
+ end
48
+
49
+ def row *args, &block
50
+ raise "row must have block" unless block_given?
51
+ row = CrayRow.new(@fish,pdf,self)
52
+ block.call row
53
+ row.draw ''
54
+ end
55
+
56
+ def row_for *args, &block
57
+ raise "row_for must have block" unless block_given?
58
+ row = CrayRow.new(@fish,pdf,self,args.first)
59
+ block.call row
60
+ row.draw ''
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,161 @@
1
+ # Copyright (c) 2012 Bingoentrepenøren AS
2
+ # Copyright (c) 2012 Patrick Hanevold
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ module Crayfish
24
+ class CrayForm < CrayContainer
25
+
26
+ def initialize fish, pdf, options = {}
27
+ @fish=fish
28
+ @spans = []
29
+ super
30
+ end
31
+
32
+ # Flush accumulated text and append a (prawn) element
33
+ def append stuff, options={}
34
+ buf = @fish.send(:flush,false)
35
+ @spans << (options[:spans] || [])
36
+ super
37
+ #form_body(buf) if buf.strip != ''
38
+ buf
39
+ end
40
+
41
+ def heading str
42
+ append [form_body(str,:spacing => 0)]
43
+ end
44
+
45
+ # Draws the form in all its glory
46
+ def draw text
47
+ form_body(text,@options)
48
+
49
+ allign_spans
50
+
51
+ table = pdf.make_table raw, :width => 540, :cell_style => { :padding => [0,0,0,0], :border_width => 1 }
52
+
53
+ pdf.fill_color 'CCCCFF'
54
+ pdf.fill { @pdf.rectangle [0, pdf.cursor], table.width, table.height }
55
+
56
+ pdf.fill_color '000000'
57
+ table.draw
58
+ table
59
+ end
60
+
61
+ private
62
+
63
+ def allign_spans
64
+ # group by span count
65
+ # in the future we may want to group by span id's or paths to allow much more fine grained controll of span allignment
66
+ groups = @spans.map{ |row| row.max }.uniq.select{ |cnt| cnt }
67
+ i = -1
68
+ indexed = @spans.map{ |span| i+=1; {:index => i, :row => raw[i] } }
69
+
70
+ groups.each do |group_cnt|
71
+ # all rows with group_cnt number of spans
72
+ considder = indexed.select{ |row| @spans[row[:index]].max == group_cnt }.map{ |row| row[:index] }
73
+
74
+ # the span width map
75
+ width_map = considder.map do |index|
76
+ spans = @spans[index]
77
+ row = raw[index].first[:content]
78
+ widths = row.row(0).columns(0..-1).map{ |cell| cell.natural_content_width }
79
+
80
+ i = 0
81
+ spans.map do |span_index|
82
+ span = widths[i..span_index-1]
83
+ i=span_index
84
+ span.sum
85
+ end
86
+ end
87
+
88
+ alligned_widths = width_map.transpose.map{ |col| col.max }
89
+
90
+ # allign cells
91
+ considder.each do |index|
92
+ row_width = reminding_width = raw[index].first[:content].row(0).width
93
+ raw[index].first[:content].row(0).columns(0..alligned_widths.size-1).each_with_index do |cell,i|
94
+ cell.width = alligned_widths[i]
95
+ reminding_width -= cell.width
96
+ end
97
+ # fit reminding cells in row
98
+ # TODO: respect constraints!
99
+ x = row_width-reminding_width
100
+ w = reminding_width / raw[index].first[:content].row(0).columns(alligned_widths.size..-1).size
101
+ raw[index].first[:content].row(0).columns(alligned_widths.size..-1).each do |cell|
102
+ cell.x = x
103
+ cell.width = w
104
+ x += w
105
+ end
106
+ end
107
+ end
108
+ end
109
+
110
+ def _text text,options = {}
111
+ Crayfish::CellHelper.new(Prawn::Table::Cell.make(pdf, text), options)
112
+ end
113
+
114
+ def _field width=nil
115
+ cell = _text('', { :type => :field })
116
+ cell.width(width) if width
117
+ cell
118
+ end
119
+
120
+ def _space width=nil
121
+ cell = _text('')
122
+ cell.width(width) if width
123
+ end
124
+
125
+ def form_body form,options = {}
126
+ color = 'CCCCFF'
127
+
128
+ space_width = 10
129
+
130
+ lines = form.split("\n").map{ |line| line.strip }.select{ |line| line.size > 0 }
131
+ return unless lines.size > 0
132
+
133
+ spans_per_line = lines.map{ |line| line.split(tokens[:span]).size-1 }
134
+ raise 'the number of spans must be the same on all lines' unless spans_per_line.uniq.size <= 1
135
+ spans_per_line = spans_per_line.first
136
+
137
+ span_table = lines.map{ |line| line.split(tokens[:span]) }
138
+
139
+ # split text from fields in all spans
140
+ table = span_table.map do |row|
141
+ row.map do |span|
142
+ span.split(tokens[:element]).zip(span.scan(tokens[:element])).flatten
143
+ end
144
+ end
145
+
146
+ # translate each span into a subtable
147
+ table.map! do |row|
148
+ row.map do |span|
149
+ pdf.make_table [ span.map{ |cell| cell =~ /^#{tokens[:element].source}$/ ? _field(cell.count(' ')*space_width) : _text(cell) } ],:cell_style => { :borders => [] }
150
+ end
151
+ end
152
+
153
+ padding = options[:spacing] || (pdf.font.line_gap + pdf.font.descender)
154
+
155
+ # note that the spans above are in sub-tables, we need adressable spans (like xpath) if we want to use them further
156
+ # for now, pass a empty span list for this row (this form body is appended as a single row)
157
+ append [pdf.make_table(table, :width => 540, :cell_style => {:padding => [padding,0,padding,0], :borders => [] })], :spans => []
158
+ end
159
+
160
+ end
161
+ end
@@ -0,0 +1,70 @@
1
+ # Copyright (c) 2012 Bingoentrepenøren AS
2
+ # Copyright (c) 2012 Patrick Hanevold
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ module Crayfish
24
+ class CrayRow < CrayContainer
25
+
26
+ def initialize fish,pdf,table,model=nil
27
+ @table = table
28
+ @color = 'CCCCFF'
29
+ @spans = []
30
+ @model = model
31
+ super fish,pdf
32
+ end
33
+
34
+ def label label
35
+ append :content => label, :background_color => @color
36
+ end
37
+
38
+ def text text
39
+ append :content => text, :background_color => 'ffffff'
40
+ end
41
+
42
+ def span
43
+ @spans << @raw.size
44
+ end
45
+
46
+ def field *args
47
+ label = ''
48
+ value = ''
49
+ if args.first.kind_of? Symbol
50
+ raise "you must use row_for(model) to reference with symbols" unless @model
51
+ label = I18n.t args.first, :scope => [:activerecord, :attributes, @model.class.name.underscore.to_sym], :default => args.first.to_s.titleize
52
+ value = @model.respond_to?(args.first) ? @model.send(args.first) : @model[args.first]
53
+ else
54
+ label = args.first[:label]
55
+ value = args.first[:value]
56
+ end
57
+ self.label label
58
+ text value
59
+ end
60
+
61
+ def draw text
62
+ if @table.kind_of? CrayForm
63
+ @table.append [ { :content => @table.pdf.make_table([raw], :width => 540) } ], :spans => @spans
64
+ else
65
+ @table.append raw
66
+ end
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,51 @@
1
+ # Copyright (c) 2012 Bingoentrepenøren AS
2
+ # Copyright (c) 2012 Patrick Hanevold
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ module Crayfish
24
+ class CrayTable < CrayContainer
25
+
26
+ attr_reader :pdf
27
+
28
+ def initialize fish,pdf
29
+ @color = 'CCCCFF'
30
+ super
31
+ end
32
+
33
+ def field *args
34
+ label = ''
35
+ value = ''
36
+ if args.first.kind_of? Symbol
37
+ label = args.first.to_s
38
+ value = ''
39
+ else
40
+ label = args.first[:label]
41
+ value = args.first[:value]
42
+ end
43
+ append [{ :content => label, :background_color => @color}, value]
44
+ end
45
+
46
+ def draw text
47
+ pdf.table raw, :width => 540
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,36 @@
1
+ # Copyright (c) 2012 Bingoentrepenøren AS
2
+ # Copyright (c) 2012 Patrick Hanevold
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ module Crayfish
24
+ module Rails
25
+ class Base
26
+
27
+ def self.call template
28
+ "setup;" +
29
+ "paint \"#{template.source}\""
30
+ end
31
+
32
+ end
33
+ end
34
+ end
35
+
36
+
@@ -0,0 +1,47 @@
1
+ # Copyright (c) 2012 Bingoentrepenøren AS
2
+ # Copyright (c) 2012 Patrick Hanevold
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ module Crayfish
24
+ module Rails
25
+
26
+ class PDF
27
+ extend ActiveSupport::Memoizable
28
+
29
+ attr_reader :options
30
+
31
+ def initialize(controller)
32
+ @options = controller.send :options || {}
33
+
34
+ controller.response.content_type ||= Mime::PDF
35
+
36
+ inline = options[:inline] ? 'inline' : 'attachment'
37
+ filename = options[:filename] ? "filename=#{options[:filename]}" : nil
38
+ controller.headers["Content-Disposition"] = [inline,filename].compact.join(';')
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+ end
45
+
46
+
47
+
@@ -0,0 +1,3 @@
1
+ module Crayfish
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :crayfish do
3
+ # # Task goes here
4
+ # end