th_simple_data_presentation 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NmYyOTExNDlkZjhkNzg1MmFhMjlhYWY5MGRkYzY3NjdhYjI4NDc0OQ==
5
+ data.tar.gz: !binary |-
6
+ YmZkYTQ0MjhiNjNmMTNkNzQyYTA3NDY1NGYyMGUzOGQ4NWRkZWFmMA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NTJhYWYyNTNhZmRkMTI2NmM4ZDU4Mjk2Yjg1OWM5YzM2ZDVmMzVkMTM0ZmU0
10
+ N2IzZjYxMWU2NjljOTRlOGJhNTRiMTk2Mjc4MTYyZmVhOTg3ZjI1NjE3Yzdj
11
+ NGIxM2IwNThhZDk3OGMyZjZkNjMxZTE1YWRhYjNkOGQ3MGQyZjc=
12
+ data.tar.gz: !binary |-
13
+ MGZmOWM2MjQ5MzEyOTI5OWNjYjQyNGY5NjU3MTc3YTEwNmI0MjAxNmI3ZmJk
14
+ OTRjZDk5YWFlNjFhMTUwZjk2NGMzNGViMzZlMzA2NjZhOWI4OTA0OWQwMmRi
15
+ MTBmNDEyNWE5YmM1ZDBkOTAyNDg2ODJmZGZlOGJkMzVkOTY0NTM=
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
18
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in th_simple_data_presentation.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Toby Hinloopen
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,29 @@
1
+ # ThSimpleDataPresentation
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'th_simple_data_presentation'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install th_simple_data_presentation
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ module SimpleDataPresentation
2
+ require "simple_data_presentation/html_options"
3
+ require "simple_data_presentation/simple_data_list"
4
+ require "simple_data_presentation/simple_table"
5
+ require "simple_data_presentation/version"
6
+ end
@@ -0,0 +1,12 @@
1
+ class SimpleDataPresentation::HtmlOptions < HashWithIndifferentAccess
2
+ def regular_writer(key, value)
3
+ super key, case key
4
+ when "class"
5
+ value.nil? ? nil : (self[key].present? ? self[key].to_s << " #{value}" : "#{value}")
6
+ when "data"
7
+ value.is_a?(Hash) ? (self[key].is_a?(Hash) ? self[key].merge(value) : value.with_indifferent_access) : value
8
+ else
9
+ "#{value}"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module SimpleDataPresentation::SimpleDataList
2
+ require "simple_data_presentation/html_options"
3
+ require "simple_data_presentation/simple_data_list/base"
4
+ require "simple_data_presentation/simple_data_list/helper"
5
+ end
@@ -0,0 +1,63 @@
1
+ class SimpleDataPresentation::SimpleDataList::Base
2
+ def initialize(context, collection, options = {}, &content_block)
3
+ @context = context
4
+ @collection = collection
5
+ @options = options
6
+ @content_block = content_block
7
+ @resource_class = @options.delete(:resource_class) || @collection.first.class
8
+ @html_options = SimpleDataPresentation::HtmlOptions.new(@options.delete(:html) || {})
9
+ @html_options[:class] = "list"
10
+ end
11
+
12
+ def item(*args, &block)
13
+ self.title(*args.clone) + self.value(*args.clone, &block)
14
+ end
15
+
16
+ def title(*args, &block)
17
+ options = args.extract_options!
18
+ content = if block.present?
19
+ @context.capture(@resource_class, &block)
20
+ elsif args.any?
21
+ if options[:label].is_a?(Symbol)
22
+ @resource_class.human_attribute_name options[:label]
23
+ elsif !options[:label].nil?
24
+ options[:label]
25
+ else
26
+ @resource_class.human_attribute_name args.join("_").to_sym
27
+ end
28
+ end
29
+ @context.content_tag :dt, content, options[:html]
30
+ end
31
+
32
+ def value(*args, &block)
33
+ options = args.extract_options!
34
+ if block.present?
35
+ content = @context.capture @resource, &block
36
+ else
37
+ content = value_for_method_chain *args
38
+ as_formatter = options[:as]
39
+ if as_formatter.present?
40
+ content = if as_formatter.respond_to?(:call)
41
+ case as_formatter.arity
42
+ when 1; as_formatter.call content
43
+ when 2; as_formatter.call @resource, content
44
+ end
45
+ elsif as_formatter.is_a?(Symbol) && @context.respond_to?(as_formatter)
46
+ @context.public_send as_formatter, content if !content.nil?
47
+ end
48
+ end
49
+ end
50
+ @context.content_tag :td, content, options[:html]
51
+ end
52
+
53
+ def render!
54
+ @context.raw @context.content_tag :dl, @context.capture(self, &@content_block), @html_options[:html]
55
+ end
56
+
57
+ protected
58
+ def value_for_method_chain(*args)
59
+ value = @resource
60
+ value = value.try args.shift while !args.empty?
61
+ value
62
+ end
63
+ end
@@ -0,0 +1,5 @@
1
+ module SimpleDataPresentation::SimpleDataList::Helper
2
+ def data_list_for(collection, options = {}, &content_block)
3
+ SimpleDataPresentation::SimpleDataList::Base.new(self, collection, options, &content_block).render!
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ module SimpleDataPresentation::SimpleTable
2
+ require "simple_data_presentation/html_options"
3
+ require "simple_data_presentation/simple_table/base"
4
+ require "simple_data_presentation/simple_table/head"
5
+ require "simple_data_presentation/simple_table/body"
6
+ require "simple_data_presentation/simple_table/row"
7
+ require "simple_data_presentation/simple_table/helper"
8
+ end
@@ -0,0 +1,29 @@
1
+ class SimpleDataPresentation::SimpleTable::Base
2
+ def initialize(context, collection, options = {}, &content_block)
3
+ @context = context
4
+ @collection = collection
5
+ @options = options
6
+ @content_block = content_block
7
+ @resource_class = @options.delete(:resource_class) || @collection.first.class
8
+ @html_options = SimpleDataPresentation::HtmlOptions.new(@options.delete(:html) || {})
9
+ @html_options[:class] = "list"
10
+ end
11
+
12
+ def item(*args, &block)
13
+ self.title(*args.clone) || self.value(*args.clone, &block)
14
+ end
15
+
16
+ def title(*args, &block)
17
+ nil
18
+ end
19
+
20
+ def value(*args, &block)
21
+ nil
22
+ end
23
+
24
+ def render!
25
+ thead = SimpleDataPresentation::SimpleTable::Head.new @context, @resource_class, @options.clone, &@content_block
26
+ tbody = SimpleDataPresentation::SimpleTable::Body.new @context, @collection, @options.clone, &@content_block
27
+ @context.raw @context.content_tag :table, thead.render! + tbody.render!, @html_options
28
+ end
29
+ end
@@ -0,0 +1,18 @@
1
+ class SimpleDataPresentation::SimpleTable::Body < SimpleDataPresentation::SimpleTable::Base
2
+ def initialize(context, collection, options = {}, &content_block)
3
+ @context = context
4
+ @collection = collection
5
+ @options = options
6
+ @content_block = content_block
7
+ end
8
+
9
+ def render!
10
+ html_options = @options.delete :tbody_html
11
+ row_options = @options.merge :html => @options.delete(:row_html)
12
+ rows_content = @collection.map do |resource|
13
+ tr = SimpleDataPresentation::SimpleTable::Row.new @context, resource, row_options, &@content_block
14
+ tr.render!
15
+ end
16
+ @context.raw @context.content_tag :tbody, rows_content.join.html_safe, html_options
17
+ end
18
+ end
@@ -0,0 +1,32 @@
1
+ class SimpleDataPresentation::SimpleTable::Head < SimpleDataPresentation::SimpleTable::Base
2
+ def initialize(context, resource_class, options = {}, &content_block)
3
+ @context = context
4
+ @options = options
5
+ @resource_class = resource_class
6
+ @search = @context.instance_variable_get(:"@search") if @context.instance_variable_get(:"@search").present? && @context.instance_variable_get(:"@search").base == @resource_class
7
+ @content_block = content_block
8
+ end
9
+
10
+ def title(*args, &block)
11
+ options = args.extract_options!
12
+ content = if block.present?
13
+ @context.capture(@resource_class, &block)
14
+ elsif args.any?
15
+ sort_attribute = args.join("_").to_sym
16
+ label = if options[:label].is_a?(Symbol)
17
+ @resource_class.human_attribute_name options[:label]
18
+ elsif !options[:label].nil?
19
+ options[:label]
20
+ else
21
+ @resource_class.human_attribute_name sort_attribute
22
+ end
23
+ !@search.nil? && options[:sortable] != false ? @context.sort_link(@search, sort_attribute, label) : label
24
+ end
25
+ @context.content_tag :th, content, options[:html]
26
+ end
27
+
28
+ def render!
29
+ content = @context.capture self, &@content_block
30
+ @context.raw @context.content_tag :thead, @context.content_tag(:tr, content, (@options[:row_html])), @options[:html]
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ module SimpleDataPresentation::SimpleTable::Helper
2
+ def table_list_for(collection, options = {}, &content_block)
3
+ SimpleDataPresentation::SimpleTable::Base.new(self, collection, options, &content_block).render!
4
+ end
5
+ end
@@ -0,0 +1,41 @@
1
+ class SimpleDataPresentation::SimpleTable::Row < SimpleDataPresentation::SimpleTable::Base
2
+ def initialize(context, resource, options = {}, &content_block)
3
+ @context = context
4
+ @resource = resource
5
+ @options = options
6
+ @content_block = content_block
7
+ end
8
+
9
+ def value(*args, &block)
10
+ options = args.extract_options!
11
+ if block.present?
12
+ content = @context.capture @resource, &block
13
+ else
14
+ content = value_for_method_chain *args
15
+ as_formatter = options[:as]
16
+ if as_formatter.present?
17
+ content = if as_formatter.respond_to?(:call)
18
+ case as_formatter.arity
19
+ when 1; as_formatter.call content
20
+ when 2; as_formatter.call @resource, content
21
+ end
22
+ elsif as_formatter.is_a?(Symbol) && @context.respond_to?(as_formatter)
23
+ @context.public_send as_formatter, content if !content.nil?
24
+ end
25
+ end
26
+ end
27
+ @context.content_tag :td, content, options[:html]
28
+ end
29
+
30
+ def render!
31
+ content = @context.capture self, &@content_block
32
+ @context.raw @context.content_tag :tr, content.html_safe, @options[:html]
33
+ end
34
+
35
+ protected
36
+ def value_for_method_chain(*args)
37
+ value = @resource
38
+ value = value.try args.shift while !args.empty?
39
+ value
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleDataPresentation
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/simple_data_presentation/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Toby Hinloopen"]
6
+ gem.email = ["toby@kutcomputers.nl"]
7
+ gem.description = %q{Collection of visual helpers}
8
+ gem.summary = %q{Collection of visual helpers}
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 = "th_simple_data_presentation"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = SimpleDataPresentation::VERSION
17
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: th_simple_data_presentation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Toby Hinloopen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Collection of visual helpers
14
+ email:
15
+ - toby@kutcomputers.nl
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - .gitignore
21
+ - Gemfile
22
+ - LICENSE
23
+ - README.md
24
+ - Rakefile
25
+ - lib/simple_data_presentation.rb
26
+ - lib/simple_data_presentation/html_options.rb
27
+ - lib/simple_data_presentation/simple_data_list.rb
28
+ - lib/simple_data_presentation/simple_data_list/base.rb
29
+ - lib/simple_data_presentation/simple_data_list/helper.rb
30
+ - lib/simple_data_presentation/simple_table.rb
31
+ - lib/simple_data_presentation/simple_table/base.rb
32
+ - lib/simple_data_presentation/simple_table/body.rb
33
+ - lib/simple_data_presentation/simple_table/head.rb
34
+ - lib/simple_data_presentation/simple_table/helper.rb
35
+ - lib/simple_data_presentation/simple_table/row.rb
36
+ - lib/simple_data_presentation/version.rb
37
+ - simple_data_presentation.gemspec
38
+ homepage: ''
39
+ licenses: []
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.0.3
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Collection of visual helpers
61
+ test_files: []