mystique 0.1.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7bca5b627fe51e0482f815c22868f0e3a87f9319
4
- data.tar.gz: 991afef7346d666191f9f86de0a676c010d85645
3
+ metadata.gz: 73d0338f218a8478a975c8efc740cd95a4b44e10
4
+ data.tar.gz: 3fa3343e877ef60871799cd936a088dd319dc1ad
5
5
  SHA512:
6
- metadata.gz: 815ea0883ee58e291526a72d2f6b3d7e268aa53da0101d2f912c4add64cb4affa885d66166412beeca43a315dcf8798b094a3f49c8e337a2a7855288c0de3d8e
7
- data.tar.gz: d28b3a87233981ad252a41704aa542809600e073b57e556f16dec0e402f60420197917c00866777ab654e85b128fd432a924f0140003ec666ffbbe19162aa795
6
+ metadata.gz: 82f86b6790a48e65cf8a86d16c3700a69741fb087465e3e9ccbaf73bef817ac90aa3af35ab5366e586e8758f235db213693e92aadd3dcb5584b8003dbf55593c
7
+ data.tar.gz: 7903d2a4720b5d04e5dd3df49e1c60cbfbbb64660e292aa4979a9711275c532f85d73ff85da399c0af7c3cfd7ca97ecce71be39f059988c110b36be25e278fe2
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # Mystique
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mystique`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Mystique is a gem that implements the presenter pattern. It allows you to augment an object, by wrapping it and giving it access to the context in which you need to render it.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ ## Usage
6
+
7
+ TODO: Write usage instructions here
6
8
 
7
9
  ## Installation
8
10
 
@@ -20,9 +22,6 @@ Or install it yourself as:
20
22
 
21
23
  $ gem install mystique
22
24
 
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
25
 
27
26
  ## Development
28
27
 
@@ -6,16 +6,20 @@ module Mystique
6
6
  class Presenter
7
7
  def initialize(object, context)
8
8
  @__object__ = object
9
- @__context__ = context
9
+ @__context__ = context || self.class.context
10
10
  end
11
11
 
12
- def self.present(object)
13
- new(object, context)
12
+ def self.present(object, context=nil)
13
+ self.new(object, context).tap do |presenter|
14
+ yield presenter if block_given?
15
+ end
14
16
  end
15
17
 
16
- def h
17
- @__context__ || self.class.context
18
+ def context
19
+ @__context__
18
20
  end
21
+ alias :ctx :context
22
+ alias :h :context
19
23
 
20
24
  def target
21
25
  @__object__
@@ -34,7 +38,7 @@ module Mystique
34
38
  else
35
39
  value
36
40
  end
37
- Callable(result).call(value)
41
+ Callable(result).call(value, context)
38
42
  end
39
43
 
40
44
  def self.context(ctx=Undefined)
@@ -42,10 +46,16 @@ module Mystique
42
46
  @__context__
43
47
  end
44
48
 
45
- def self.format(key, value)
46
- __formats__[key] = value
49
+ def self.format(matcher, value=nil, &block)
50
+ __formats__[matcher] = block_given? ? block : value
47
51
  end
48
52
 
53
+ def self.format_multiple(*matchers, &block)
54
+ matchers.each do |matcher|
55
+ format(matcher, &block)
56
+ end
57
+ end
58
+
49
59
  def __formats__
50
60
  self.class.__formats__
51
61
  end
@@ -77,16 +87,15 @@ module Mystique
77
87
 
78
88
  module_function
79
89
 
80
- def present(object, with: nil)
81
- from_module &&= "#{from_module.to_s.camelcase}::"
90
+ def present(object, with: nil, context: nil, &block)
82
91
  presenter_class = case with
83
92
  when nil
84
93
  "#{object.class}Presenter".constantize
85
94
  when Symbol, String
86
- "#{presenter}Presenter".constantize
95
+ "#{with}Presenter".constantize
87
96
  else
88
- presenter
97
+ with
89
98
  end
90
- presenter_class.present(object)
99
+ presenter_class.present(object, context, &block)
91
100
  end
92
101
  end
@@ -1,3 +1,3 @@
1
1
  module Mystique
2
- VERSION = "0.1.2"
2
+ VERSION = "0.3.0"
3
3
  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.1.2
4
+ version: 0.3.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: 2015-03-06 00:00:00.000000000 Z
11
+ date: 2015-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler