mystique 0.7.0 → 0.8.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.
- checksums.yaml +4 -4
- data/lib/mystique.rb +8 -3
- data/lib/mystique/presenter.rb +58 -9
- data/lib/mystique/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0c4b41e5e88082dcc7d1409113cfa6fa93e744d181085d1a95d5206eb35c47ca
|
4
|
+
data.tar.gz: ace4c3b4da4d88494a6ae354f79a13d143f3aa7245e5088a2672e95274503ed6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b2f71369a89bc564ccf0633726ffe4bde00dad7fe066c585b346770bd3aabfeb999b33d02cfe253cf6fc8cbb982dec9eded199cc69bd66344dd35d6903db346
|
7
|
+
data.tar.gz: 726e814f716fce77558ba71ae09d3a776803742cfe0b0287e30d1a1a818d6af6dae2c547aa1304b6d813a98fa780c23ff38cb9f8f929ac0bc80ac7fa96b60593
|
data/lib/mystique.rb
CHANGED
@@ -12,7 +12,7 @@ module Mystique
|
|
12
12
|
def present(object, with: nil, context: nil, &block)
|
13
13
|
begin
|
14
14
|
presenter_class = presenter_class_for(object, with)
|
15
|
-
presenter_class.
|
15
|
+
presenter_class.for(object, context, &block)
|
16
16
|
rescue NameError
|
17
17
|
return object
|
18
18
|
end
|
@@ -22,12 +22,17 @@ module Mystique
|
|
22
22
|
return to_enum(:present_collection, collection, context: context, &block) unless block_given?
|
23
23
|
|
24
24
|
collection.each do |element|
|
25
|
-
block.
|
25
|
+
case block.arity
|
26
|
+
when 2
|
27
|
+
block.call( present(element, context: context), element )
|
28
|
+
else
|
29
|
+
block.call(present(element, context: context))
|
30
|
+
end
|
26
31
|
end
|
27
32
|
end
|
28
33
|
|
29
34
|
def presenter_class_for(object, with)
|
30
|
-
if with.respond_to?(:
|
35
|
+
if with.respond_to?(:for)
|
31
36
|
with
|
32
37
|
else
|
33
38
|
StringPlus.constantize("#{with || object.class}Presenter")
|
data/lib/mystique/presenter.rb
CHANGED
@@ -7,7 +7,7 @@ module Mystique
|
|
7
7
|
@__context__ = context || self.class.context || NullContext
|
8
8
|
end
|
9
9
|
|
10
|
-
def self.
|
10
|
+
def self.for(object, context=nil)
|
11
11
|
self.new(object, context).tap do |presenter|
|
12
12
|
yield presenter if block_given?
|
13
13
|
end
|
@@ -31,21 +31,37 @@ module Mystique
|
|
31
31
|
|
32
32
|
def method_missing(method, *args, &block)
|
33
33
|
return target.send(method, *args, &block) if method.to_s.start_with?("to_")
|
34
|
-
|
34
|
+
|
35
|
+
value = target.send(method, *args, &block)
|
36
|
+
|
37
|
+
case
|
38
|
+
when formatted_method?(method)
|
39
|
+
format( value )
|
40
|
+
when presented_method?(method)
|
41
|
+
Mystique.present(value, context: context)
|
42
|
+
else
|
43
|
+
value
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def formatted_method?(method)
|
48
|
+
__formatted_methods__.include?(method)
|
49
|
+
end
|
50
|
+
|
51
|
+
def presented_method?(method)
|
52
|
+
__presented_methods__.include?(method)
|
35
53
|
end
|
36
54
|
|
37
55
|
def format(value)
|
38
|
-
result =
|
39
|
-
when __formats__.keys.include?(value)
|
56
|
+
result = if __formats__.keys.include?(value)
|
40
57
|
__formats__[value]
|
41
|
-
|
58
|
+
elsif __regex_formats__.any? { |regex, _| value =~ regex}
|
42
59
|
__regex_formats__.select { |regex, _| value =~ regex}.first.last
|
43
|
-
|
60
|
+
elsif __class_formats__.any? { |klass, _| value.is_a?(klass)}
|
44
61
|
__class_formats__.select { |klass, _| value.is_a?(klass)}.first.last
|
45
62
|
else
|
46
63
|
value
|
47
64
|
end
|
48
|
-
|
49
65
|
Mystique.present(Callable(result).call(value, context))
|
50
66
|
end
|
51
67
|
|
@@ -54,13 +70,46 @@ module Mystique
|
|
54
70
|
@__context__
|
55
71
|
end
|
56
72
|
|
57
|
-
def self.
|
73
|
+
def self.apply_format(matcher, value=nil, &block)
|
58
74
|
__formats__[matcher] = block_given? ? block : value
|
59
75
|
end
|
60
76
|
|
77
|
+
def self.format(matcher)
|
78
|
+
if matcher.is_a?(Symbol)
|
79
|
+
__formatted_methods__ << matcher
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.present(matcher)
|
84
|
+
if matcher.is_a?(Symbol)
|
85
|
+
__presented_methods__ << matcher
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.format_and_present(matcher)
|
90
|
+
format_method(method)
|
91
|
+
present_method(method)
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.__presented_methods__
|
95
|
+
@__presented_methods__ ||= []
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.__formatted_methods__
|
99
|
+
@__formatted_methods__ ||= []
|
100
|
+
end
|
101
|
+
|
102
|
+
def __presented_methods__
|
103
|
+
self.class.__presented_methods__
|
104
|
+
end
|
105
|
+
|
106
|
+
def __formatted_methods__
|
107
|
+
self.class.__formatted_methods__
|
108
|
+
end
|
109
|
+
|
61
110
|
def self.format_multiple(*matchers, &block)
|
62
111
|
matchers.each do |matcher|
|
63
|
-
|
112
|
+
apply_format(matcher, &block)
|
64
113
|
end
|
65
114
|
end
|
66
115
|
|
data/lib/mystique/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mystique
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Federico Iachetti
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-11-
|
11
|
+
date: 2018-11-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|