slim 0.7.4 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,4 +11,4 @@ require 'slim/compiler'
11
11
  require 'slim/engine'
12
12
  require 'slim/template'
13
13
  require 'slim/version'
14
-
14
+ require 'slim/wrapper'
@@ -5,12 +5,12 @@ module Slim
5
5
  class Sections < Filter
6
6
  set_default_options :dictionary => 'self',
7
7
  :sections => false,
8
- :dictionary_access => :symbol # :symbol, :string
8
+ :dictionary_access => :wrapped # :symbol, :string, :wrapped
9
9
 
10
10
  def initialize(opts = {})
11
11
  super
12
- unless [:string, :symbol].include?(@options[:dictionary_access])
13
- raise "Invalid dictionary access #{@options[:dictionary_access].inspect}"
12
+ unless [:string, :symbol, :wrapped].include?(options[:dictionary_access])
13
+ raise "Invalid dictionary access #{options[:dictionary_access].inspect}"
14
14
  end
15
15
  end
16
16
 
@@ -18,7 +18,7 @@ module Slim
18
18
  if options[:sections]
19
19
  # Store the dictionary in the _slimdict variable
20
20
  [:multi,
21
- [:block, "_slimdict = #{@options[:dictionary]}"],
21
+ [:block, "_slimdict = #{dictionary}"],
22
22
  super]
23
23
  else
24
24
  exp
@@ -69,11 +69,20 @@ module Slim
69
69
 
70
70
  def access(name)
71
71
  case options[:dictionary_access]
72
- when :symbol
73
- "_slimdict[#{name.to_sym.inspect}]"
74
72
  when :string
75
73
  "_slimdict[#{name.to_s.inspect}]"
74
+ else
75
+ "_slimdict[#{name.to_sym.inspect}]"
76
+ end
77
+ end
78
+
79
+ def dictionary
80
+ if options[:dictionary_access] == :wrapped
81
+ "Slim::Wrapper.new(#{options[:dictionary]})"
82
+ else
83
+ options[:dictionary]
76
84
  end
77
85
  end
86
+
78
87
  end
79
88
  end
@@ -1,3 +1,3 @@
1
1
  module Slim
2
- VERSION = '0.7.4'
2
+ VERSION = '0.8.0'
3
3
  end
@@ -0,0 +1,62 @@
1
+ module Slim
2
+ # For logic-less mode, objects can be encased in the Wrapper class.
3
+ #
4
+ # For Rails, this allows us to use the environment provided for rendering
5
+ # a view including the instance variables, view helper and application helper
6
+ # methods.
7
+ class Wrapper
8
+ attr_reader :value, :parent
9
+
10
+ def initialize(value, parent = nil)
11
+ @value, @parent = value, parent
12
+ end
13
+
14
+ # To find the reference, first check for standard method
15
+ # access by using respond_to?.
16
+ #
17
+ # If not found, check to see if the value is a hash and if the
18
+ # the name is a key on the hash.
19
+ #
20
+ # Not a hash, or not a key on the hash, then check to see if there
21
+ # is an instance variable with the name.
22
+ #
23
+ # If the instance variable doesn't exist and there is a parent object,
24
+ # go through the same steps on the parent object. This is useful when
25
+ # you are iterating over objects.
26
+ def [](name)
27
+ if value.respond_to?(name)
28
+ wrap value.send(name)
29
+ elsif value.respond_to?(:has_key?) && value.has_key?(name.to_sym)
30
+ wrap value[name]
31
+ elsif value.instance_variable_defined?("@#{name}")
32
+ wrap value.instance_variable_get("@#{name}")
33
+ elsif parent
34
+ parent[name]
35
+ end
36
+ end
37
+
38
+ # Empty objects must appear empty for inverted sections
39
+ def empty?
40
+ value.respond_to?(:empty) && value.empty?
41
+ end
42
+
43
+ # Used for output
44
+ def to_s
45
+ value.to_s
46
+ end
47
+
48
+ private
49
+
50
+ def wrap(response)
51
+ # Primitives are not wrapped
52
+ if [String, Numeric, TrueClass, FalseClass, NilClass].any? {|primitive| primitive === response }
53
+ response
54
+ # Enumerables are mapped with wrapped values (except Hash-like objects)
55
+ elsif !response.respond_to?(:has_key?) && response.respond_to?(:map)
56
+ response.map {|v| wrap(v) }
57
+ else
58
+ Wrapper.new(response, self)
59
+ end
60
+ end
61
+ end
62
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 7
8
- - 4
9
- version: 0.7.4
7
+ - 8
8
+ - 0
9
+ version: 0.8.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Andrew Stone
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-11-22 00:00:00 -05:00
19
+ date: 2010-11-29 00:00:00 -05:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -168,6 +168,7 @@ files:
168
168
  - lib/slim/sections.rb
169
169
  - lib/slim/template.rb
170
170
  - lib/slim/version.rb
171
+ - lib/slim/wrapper.rb
171
172
  has_rdoc: true
172
173
  homepage: http://github.com/stonean/slim
173
174
  licenses: []