slim 1.2.2 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,57 +0,0 @@
1
- module Slim
2
- # For logic-less mode, objects can be encased in the Wrapper class.
3
- # @api private
4
- class Wrapper
5
- attr_reader :value, :parent
6
-
7
- def initialize(value, parent = nil)
8
- @value, @parent = value, parent
9
- end
10
-
11
- # To find the reference, first check for standard method
12
- # access by using respond_to?.
13
- #
14
- # If not found, check to see if the value is a hash and if the
15
- # the name is a key on the hash.
16
- #
17
- # Not a hash, or not a key on the hash, then check to see if there
18
- # is an instance variable with the name.
19
- #
20
- # If the instance variable doesn't exist and there is a parent object,
21
- # go through the same steps on the parent object. This is useful when
22
- # you are iterating over objects.
23
- def [](name)
24
- return wrap(value.send(name)) if value.respond_to?(name)
25
- if value.respond_to?(:has_key?)
26
- return wrap(value[name.to_sym]) if value.has_key?(name.to_sym)
27
- return wrap(value[name.to_s]) if value.has_key?(name.to_s)
28
- end
29
- return wrap(value.instance_variable_get("@#{name}")) if value.instance_variable_defined?("@#{name}")
30
- parent[name] if parent
31
- end
32
-
33
- # Empty objects must appear empty for inverted sections
34
- def empty?
35
- value.respond_to?(:empty) && value.empty?
36
- end
37
-
38
- # Used for output
39
- def to_s
40
- value.to_s
41
- end
42
-
43
- private
44
-
45
- def wrap(response)
46
- # Primitives are not wrapped
47
- if [String, Numeric, TrueClass, FalseClass, NilClass].any? {|primitive| primitive === response }
48
- response
49
- # Enumerables are mapped with wrapped values (except Hash-like objects)
50
- elsif !response.respond_to?(:has_key?) && response.respond_to?(:map)
51
- response.map {|v| wrap(v) }
52
- else
53
- Wrapper.new(response, self)
54
- end
55
- end
56
- end
57
- end