mystique 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mystique.rb +2 -98
- data/lib/mystique/presenter.rb +107 -0
- data/lib/mystique/undefined.rb +3 -0
- data/lib/mystique/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 962f1c55063f1f6290c886c5745b27fe1e050dd0
|
4
|
+
data.tar.gz: 4ad5b6fb40671f1e6dd48c98e303be69c98a6235
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9031edaf2db5862fd0aed63f2e2ab66a85e6a29f0306365f79f1a272a767e7c0cb3d03d9601c93fe01b21d3387c2d866833e5e0008ff22d9e8c3931145c7ce52
|
7
|
+
data.tar.gz: fe92484bfa886c6ac6b5707aed1c2908d5f1315579c2bf5cd70e91d88efb3bbc0a6b602a2d91a8e9f33ed7508ace61ca623a208cfbab4902ad9774ea9a0a3a8d
|
data/lib/mystique.rb
CHANGED
@@ -3,106 +3,10 @@ require "mystique/version"
|
|
3
3
|
require "callable"
|
4
4
|
require "string_plus"
|
5
5
|
|
6
|
-
require "mystique/
|
7
|
-
|
6
|
+
require "mystique/undefined"
|
7
|
+
require "mystique/presenter"
|
8
8
|
|
9
9
|
module Mystique
|
10
|
-
class Presenter
|
11
|
-
self.methods.select {|m| m.to_s.start_with?("to_") }.each do |m|
|
12
|
-
define_method(m) do |*args, &block|
|
13
|
-
target.send(m, *args, &block)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def initialize(object, context)
|
18
|
-
@__object__ = object
|
19
|
-
@__context__ = context || self.class.context || NullContext
|
20
|
-
end
|
21
|
-
|
22
|
-
def self.present(object, context=nil)
|
23
|
-
self.new(object, context).tap do |presenter|
|
24
|
-
yield presenter if block_given?
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def context
|
29
|
-
@__context__
|
30
|
-
end
|
31
|
-
alias :ctx :context
|
32
|
-
alias :h :context
|
33
|
-
|
34
|
-
def target
|
35
|
-
@__object__
|
36
|
-
end
|
37
|
-
|
38
|
-
def inspect
|
39
|
-
"<#{self.class}(#{target.inspect}) context: #{context.inspect}>"
|
40
|
-
end
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
def method_missing(method, *args, &block)
|
45
|
-
return target.send(method, *args, &block) if method.to_s.start_with?("to_")
|
46
|
-
format( target.send(method, *args, &block) )
|
47
|
-
end
|
48
|
-
|
49
|
-
def format(value)
|
50
|
-
result = if __formats__.keys.include?(value)
|
51
|
-
__formats__[value]
|
52
|
-
elsif __regex_formats__.any? { |regex, _| value =~ regex}
|
53
|
-
__regex_formats__.select { |regex, _| value =~ regex}.first.last
|
54
|
-
elsif __class_formats__.any? { |klass, _| value.is_a?(klass)}
|
55
|
-
__class_formats__.select { |klass, _| value.is_a?(klass)}.first.last
|
56
|
-
else
|
57
|
-
value
|
58
|
-
end
|
59
|
-
Mystique.present(Callable(result).call(value, context))
|
60
|
-
end
|
61
|
-
|
62
|
-
def self.context(ctx=Undefined)
|
63
|
-
@__context__ = ctx unless ctx == Undefined
|
64
|
-
@__context__
|
65
|
-
end
|
66
|
-
|
67
|
-
def self.format(matcher, value=nil, &block)
|
68
|
-
__formats__[matcher] = block_given? ? block : value
|
69
|
-
end
|
70
|
-
|
71
|
-
def self.format_multiple(*matchers, &block)
|
72
|
-
matchers.each do |matcher|
|
73
|
-
format(matcher, &block)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def __formats__
|
78
|
-
self.class.__formats__
|
79
|
-
end
|
80
|
-
|
81
|
-
def self.__formats__
|
82
|
-
@__formats__ ||= {
|
83
|
-
nil => "-----",
|
84
|
-
}
|
85
|
-
end
|
86
|
-
|
87
|
-
def __regex_formats__
|
88
|
-
self.class.__regex_formats__
|
89
|
-
end
|
90
|
-
|
91
|
-
def self.__regex_formats__
|
92
|
-
@__regex_formats__ ||= __formats__.select {|k, v| k.is_a?(Regexp)}
|
93
|
-
end
|
94
|
-
|
95
|
-
def __class_formats__
|
96
|
-
self.class.__class_formats__
|
97
|
-
end
|
98
|
-
|
99
|
-
def self.__class_formats__
|
100
|
-
@__class_formats__ ||= __formats__.select {|k, v| k.is_a?(Class)}
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
class Undefined; end
|
105
|
-
|
106
10
|
module_function
|
107
11
|
|
108
12
|
def present(object, with: nil, context: nil, &block)
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require "mystique/null_context"
|
2
|
+
|
3
|
+
module Mystique
|
4
|
+
class Presenter
|
5
|
+
self.methods.select {|m| m.to_s.start_with?("to_") }.each do |m|
|
6
|
+
define_method(m) do |*args, &block|
|
7
|
+
target.send(m, *args, &block)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(object, context)
|
12
|
+
@__object__ = object
|
13
|
+
@__context__ = context || self.class.context || NullContext
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.present(object, context=nil)
|
17
|
+
self.new(object, context).tap do |presenter|
|
18
|
+
yield presenter if block_given?
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def context
|
23
|
+
@__context__
|
24
|
+
end
|
25
|
+
alias :ctx :context
|
26
|
+
alias :h :context
|
27
|
+
|
28
|
+
def target
|
29
|
+
@__object__
|
30
|
+
end
|
31
|
+
|
32
|
+
def inspect
|
33
|
+
"<#{self.class}(#{target.inspect}) context: #{context.inspect}>"
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def method_missing(method, *args, &block)
|
39
|
+
return target.send(method, *args, &block) if method.to_s.start_with?("to_")
|
40
|
+
format( target.send(method, *args, &block) )
|
41
|
+
end
|
42
|
+
|
43
|
+
def format(value)
|
44
|
+
result = if __formats__.keys.include?(value)
|
45
|
+
__formats__[value]
|
46
|
+
elsif __regex_formats__.any? { |regex, _| value =~ regex}
|
47
|
+
__regex_formats__.select { |regex, _| value =~ regex}.first.last
|
48
|
+
elsif __class_formats__.any? { |klass, _| value.is_a?(klass)}
|
49
|
+
__class_formats__.select { |klass, _| value.is_a?(klass)}.first.last
|
50
|
+
else
|
51
|
+
value
|
52
|
+
end
|
53
|
+
Mystique.present(Callable(result).call(value, context))
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.context(ctx=Undefined)
|
57
|
+
@__context__ = ctx unless ctx == Undefined
|
58
|
+
@__context__
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.format(matcher, value=nil, &block)
|
62
|
+
__formats__[matcher] = block_given? ? block : value
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.format_multiple(*matchers, &block)
|
66
|
+
matchers.each do |matcher|
|
67
|
+
format(matcher, &block)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def __formats__
|
72
|
+
self.class.__formats__
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.default_formats
|
76
|
+
if block_given?
|
77
|
+
@__default_formats__ = yield
|
78
|
+
else
|
79
|
+
@__default_formats__ ||= {}
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def default_formats
|
84
|
+
Mystique::Presenter.default_formats
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.__formats__
|
88
|
+
@__formats__ ||= Mystique::Presenter.default_formats
|
89
|
+
end
|
90
|
+
|
91
|
+
def __regex_formats__
|
92
|
+
self.class.__regex_formats__
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.__regex_formats__
|
96
|
+
@__regex_formats__ ||= __formats__.select {|k, v| k.is_a?(Regexp)}
|
97
|
+
end
|
98
|
+
|
99
|
+
def __class_formats__
|
100
|
+
self.class.__class_formats__
|
101
|
+
end
|
102
|
+
|
103
|
+
def self.__class_formats__
|
104
|
+
@__class_formats__ ||= __formats__.select {|k, v| k.is_a?(Class)}
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
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.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Federico Iachetti
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -97,8 +97,10 @@ files:
|
|
97
97
|
- bin/setup
|
98
98
|
- lib/mystique.rb
|
99
99
|
- lib/mystique/null_context.rb
|
100
|
+
- lib/mystique/presenter.rb
|
100
101
|
- lib/mystique/presenters.rb
|
101
102
|
- lib/mystique/presenters/hash_presenter.rb
|
103
|
+
- lib/mystique/undefined.rb
|
102
104
|
- lib/mystique/version.rb
|
103
105
|
- mystique.gemspec
|
104
106
|
homepage: https://github.com/iachettifederico/mystique
|
@@ -121,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
121
123
|
version: '0'
|
122
124
|
requirements: []
|
123
125
|
rubyforge_project:
|
124
|
-
rubygems_version: 2.4.
|
126
|
+
rubygems_version: 2.4.8
|
125
127
|
signing_key:
|
126
128
|
specification_version: 4
|
127
129
|
summary: Ruby presenter gem.
|