mystique 0.8.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +3 -0
- data/Rakefile +0 -1
- data/lib/mystique.rb +11 -15
- data/lib/mystique/presenter.rb +40 -16
- data/lib/mystique/presenter_class.rb +36 -0
- data/lib/mystique/version.rb +1 -1
- data/mystique.gemspec +4 -4
- metadata +18 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d6a1dbb9a3299b3f67616bf2ede3357984a31d59552f6b09994f93d1f100877f
|
4
|
+
data.tar.gz: e1bcc1e21a9100e6d7726b6cf669287e11f0490970e6b9c06c8b627d1e76c7af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29409b10c2b6a53917853cb50f7fca7acb67f956c855facde0a3ae7ac2a9a33b98eb3ff1bdc0461c609093516643aa9749d657a8aeba45adfafff53e1bf50799
|
7
|
+
data.tar.gz: 24479c27b05b802d70a80933ef932c7b5c6b10f141a7de29036b2a1fd60a2b601a007f364377cb9d9b973811e501792aad7f797213b39eab9236f94bdf9fd0f6
|
data/.rspec
ADDED
data/Rakefile
CHANGED
data/lib/mystique.rb
CHANGED
@@ -1,42 +1,38 @@
|
|
1
1
|
require "mystique/version"
|
2
2
|
|
3
3
|
require "callable"
|
4
|
-
require "string_plus"
|
5
4
|
|
6
5
|
require "mystique/undefined"
|
6
|
+
require "mystique/presenter_class"
|
7
7
|
require "mystique/presenter"
|
8
8
|
|
9
9
|
module Mystique
|
10
|
-
module_function
|
11
10
|
|
12
|
-
def present(object, with: nil, context: nil, &block)
|
11
|
+
def self.present(object, with: nil, context: nil, &block)
|
13
12
|
begin
|
14
|
-
presenter_class =
|
13
|
+
presenter_class = PresenterClass.new(object, with).to_class
|
15
14
|
presenter_class.for(object, context, &block)
|
16
15
|
rescue NameError
|
17
16
|
return object
|
18
17
|
end
|
19
18
|
end
|
20
19
|
|
21
|
-
def present_collection(collection, context: nil, &block)
|
22
|
-
return to_enum(:present_collection, collection, context: context, &block) unless block_given?
|
20
|
+
def self.present_collection(collection, context: nil, with: nil, &block)
|
21
|
+
return to_enum(:present_collection, collection, context: context, with: with, &block) unless block_given?
|
23
22
|
|
24
23
|
collection.each do |element|
|
25
24
|
case block.arity
|
26
25
|
when 2
|
27
|
-
block.call( present(element, context: context), element )
|
26
|
+
block.call( present(element, context: context, with: with), element )
|
28
27
|
else
|
29
|
-
block.call(present(element, context: context))
|
28
|
+
block.call(present(element, context: context, with: with))
|
30
29
|
end
|
31
30
|
end
|
32
31
|
end
|
33
32
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
StringPlus.constantize("#{with || object.class}Presenter")
|
39
|
-
end
|
33
|
+
private
|
34
|
+
|
35
|
+
def self.presenter_class_for(object, with)
|
36
|
+
with || Object.send(:const_get, "#{object.class}Presenter")
|
40
37
|
end
|
41
|
-
private :presenter_class_for
|
42
38
|
end
|
data/lib/mystique/presenter.rb
CHANGED
@@ -2,15 +2,17 @@ require "mystique/null_context"
|
|
2
2
|
|
3
3
|
module Mystique
|
4
4
|
class Presenter
|
5
|
+
FORMATS = {}
|
6
|
+
|
5
7
|
def initialize(object, context)
|
6
8
|
@__object__ = object
|
7
9
|
@__context__ = context || self.class.context || NullContext
|
8
10
|
end
|
9
11
|
|
10
12
|
def self.for(object, context=nil)
|
11
|
-
|
13
|
+
new(object, context).tap { |presenter|
|
12
14
|
yield presenter if block_given?
|
13
|
-
|
15
|
+
}
|
14
16
|
end
|
15
17
|
|
16
18
|
def context
|
@@ -32,16 +34,18 @@ module Mystique
|
|
32
34
|
def method_missing(method, *args, &block)
|
33
35
|
return target.send(method, *args, &block) if method.to_s.start_with?("to_")
|
34
36
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
37
|
+
target.send(method, *args, &block).yield_self { |value|
|
38
|
+
case
|
39
|
+
when formatted_method?(method)
|
40
|
+
format( value )
|
41
|
+
when presented_method?(method)
|
42
|
+
Mystique.present(value, context: context)
|
43
|
+
when presented_collection?(method)
|
44
|
+
Mystique.present_collection(value, context: context, &block)
|
45
|
+
else
|
46
|
+
value
|
47
|
+
end
|
48
|
+
}
|
45
49
|
end
|
46
50
|
|
47
51
|
def formatted_method?(method)
|
@@ -52,16 +56,22 @@ module Mystique
|
|
52
56
|
__presented_methods__.include?(method)
|
53
57
|
end
|
54
58
|
|
59
|
+
def presented_collection?(method)
|
60
|
+
__presented_collections__.include?(method)
|
61
|
+
end
|
62
|
+
|
55
63
|
def format(value)
|
56
|
-
result =
|
64
|
+
result = case
|
65
|
+
when __formats__.keys.include?(value)
|
57
66
|
__formats__[value]
|
58
|
-
|
67
|
+
when __regex_formats__.any? { |regex, _| value =~ regex}
|
59
68
|
__regex_formats__.select { |regex, _| value =~ regex}.first.last
|
60
|
-
|
69
|
+
when __class_formats__.any? { |klass, _| value.is_a?(klass)}
|
61
70
|
__class_formats__.select { |klass, _| value.is_a?(klass)}.first.last
|
62
71
|
else
|
63
72
|
value
|
64
73
|
end
|
74
|
+
|
65
75
|
Mystique.present(Callable(result).call(value, context))
|
66
76
|
end
|
67
77
|
|
@@ -86,6 +96,12 @@ module Mystique
|
|
86
96
|
end
|
87
97
|
end
|
88
98
|
|
99
|
+
def self.present_collection(matcher)
|
100
|
+
if matcher.is_a?(Symbol)
|
101
|
+
__presented_collections__ << matcher
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
89
105
|
def self.format_and_present(matcher)
|
90
106
|
format_method(method)
|
91
107
|
present_method(method)
|
@@ -95,6 +111,10 @@ module Mystique
|
|
95
111
|
@__presented_methods__ ||= []
|
96
112
|
end
|
97
113
|
|
114
|
+
def self.__presented_collections__
|
115
|
+
@__presented_collections__ ||= []
|
116
|
+
end
|
117
|
+
|
98
118
|
def self.__formatted_methods__
|
99
119
|
@__formatted_methods__ ||= []
|
100
120
|
end
|
@@ -103,6 +123,10 @@ module Mystique
|
|
103
123
|
self.class.__presented_methods__
|
104
124
|
end
|
105
125
|
|
126
|
+
def __presented_collections__
|
127
|
+
self.class.__presented_collections__
|
128
|
+
end
|
129
|
+
|
106
130
|
def __formatted_methods__
|
107
131
|
self.class.__formatted_methods__
|
108
132
|
end
|
@@ -118,7 +142,7 @@ module Mystique
|
|
118
142
|
end
|
119
143
|
|
120
144
|
def self.__formats__
|
121
|
-
|
145
|
+
FORMATS
|
122
146
|
end
|
123
147
|
|
124
148
|
def __regex_formats__
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Mystique
|
2
|
+
class PresenterClass
|
3
|
+
def initialize(object, with=nil)
|
4
|
+
@with = with
|
5
|
+
@object = object
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_class
|
9
|
+
with || Object.send(:const_get, class_name)
|
10
|
+
end
|
11
|
+
|
12
|
+
def class_name
|
13
|
+
return with.to_s if with
|
14
|
+
|
15
|
+
"#{base_class_name(object)}Presenter"
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
attr_reader :with
|
21
|
+
attr_reader :object
|
22
|
+
|
23
|
+
def base_class_name(for_object)
|
24
|
+
case for_object
|
25
|
+
when Symbol, String
|
26
|
+
for_object.to_s.split(/_/).map(&:capitalize).join
|
27
|
+
when Array
|
28
|
+
for_object.map { |current_object|
|
29
|
+
base_class_name(current_object)
|
30
|
+
}.join("::")
|
31
|
+
else
|
32
|
+
for_object.class.to_s
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/mystique/version.rb
CHANGED
data/mystique.gemspec
CHANGED
@@ -19,9 +19,9 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
-
spec.add_development_dependency "bundler", "~> 1.
|
23
|
-
spec.add_development_dependency "rake", "
|
24
|
-
spec.add_development_dependency "
|
22
|
+
spec.add_development_dependency "bundler", "~> 2.1.4"
|
23
|
+
spec.add_development_dependency "rake", ">= 12.3.3"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_development_dependency "pry-byebug"
|
25
26
|
spec.add_runtime_dependency "callable"
|
26
|
-
spec.add_runtime_dependency "string_plus"
|
27
27
|
end
|
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: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Federico Iachetti
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-06-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,30 +16,30 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 2.1.4
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 2.1.4
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 12.3.3
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 12.3.3
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
@@ -53,13 +53,13 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: pry-byebug
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
|
-
type: :
|
62
|
+
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
@@ -67,7 +67,7 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: callable
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - ">="
|
@@ -88,6 +88,7 @@ extensions: []
|
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
90
|
- ".gitignore"
|
91
|
+
- ".rspec"
|
91
92
|
- CODE_OF_CONDUCT.md
|
92
93
|
- Gemfile
|
93
94
|
- LICENSE.txt
|
@@ -98,6 +99,7 @@ files:
|
|
98
99
|
- lib/mystique.rb
|
99
100
|
- lib/mystique/null_context.rb
|
100
101
|
- lib/mystique/presenter.rb
|
102
|
+
- lib/mystique/presenter_class.rb
|
101
103
|
- lib/mystique/presenters.rb
|
102
104
|
- lib/mystique/presenters/hash_presenter.rb
|
103
105
|
- lib/mystique/undefined.rb
|
@@ -107,7 +109,7 @@ homepage: https://github.com/iachettifederico/mystique
|
|
107
109
|
licenses:
|
108
110
|
- MIT
|
109
111
|
metadata: {}
|
110
|
-
post_install_message:
|
112
|
+
post_install_message:
|
111
113
|
rdoc_options: []
|
112
114
|
require_paths:
|
113
115
|
- lib
|
@@ -122,9 +124,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
124
|
- !ruby/object:Gem::Version
|
123
125
|
version: '0'
|
124
126
|
requirements: []
|
125
|
-
|
126
|
-
|
127
|
-
signing_key:
|
127
|
+
rubygems_version: 3.0.8
|
128
|
+
signing_key:
|
128
129
|
specification_version: 4
|
129
130
|
summary: Ruby presenter gem.
|
130
131
|
test_files: []
|