model_tableizer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in model_tableizer.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Your Name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # ModelTableizer
2
+
3
+ Given a collection of objects where within the object instance variables
4
+ are accessable, as method calls, produce an html table layout for the collection. Since this
5
+ generates raw html you can use in any ruby web application server(Sinatra, Rails, Camping, etc...)
6
+
7
+ Example:
8
+ <code>
9
+ users = [
10
+ User.new(:name => 'Some Name 1', :email => 'some email 1',
11
+ :created_at => Time.now, :beta_request => '/shome_url_1'),
12
+ User.new(:name => 'Some Name 2', :email => 'some email 2',
13
+ :created_at => Time.now, :beta_request => ''),
14
+ User.new(:name => 'Some Name 3', :email => 'some email 3',
15
+ :created_at => Time.now, :beta_request => '/shome_url_3')
16
+ ]
17
+
18
+
19
+ table users, :id => 'list_of_users' do
20
+ column :name, :id => 'blah_id'
21
+ column :email
22
+ column :created_at
23
+ column "CRM Status", :id => 'from_crm' do
24
+ "<a href='#{user.beta_request}'>#{user.beta_request}</a>" if user.beta_request
25
+ end
26
+ end
27
+ </code>
28
+
29
+ Results in:
30
+
31
+ <table id='list_of_users'><thead><th class='name' id = 'blah_id' >Name</th><th class='email' >Email</th><th class='created_at' >Created At</th><th class='CRM Status' id = 'from_crm' >Crm Status</th></thead><tr><td name = 'name' id = 'blah_id'>Some Name 1</td><td name = 'email'>some email 1</td><td name = 'created_at'>2012-04-06 15:37:49 -0700</td><td name = 'CRM Status' id = 'from_crm'><a href='/shome_url_1'>/shome_url_1</a></td></tr><tr><td name = 'name' id = 'blah_id'>Some Name 2</td><td name = 'email'>some email 2</td><td name = 'created_at'>2012-04-06 15:37:49 -0700</td><td name = 'CRM Status' id = 'from_crm'><a href=''></a></td></tr><tr><td name = 'name' id = 'blah_id'>Some Name 3</td><td name = 'email'>some email 3</td><td name = 'created_at'>2012-04-06 15:37:49 -0700</td><td name = 'CRM Status' id = 'from_crm'><a href='/shome_url_3'>/shome_url_3</a></td></tr></table>
32
+
33
+
34
+
35
+ ## Installation
36
+
37
+ Add this line to your application's Gemfile:
38
+
39
+ gem 'model_tableizer'
40
+
41
+ And then execute:
42
+
43
+ $ bundle
44
+
45
+ Or install it yourself as:
46
+
47
+ $ gem install model_tableizer
48
+
49
+ ## Usage
50
+
51
+ <code>
52
+ include ModelTableizer
53
+ </code>
54
+
55
+ If you are going to be using this with Rails, in your application controller add the above include line. Then in a controller method make a call such as:
56
+
57
+ ```ruby
58
+ @user_table = table users, :id => 'list_of_users' do
59
+ column :name, :id => 'blah_id'
60
+ column :email
61
+ column :created_at
62
+ column "CRM Status", :id => 'from_crm' do
63
+ "<a href='#{user.beta_request}'>#{user.beta_request}</a>" if user.beta_request
64
+ end
65
+ end
66
+ ```
67
+
68
+ Then in your view you can access
69
+
70
+ <code>
71
+ <% raw(@user_table) %>
72
+ </code>
73
+
74
+ ## Contributing
75
+
76
+ 1. Fork it
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
79
+ 4. Push to the branch (`git push origin my-new-feature`)
80
+ 5. Create new Pull Request
81
+
82
+ ## License
83
+ GPLv3: http://www.gnu.org/licenses/gpl.html
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/lib/hash.rb ADDED
@@ -0,0 +1,9 @@
1
+ class Hash
2
+ def attr_str
3
+ res = ''
4
+ self.each_pair { |attr, value|
5
+ res += " #{attr} = '#{value}'"
6
+ }
7
+ res
8
+ end
9
+ end
@@ -0,0 +1,57 @@
1
+ # Authod: Cliff Cyphers
2
+ # Licesnse: GPLv3: http://www.gnu.org/licenses/gpl.html
3
+
4
+ base = File.expand_path(File.dirname(__FILE__))
5
+ require base + '/../models/user'
6
+ require base + '/string'
7
+ require base + '/hash'
8
+
9
+
10
+ module ModelTableizer
11
+ def column(name, params={}, &blk)
12
+ content = ''
13
+ if block_given?
14
+ content = blk.call
15
+ else
16
+ if @item.respond_to?(name)
17
+ content = @item.send(name)
18
+ end
19
+ end
20
+
21
+ tmp="<td name = '#{name}'"
22
+ tmp += params.attr_str
23
+ tmp += ">#{content}</td>"
24
+ @columns << tmp
25
+ unless @header.include?(name)
26
+ @header << name
27
+ @header_params[@header.length-1] = params.attr_str
28
+ end
29
+ end
30
+
31
+ def table(items=[], params={}, &blk)
32
+ params[:id] ||= Time.now
33
+ row_html = ""
34
+ if block_given?
35
+ @header = []
36
+ @header_params = []
37
+ html = "<table id='#{params[:id]}'>"
38
+ items.each_with_index { |item, ct|
39
+ row_html += "<tr>"
40
+ @columns = []
41
+ @item = item
42
+ instance_variable_set("@#{item.class.to_s.downcase}", item)
43
+ define_me(item.class.to_s.downcase) { item }
44
+ blk.call
45
+ @columns.each { |col| row_html += col }
46
+ row_html += "</tr>"
47
+ }
48
+ head = "<thead>"
49
+ @header.each_with_index { |col, ct|
50
+ head += "<th class='#{col}' #{@header_params[ct]} >#{col.to_s.to_camel}</th>" }
51
+ head += "</thead>"
52
+
53
+ "#{html}#{head}#{row_html}</table>"
54
+
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ module ModelTableizer
2
+ VERSION = "0.0.1"
3
+ end
data/lib/string.rb ADDED
@@ -0,0 +1,9 @@
1
+ class String
2
+ def to_camel
3
+ spaced=self.gsub(/_/, " ")
4
+ tmp = spaced.split
5
+ ret=''
6
+ tmp.each { |word| ret += " #{word.capitalize}" }
7
+ ret.strip
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/model_tableizer/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Cliff Cyphers"]
6
+ gem.email = ["cliff.cyphers@gmail.com"]
7
+ gem.description = %q{HTML Table for ruby object connections}
8
+ gem.summary = %q{}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "model_tableizer"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ModelTableizer::VERSION
17
+ end
data/models/user.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'platform_helpers'
2
+ class User
3
+ def initialize(params={})
4
+ inject(self, params)
5
+ end
6
+
7
+ private
8
+ def inject(obj, params)
9
+ params.each_pair { |k, v|
10
+ obj.instance_variable_set("@#{k.to_s}", v)
11
+ define_me(k) { v }
12
+ }
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ base = File.expand_path(File.dirname(__FILE__))
2
+ require base + '/../../models/user'
3
+
4
+ describe User do
5
+ it "should have accessor instance variables when symbol keys" do
6
+ params = { :blah1 => 'blah1', :blah2 => 'blah2', :blah3 => 'blah3' }
7
+ u = User.new(params)
8
+ params.each_pair { |k, v| u.send(k).should == v }
9
+ end
10
+
11
+ it "should have accessor instance variables when string keys" do
12
+ params = { 'blah1' => 'blah1', 'blah2' => 'blah2', 'blah3' => 'blah3' }
13
+ u = User.new(params)
14
+ params.each_pair { |k, v| u.send(k).should == v }
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: model_tableizer
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Cliff Cyphers
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-04-06 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: HTML Table for ruby object connections
22
+ email:
23
+ - cliff.cyphers@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - LICENSE
34
+ - README.md
35
+ - Rakefile
36
+ - lib/hash.rb
37
+ - lib/model_tableizer.rb
38
+ - lib/model_tableizer/version.rb
39
+ - lib/string.rb
40
+ - model_tableizer.gemspec
41
+ - models/user.rb
42
+ - test/spec/obj_instance_access.rb
43
+ has_rdoc: true
44
+ homepage: ""
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: ""
75
+ test_files:
76
+ - test/spec/obj_instance_access.rb