mystique 0.10.2 → 1.0.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 +3 -1
- data/lib/mystique/presenter.rb +19 -18
- data/lib/mystique/presenter_class.rb +36 -0
- data/lib/mystique/version.rb +1 -1
- data/mystique.gemspec +2 -2
- metadata +13 -12
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/lib/mystique.rb
CHANGED
|
@@ -3,12 +3,14 @@ require "mystique/version"
|
|
|
3
3
|
require "callable"
|
|
4
4
|
|
|
5
5
|
require "mystique/undefined"
|
|
6
|
+
require "mystique/presenter_class"
|
|
6
7
|
require "mystique/presenter"
|
|
7
8
|
|
|
8
9
|
module Mystique
|
|
10
|
+
|
|
9
11
|
def self.present(object, with: nil, context: nil, &block)
|
|
10
12
|
begin
|
|
11
|
-
presenter_class =
|
|
13
|
+
presenter_class = PresenterClass.new(object, with).to_class
|
|
12
14
|
presenter_class.for(object, context, &block)
|
|
13
15
|
rescue NameError
|
|
14
16
|
return object
|
data/lib/mystique/presenter.rb
CHANGED
|
@@ -10,9 +10,9 @@ module Mystique
|
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
def self.for(object, context=nil)
|
|
13
|
-
|
|
13
|
+
new(object, context).tap { |presenter|
|
|
14
14
|
yield presenter if block_given?
|
|
15
|
-
|
|
15
|
+
}
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
def context
|
|
@@ -34,18 +34,18 @@ module Mystique
|
|
|
34
34
|
def method_missing(method, *args, &block)
|
|
35
35
|
return target.send(method, *args, &block) if method.to_s.start_with?("to_")
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
+
}
|
|
49
49
|
end
|
|
50
50
|
|
|
51
51
|
def formatted_method?(method)
|
|
@@ -61,15 +61,17 @@ module Mystique
|
|
|
61
61
|
end
|
|
62
62
|
|
|
63
63
|
def format(value)
|
|
64
|
-
result =
|
|
64
|
+
result = case
|
|
65
|
+
when __formats__.keys.include?(value)
|
|
65
66
|
__formats__[value]
|
|
66
|
-
|
|
67
|
+
when __regex_formats__.any? { |regex, _| value =~ regex}
|
|
67
68
|
__regex_formats__.select { |regex, _| value =~ regex}.first.last
|
|
68
|
-
|
|
69
|
+
when __class_formats__.any? { |klass, _| value.is_a?(klass)}
|
|
69
70
|
__class_formats__.select { |klass, _| value.is_a?(klass)}.first.last
|
|
70
71
|
else
|
|
71
72
|
value
|
|
72
73
|
end
|
|
74
|
+
|
|
73
75
|
Mystique.present(Callable(result).call(value, context))
|
|
74
76
|
end
|
|
75
77
|
|
|
@@ -94,7 +96,6 @@ module Mystique
|
|
|
94
96
|
end
|
|
95
97
|
end
|
|
96
98
|
|
|
97
|
-
# TODO: Define this
|
|
98
99
|
def self.present_collection(matcher)
|
|
99
100
|
if matcher.is_a?(Symbol)
|
|
100
101
|
__presented_collections__ << matcher
|
|
@@ -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,8 +19,8 @@ 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", "
|
|
22
|
+
spec.add_development_dependency "bundler", "~> 2.1.4"
|
|
23
|
+
spec.add_development_dependency "rake", ">= 12.3.3"
|
|
24
24
|
spec.add_development_dependency "rspec"
|
|
25
25
|
spec.add_development_dependency "pry-byebug"
|
|
26
26
|
spec.add_runtime_dependency "callable"
|
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,28 +16,28 @@ 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
42
|
name: rspec
|
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -99,6 +99,7 @@ files:
|
|
|
99
99
|
- lib/mystique.rb
|
|
100
100
|
- lib/mystique/null_context.rb
|
|
101
101
|
- lib/mystique/presenter.rb
|
|
102
|
+
- lib/mystique/presenter_class.rb
|
|
102
103
|
- lib/mystique/presenters.rb
|
|
103
104
|
- lib/mystique/presenters/hash_presenter.rb
|
|
104
105
|
- lib/mystique/undefined.rb
|
|
@@ -108,7 +109,7 @@ homepage: https://github.com/iachettifederico/mystique
|
|
|
108
109
|
licenses:
|
|
109
110
|
- MIT
|
|
110
111
|
metadata: {}
|
|
111
|
-
post_install_message:
|
|
112
|
+
post_install_message:
|
|
112
113
|
rdoc_options: []
|
|
113
114
|
require_paths:
|
|
114
115
|
- lib
|
|
@@ -123,8 +124,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
123
124
|
- !ruby/object:Gem::Version
|
|
124
125
|
version: '0'
|
|
125
126
|
requirements: []
|
|
126
|
-
rubygems_version: 3.0.
|
|
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: []
|