sal 0.2.0 → 0.2.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.
- data/README.md +43 -1
- data/lib/sal/compiler.rb +8 -12
- data/lib/sal/parser.rb +0 -18
- data/lib/sal/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -44,7 +44,7 @@ As you've no doubt noticed, the data-sal attribute is gone and the contents have
|
|
44
44
|
|
45
45
|
|
46
46
|
#### Hash
|
47
|
-
The next return value sal knows about is a hash. With the exception of an :html key, sal simply treats each the keys as attributes to be added to the tag. The value of the html key becomes the value of the tag.
|
47
|
+
The next return value sal knows about is a hash. With the exception of an :html key, sal simply treats each of the keys as attributes to be added to the tag. The value of the html key becomes the value of the tag.
|
48
48
|
|
49
49
|
Lets say there's a method called login_link:
|
50
50
|
|
@@ -64,6 +64,48 @@ If logged_in is false you'll get:
|
|
64
64
|
|
65
65
|
<a href='/login'>Log in</a>
|
66
66
|
|
67
|
+
#### Object
|
68
|
+
For Rails users, you will probably need to access objects like an ActiveRecord model in your view. To simplify this, you can have tags scoped to an object.
|
69
|
+
|
70
|
+
For example, you may have a user detail page:
|
71
|
+
|
72
|
+
<div data-sal="current_user">
|
73
|
+
<h1>Welcome <span data-sal="full_name">Awesome user</span></h1>
|
74
|
+
<form method="post" action="/account">
|
75
|
+
<input data-sal="email_input" type="text"/>
|
76
|
+
...
|
77
|
+
</form>
|
78
|
+
</div>
|
79
|
+
|
80
|
+
Since this will probably be a common scenario you probably don't want to write an input method for each attribute, so lets use method_missing:
|
81
|
+
|
82
|
+
def method_missing(meth)
|
83
|
+
meth = meth.to_s
|
84
|
+
if meth =~ /_input$/
|
85
|
+
name = meth.split('_').first
|
86
|
+
klass = self.class.name.downcase
|
87
|
+
{:name => "#{klass}[#{name}]", :id => "#{klass}_#{name}", :value => send(name)}
|
88
|
+
else
|
89
|
+
super
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
This will give you the Rails-y format for input tags. For example, if current_user returns a user object where:
|
94
|
+
|
95
|
+
class User < ActiveRecord::Base
|
96
|
+
def full_name
|
97
|
+
first_name + " " + last_name
|
98
|
+
end
|
99
|
+
def
|
100
|
+
|
101
|
+
<!-- first_name = 'John', last_name = 'Smith, email = 'jsmith@test.com' -->
|
102
|
+
<div>
|
103
|
+
<h1>Welcome <span>John Smith</span></h1>
|
104
|
+
<form method="post" action="/account">
|
105
|
+
<input name="user[email]" id="user_email" value="jsmith@test.com" type="text"/>
|
106
|
+
...
|
107
|
+
</form>
|
108
|
+
</div>
|
67
109
|
|
68
110
|
#### Array
|
69
111
|
If the return value is an array, sal repeats the markup for each item in the array applying the contents accordingly.
|
data/lib/sal/compiler.rb
CHANGED
@@ -15,26 +15,22 @@ module Sal
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
def on_sal_nested(tag, attrs, content)
|
19
|
-
[:html, :tag, tag, ada(attrs, '_saldict'), false, compile(content)]
|
20
|
-
end
|
21
|
-
|
22
18
|
def on_sal_code(code, tag, attrs, content)
|
23
19
|
tmp1, tmp2 = tmp_var(:sal), tmp_var(:sal)
|
24
|
-
|
20
|
+
content = compile(content)
|
25
21
|
[:multi,
|
26
22
|
[:block, "if (#{tmp1} = _saldict['#{code}'])"],
|
23
|
+
[:block, "#{tmp2} = _saldict"],
|
24
|
+
[:block, "_saldict = #{tmp1}"],
|
27
25
|
[:block, "case (#{tmp1})"],
|
28
26
|
[:block, "when Array"],
|
29
|
-
[:block, "#{tmp2} = _saldict"],
|
30
27
|
[:block, "#{tmp1}.each do |_saldict|"],
|
31
|
-
|
28
|
+
[:html, :tag, tag, ada(attrs), false, content],
|
32
29
|
[:block, 'end'],
|
33
|
-
[:block, "_saldict = #{tmp2}"],
|
34
30
|
[:block, "else"],
|
35
|
-
[:html, :tag, tag, ada(attrs
|
36
|
-
[:dynamic, "Sal::U.parse_for_html(#{tmp1})"]],
|
31
|
+
[:html, :tag, tag, ada(attrs), false, content],
|
37
32
|
[:block, 'end'],
|
33
|
+
[:block, "_saldict = #{tmp2}"],
|
38
34
|
[:block, 'end'],
|
39
35
|
]
|
40
36
|
end
|
@@ -47,8 +43,8 @@ module Sal
|
|
47
43
|
[:multi, [:html, :staticattrs] + a]
|
48
44
|
end
|
49
45
|
|
50
|
-
def ada(attrs
|
51
|
-
[:dynamic, "Sal::U.parse_for_attributes(
|
46
|
+
def ada(attrs)
|
47
|
+
[:dynamic, "Sal::U.parse_for_attributes(_saldict, #{attrs.inspect})"]
|
52
48
|
end
|
53
49
|
end
|
54
50
|
end
|
data/lib/sal/parser.rb
CHANGED
@@ -7,22 +7,6 @@ module Sal
|
|
7
7
|
set_default_options :tabsize => 4,
|
8
8
|
:encoding => 'utf-8'
|
9
9
|
|
10
|
-
class << self
|
11
|
-
#TODO: Remove node_types after fleshing out tests
|
12
|
-
def node_types
|
13
|
-
@node_types ||= define_node_types
|
14
|
-
end
|
15
|
-
|
16
|
-
private
|
17
|
-
def define_node_types
|
18
|
-
nt = {}
|
19
|
-
Nokogiri::XML::Node.constants.each do |name|
|
20
|
-
nt[eval("Nokogiri::XML::Node::#{name}")] = name if name =~ /_NODE$/
|
21
|
-
end
|
22
|
-
nt
|
23
|
-
end
|
24
|
-
end # class block
|
25
|
-
|
26
10
|
def initialize(options = {})
|
27
11
|
super
|
28
12
|
@tab = ' ' * @options[:tabsize]
|
@@ -82,8 +66,6 @@ module Sal
|
|
82
66
|
stacks.last << [:static, node.to_s]
|
83
67
|
when Nokogiri::XML::Node::COMMENT_NODE
|
84
68
|
stacks.last << [:html, :comment, [:static, node.text]]
|
85
|
-
else
|
86
|
-
raise "Unexpected node type: #{self.class.node_types[node.node_type]}"
|
87
69
|
end
|
88
70
|
end
|
89
71
|
end
|
data/lib/sal/version.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: sal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.2.
|
5
|
+
version: 0.2.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Andrew Stone
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-04-
|
13
|
+
date: 2011-04-27 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|