forma 0.0.0

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.
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/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ script: rspec spec
2
+ language: ruby
3
+ rvm:
4
+ - 1.9.3
5
+ - rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in forma.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Dimitri Kurashvili
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,3 @@
1
+ # Forma
2
+
3
+ [![Build Status](https://travis-ci.org/dimakura/forma.png?branch=master)](https://travis-ci.org/dimakura/forma)
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'less'
3
+
4
+ LESS_PATH = 'vendor/less'
5
+ CSS_PATH = 'vendor/assets/stylesheets'
6
+
7
+ def less_to_css(file)
8
+ less_file = "#{LESS_PATH}/#{file}.less"
9
+ rcss_file = "#{CSS_PATH}/#{file}.css"
10
+ mcss_file = "#{CSS_PATH}/#{file}-min.css"
11
+ rf = File.new(rcss_file, File::CREAT|File::TRUNC|File::RDWR, 0644)
12
+ mf = File.new(mcss_file, File::CREAT|File::TRUNC|File::RDWR, 0644)
13
+ parser = Less::Parser.new
14
+ tree = parser.parse(File.new(less_file).read)
15
+ rf.write tree.to_css; rf.flush
16
+ mf.write tree.to_css(compress: true); mf.flush
17
+ end
18
+
19
+ task :less do
20
+ less_to_css('forma')
21
+ end
data/forma.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'forma/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'forma'
8
+ spec.version = Forma::VERSION
9
+ spec.authors = ['Dimitri Kurashvili']
10
+ spec.email = ['dimitri@c12.ge']
11
+ spec.description = %q{killer forms for ruby}
12
+ spec.summary = %q{highly informative and flexible forms with ruby}
13
+ spec.homepage = 'http://github.com/dimakura/forma'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ # development dependencies
22
+ spec.add_development_dependency 'bundler', '~> 1.3'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'rspec', '~> 2'
25
+ spec.add_development_dependency 'therubyracer', '~> 0.11'
26
+ spec.add_development_dependency 'less', '~> 2'
27
+
28
+ # runtime dependencies
29
+ spec.add_dependency 'railties', '>= 3.1'
30
+ spec.add_dependency 'activesupport'
31
+ end
@@ -0,0 +1,104 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Forma::Html
3
+
4
+ class Attributes
5
+
6
+ def initialize(attrs = {})
7
+ self.update_attributes(attrs) if attrs.present?
8
+ end
9
+
10
+ def update_attributes(attrs)
11
+ attrs.each { |k, v| self[k] = v }
12
+ end
13
+
14
+ def [](k)
15
+ k = k.to_s
16
+ v = attributes[k.to_s]
17
+ case k
18
+ when 'class' then v = @klass ||= []
19
+ when 'style' then v = @style ||= {}
20
+ end
21
+ v
22
+ end
23
+
24
+ def []=(k,v)
25
+ k = k.to_s
26
+ case k
27
+ when 'class' then assign_class(v)
28
+ when 'style' then assign_style(v)
29
+ else attributes[k] = v
30
+ end
31
+ end
32
+
33
+ def add_class(k)
34
+ klass << k
35
+ end
36
+
37
+ def add_style(k,v)
38
+ style[k] = v
39
+ end
40
+
41
+ def klass
42
+ self[:class]
43
+ end
44
+
45
+ def style
46
+ self[:style]
47
+ end
48
+
49
+ def empty?
50
+ attributes.empty? and klass.empty? and style.empty?
51
+ end
52
+
53
+ def html
54
+ generate_html unless empty?
55
+ end
56
+
57
+ private
58
+
59
+ def attributes
60
+ @attributes ||= {}
61
+ end
62
+
63
+ def assign_class(v)
64
+ if v.is_a? Array
65
+ @klass = v
66
+ elsif v.nil?
67
+ @klass = []
68
+ else
69
+ @klass = [ v ]
70
+ end
71
+ end
72
+
73
+ def assign_style(v)
74
+ raise 'style should be a hash' unless v.is_a? Hash
75
+ @style = v
76
+ end
77
+
78
+ def generate_html
79
+ h = ''
80
+ h << ' ' << generate_class_html unless klass.empty?
81
+ h << ' ' << generate_style_html unless style.empty?
82
+ h << ' ' << generate_attributes_html unless attributes.empty?
83
+ h.strip
84
+ end
85
+
86
+ def generate_class_html
87
+ %Q{class="#{klass.join(' ')}"}
88
+ end
89
+
90
+ def generate_style_html
91
+ st = []
92
+ style.each { |k,v| st << "#{k}:#{v}" }
93
+ %Q{style="#{st.join(';')}"}
94
+ end
95
+
96
+ def generate_attributes_html
97
+ s = []
98
+ attributes.each { |k,v| s << %Q{#{k}="#{v}"} }
99
+ s.join(' ')
100
+ end
101
+
102
+ end
103
+
104
+ end
@@ -0,0 +1,74 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Forma::Html
3
+
4
+ class Element
5
+
6
+ # Text for this element.
7
+ attr_accessor :text
8
+
9
+ # Safe text for this element.
10
+ attr_accessor :safe
11
+
12
+ def initialize(tag, h = {})
13
+ self.tag = tag
14
+ h = h.symbolize_keys
15
+ self.text = h[:text]
16
+ self.safe = h[:safe]
17
+ @attributes = Attributes.new(h[:attrs] || h[:attributes])
18
+ end
19
+
20
+ def tag
21
+ @tag
22
+ end
23
+
24
+ def tag=(t)
25
+ raise "element's tag should be defined" if t.blank?
26
+ @tag = t.to_s.downcase
27
+ end
28
+
29
+ def attributes
30
+ @attributes
31
+ end
32
+
33
+ alias_method :attrs, :attributes
34
+
35
+ def html
36
+ generate_html
37
+ end
38
+
39
+ private
40
+
41
+ def has_inner_content?
42
+ not self.text.blank? or not self.safe.blank?
43
+ end
44
+
45
+ def generate_html
46
+ h = ''
47
+ if has_inner_content?
48
+ h << '<' << generate_tag_and_attributes << '>'
49
+ h << generate_inner_html
50
+ h << '</' << self.tag << '>'
51
+ else
52
+ h << '<' << generate_tag_and_attributes << '/>'
53
+ end
54
+ h
55
+ end
56
+
57
+ def generate_tag_and_attributes
58
+ h = ''
59
+ attrs = @attributes.html
60
+ h << self.tag
61
+ h << ' ' << attrs unless attrs.blank?
62
+ h
63
+ end
64
+
65
+ def generate_inner_html
66
+ h = ''
67
+ h << ERB::Util.html_escape(self.text) unless self.text.blank?
68
+ h << self.safe unless self.safe.blank?
69
+ h
70
+ end
71
+
72
+ end
73
+
74
+ end
data/lib/forma/html.rb ADDED
@@ -0,0 +1,3 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'forma/html/attributes'
3
+ require 'forma/html/element'
@@ -0,0 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Forma
3
+ VERSION = "0.0.0"
4
+ end
data/lib/forma.rb ADDED
@@ -0,0 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'active_support/core_ext'
3
+ require 'forma/version'
4
+ require 'forma/html'
@@ -0,0 +1,54 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'spec_helper'
3
+ include Forma::Html
4
+
5
+ def test_attributes_emptiness(attrs, empty = true)
6
+ if empty
7
+ specify { attrs.klass.should be_empty }
8
+ specify { attrs.style.should be_empty }
9
+ specify { attrs.should be_empty }
10
+ specify { attrs.html.should be_nil }
11
+ else
12
+ specify { attrs.should_not be_empty }
13
+ specify { attrs.html.should_not be_nil }
14
+ end
15
+ end
16
+
17
+ describe 'Empty attributes' do
18
+ test_attributes_emptiness(Attributes.new)
19
+ test_attributes_emptiness(Attributes.new(class: []))
20
+ test_attributes_emptiness(Attributes.new(style: {}))
21
+ test_attributes_emptiness(Attributes.new(class: [], style: {}))
22
+ test_attributes_emptiness(Attributes.new(id: '1'), false)
23
+ test_attributes_emptiness(Attributes.new(class: 'myclass'), false)
24
+ test_attributes_emptiness(Attributes.new(style: {'font-size' => '12px'}), false)
25
+ end
26
+
27
+ describe 'Attributes creation' do
28
+ before(:all) do
29
+ @attrs = Attributes.new(id: 'id-1', class: 'myclass', 'data-method' => 'delete', style: { 'font-size' => '10px', 'color' => 'red' })
30
+ @attrs.add_class('important')
31
+ @attrs.add_style('font-weigh', 'bold')
32
+ end
33
+ specify { @attrs.klass.should == [ 'myclass', 'important' ] }
34
+ specify { @attrs.style['font-size'].should == '10px' }
35
+ specify { @attrs.style['color'].should == 'red' }
36
+ specify { @attrs.style['font-weigh'].should == 'bold' }
37
+ end
38
+
39
+ describe 'Html generation' do
40
+ before(:all) do
41
+ @attr1 = Attributes.new(class: 'myclass')
42
+ @attr2 = Attributes.new(class: ['class1', 'class2'])
43
+ @attr3 = Attributes.new(style: {'font-size' => '14px', 'color' => 'red'})
44
+ @attr4 = Attributes.new(class: ['class1', 'class2'], style: {'font-size' => '14px', 'color' => 'red'})
45
+ @attr5 = Attributes.new(id: 'id1', class: ['class1', 'class2'], style: {'font-size' => '14px', 'color' => 'red'})
46
+ @attr6 = Attributes.new(id: 'id1', class: ['class1', 'class2'], style: {'font-size' => '14px', 'color' => 'red'}, 'data-method' => 'delete')
47
+ end
48
+ specify { @attr1.html.should == 'class="myclass"' }
49
+ specify { @attr2.html.should == 'class="class1 class2"' }
50
+ specify { @attr3.html.should == 'style="font-size:14px;color:red"' }
51
+ specify { @attr4.html.should == 'class="class1 class2" style="font-size:14px;color:red"' }
52
+ specify { @attr5.html.should == 'class="class1 class2" style="font-size:14px;color:red" id="id1"' }
53
+ specify { @attr6.html.should == 'class="class1 class2" style="font-size:14px;color:red" id="id1" data-method="delete"' }
54
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'spec_helper'
3
+ include Forma::Html
4
+
5
+ describe 'Element creation' do
6
+ compare_object(Element.new('div'), { tag: 'div', text: nil, safe: nil, html: '<div/>' })
7
+ compare_object(Element.new('SPAN', text: 'some text'), { tag: 'span', text: 'some text', safe: nil, html: '<span>some text</span>' } )
8
+ compare_object(Element.new('div', text: '<a>b</a>'), { html: '<div>&lt;a&gt;b&lt;/a&gt;</div>' })
9
+ compare_object(Element.new('div', safe: '<a>b</a>'), { html: '<div><a>b</a></div>' })
10
+ end
11
+
12
+ describe 'Element html generation' do
13
+ before(:all) do
14
+ @el1 = Element.new('div')
15
+ @el2 = Element.new('div', text: 'text')
16
+ @el3 = Element.new('div', text: 'text', attrs: { class: 'class1' })
17
+ end
18
+ specify { @el1.html.should == '<div/>' }
19
+ specify { @el2.html.should == '<div>text</div>' }
20
+ specify { @el3.html.should == '<div class="class1">text</div>' }
21
+ end
@@ -0,0 +1,13 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'rspec'
3
+ require 'forma'
4
+
5
+ RSpec.configure do |config|
6
+ config.include(RSpec::Matchers)
7
+ end
8
+
9
+ def compare_object(obj, properties)
10
+ properties.each do |k, v|
11
+ specify { obj.send(k).should == v }
12
+ end
13
+ end
@@ -0,0 +1 @@
1
+ .ff{font-size:14px;}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * forma.less
3
+ * ==========
4
+ * (C) 2013 Dimitri Kurashvili
5
+ */
6
+ .ff {
7
+ font-size: 14px;
8
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * forma.less
3
+ * ==========
4
+ * (C) 2013 Dimitri Kurashvili
5
+ */
6
+
7
+ @font-size: 14px;
8
+
9
+ .ff {
10
+ font-size: @font-size;
11
+ }
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: forma
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dimitri Kurashvili
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2'
62
+ - !ruby/object:Gem::Dependency
63
+ name: therubyracer
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '0.11'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '0.11'
78
+ - !ruby/object:Gem::Dependency
79
+ name: less
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '2'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '2'
94
+ - !ruby/object:Gem::Dependency
95
+ name: railties
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '3.1'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '3.1'
110
+ - !ruby/object:Gem::Dependency
111
+ name: activesupport
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: killer forms for ruby
127
+ email:
128
+ - dimitri@c12.ge
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - .rspec
135
+ - .travis.yml
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - forma.gemspec
141
+ - lib/forma.rb
142
+ - lib/forma/html.rb
143
+ - lib/forma/html/attributes.rb
144
+ - lib/forma/html/element.rb
145
+ - lib/forma/version.rb
146
+ - spec/html/attributes_spec.rb
147
+ - spec/html/element_spec.rb
148
+ - spec/spec_helper.rb
149
+ - vendor/assets/stylesheets/forma-min.css
150
+ - vendor/assets/stylesheets/forma.css
151
+ - vendor/less/forma.less
152
+ homepage: http://github.com/dimakura/forma
153
+ licenses:
154
+ - MIT
155
+ post_install_message:
156
+ rdoc_options: []
157
+ require_paths:
158
+ - lib
159
+ required_ruby_version: !ruby/object:Gem::Requirement
160
+ none: false
161
+ requirements:
162
+ - - ! '>='
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ none: false
167
+ requirements:
168
+ - - ! '>='
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ requirements: []
172
+ rubyforge_project:
173
+ rubygems_version: 1.8.24
174
+ signing_key:
175
+ specification_version: 3
176
+ summary: highly informative and flexible forms with ruby
177
+ test_files:
178
+ - spec/html/attributes_spec.rb
179
+ - spec/html/element_spec.rb
180
+ - spec/spec_helper.rb