sculpt 0.1.02 → 0.1.03

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,6 +10,11 @@ class Tag < ElementContainer
10
10
  @inline = false
11
11
  @text = ''
12
12
 
13
+ set_up(text, attrs, &block)
14
+ end
15
+
16
+ def set_up(text = '', attrs = {}, &block)
17
+ # this method can be called from either the class hack or the init.
13
18
  if text.kind_of? Tag
14
19
  @inline = text
15
20
  @elements << text
@@ -26,7 +31,9 @@ class Tag < ElementContainer
26
31
  if block_given?
27
32
  @elements += elements_from_block(&block)
28
33
  end
29
- @singleton = is_singleton? name
34
+ @singleton = is_singleton? @name
35
+
36
+ self # needed for funky classes
30
37
  end
31
38
 
32
39
  def generate_html(ugly = false)
@@ -49,6 +56,36 @@ class Tag < ElementContainer
49
56
  return "#{open}\n#{html}#{close}" if pp and not inline
50
57
  return open + html + close
51
58
  end
59
+
60
+ def method_missing(m, *args, &block)
61
+ # this is the class hack.
62
+ # it's quite cool.
63
+ # it basically let's you call tag.class and have class be set as the class for tag.
64
+ # it requires a bit of extra work elsewhere (as arguments get passed to this method instead).
65
+ @attrs[:class] = "" unless @attrs[:class]
66
+ @attrs[:class] += " " unless @attrs[:class].empty?
67
+ @attrs[:class] += m.to_s
68
+ case @name
69
+ when :a
70
+ attrs = special_attr(:href, args[1], args[2])
71
+ set_up(args[0], attrs, &block)
72
+ when :img
73
+ attrs = special_attr(:src, args[0], args[1])
74
+ set_up(attrs)
75
+ when :ul,:ol
76
+ t = args[0]
77
+ if t.kind_of? Array or t.kind_of? Range
78
+ t.each do |item|
79
+ @elements << Tag.new(:li,item)
80
+ end
81
+ set_up('',args[1])
82
+ else
83
+ set_up(args[0],args[1], &block)
84
+ end
85
+ else
86
+ set_up(args[0], args[1], &block)
87
+ end
88
+ end
52
89
  end
53
90
 
54
91
  class Static
@@ -1,7 +1,7 @@
1
1
  class Sculpture < ElementContainer
2
2
  #
3
3
  # This is a container class for elements.
4
- # It responds to all the method calls in a block that generates HTMl.
4
+ # It responds to all the method calls in a block that generates HTML.
5
5
  #
6
6
 
7
7
  include SculptHelpers
@@ -47,34 +47,47 @@ class Sculpture < ElementContainer
47
47
  def doctype
48
48
  @elements << Static.new('<!DOCTYPE html>') # enforce HTML5
49
49
  end
50
+
51
+ # multi constructors below.
52
+ # just a warning, multi constructors do NOT support funky classes.
53
+
54
+ private
55
+ def _js(src)
56
+ attrs = {type:"text/javascript",src:src}
57
+ add_tag Tag.new(:script, attrs)
58
+ end
50
59
 
51
60
  def js(*args)
52
- args.each do |arg|
53
- attrs = {type:"text/javascript", src:arg}
54
- add_tag Tag.new(:script, attrs)
61
+ if args[0].kind_of? Array
62
+ args[0].each {|script| _js script }
63
+ else
64
+ args.each {|script| _js script }
55
65
  end
56
66
  end
57
67
 
58
68
  private
59
- def _stylesheet(name)
60
- attrs = {type:"text/css", rel:"stylesheet", href:name}
69
+ def _css(src)
70
+ attrs = {type:"text/css", rel:"stylesheet", href:src}
61
71
  add_tag Tag.new(:link, attrs)
62
72
  end
63
73
 
64
74
  def css(*args)
65
- if args[0].respond_to? :to_a
66
- args[0].each {|sheet| _stylesheet sheet}
75
+ if args[0].kind_of? Array
76
+ args[0].each {|sheet| _css sheet}
77
+ else
78
+ args.each {|sheet| _css sheet}
67
79
  end
68
- args.each {|sheet| _stylesheet sheet}
69
80
  end
81
+
82
+ # special constructors below
70
83
 
71
- def a(text, href = '', ahash = {}, &block)
84
+ def a(text = '', href = '', ahash = {}, &block)
72
85
  # funky constructor for easier linking
73
86
  attrs = special_attr(:href, href, ahash)
74
87
  add_tag Tag.new(:a, text, attrs, &block)
75
88
  end
76
89
 
77
- def img(src, ahash = {})
90
+ def img(src = '', ahash = {})
78
91
  # funky img constructor
79
92
  attrs = special_attr(:src, src, ahash)
80
93
  add_tag Tag.new(:img, attrs)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sculpt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.02
4
+ version: 0.1.03
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -21,7 +21,7 @@ files:
21
21
  - lib/sculpt/sculpt_helpers.rb
22
22
  - lib/sculpt/sculpture.rb
23
23
  - lib/sculpt.rb
24
- homepage: http://github.com/alexcoplan/sculpt
24
+ homepage: http://alexcoplan.co.uk/sculpt
25
25
  licenses: []
26
26
  post_install_message:
27
27
  rdoc_options: []