adorn 0.0.4 → 0.0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 74658299c2b97f2148f863e4a0d02bdd800c5f49
4
- data.tar.gz: 037a64e3129e9fdb4fb3105e6bc137e0b0eecd75
3
+ metadata.gz: a856199ed966d40cc4030d5e386ddb163f2cf677
4
+ data.tar.gz: a396250bf6f4a17855e22d9b8f28c2d668cba872
5
5
  SHA512:
6
- metadata.gz: 90bf1f8055f3bc83a8b68e2e674d2fa14e756c8828a1aeae7badfd4be3ebab6969722cc468dbc028f96a8221ed2381749e2df0359f36254d59b605bca5618cbd
7
- data.tar.gz: ea6a4588ffd4b22b45d60ee7c875e05df9e61579e8a672a25376d8600eca186bdce0366833ee6401026f5b24b70ff24887d29b7b804807d9292c14f0f3df4712
6
+ metadata.gz: 8c91111ae02408676c852c0e20ee9de70065f3825a41e84d586b901e00f6a32c6c9bf29596f0e55d1e7f680bc1b5367ede1edbb77c4b560e771bc0bea41f8146
7
+ data.tar.gz: fdfa4a711f668212af484060486346dad9f2d013ec83d4a4da624cde7a548b499d6c8c3ac0860dd77dfe1eb84ba8f94a77cce85dae0accdf287e50e70e330e80
@@ -1,11 +1,17 @@
1
1
  module Adorn
2
2
  class AbstractPresenter
3
3
 
4
+ # Macro used to create an instance method that wraps the underlying
5
+ # object passed to the decorator
6
+ #
7
+ # @param [Symbol]
8
+ # @return [Method] Adorn instance.
4
9
  def self.presents(name)
5
10
  define_method(name) { @object }
6
11
  end
7
12
 
8
- # delegate to context
13
+ # Check both the context and the object before passing exception along
14
+ # @return [Method, StandardError]
9
15
  def method_missing(method, *args, &block)
10
16
  @context.send(method, *args, &block) if @context.respond_to?(method)
11
17
  @object.send(method, *args, &block) if @object.respond_to?(method)
@@ -1,13 +1,27 @@
1
- require 'adorn/middleware'
2
- require 'adorn/railtie' if defined?(Rails::Railtie)
3
-
1
+ # A threadsafe (on write), cache for storing objects during the request response cycle.
2
+ # User responsibility to provide a serialization | deserialization strategy to set
3
+ # get items from the hash.
4
+ #
5
+ # ex. setting data in Adorn::Cache can be acheieved like so:
6
+ # Adorn::Cache.append!(:foo, serialized_object)
7
+ #
8
+ # ex. retrieving data in Adorn::Cache can be acheieved like so:
9
+ # Adorn::Cache[:foo] #=> serialized_object
4
10
  module Adorn
5
11
  module Cache
6
12
  class << self
13
+ # Used to create a critical area during writes to Adorn::Cache
14
+ #
15
+ # @return [Mutex]
7
16
  def semaphore
8
17
  Thread.current[:semaphore] ||= Mutex.new
9
18
  end
10
19
 
20
+ # Instantiate or read from Adorn::Cache.
21
+ # Provide an interface to append to Adorn::Cache during request
22
+ #
23
+ # @param [Symbol, *]
24
+ # @return [Hash]
11
25
  def append!(key, value)
12
26
  Thread.current[:adorn_cache] ||= Hash.new
13
27
 
@@ -16,6 +30,25 @@ module Adorn
16
30
  end rescue nil #rescue ThreadError
17
31
  end
18
32
 
33
+ # Return value at given key from the Adorn::Cache.
34
+ #
35
+ # @param [Symbol, String]
36
+ # @return [Hash]
37
+ def [](key)
38
+ case key.class.to_s
39
+ when 'String'
40
+ Thread.current[:adorn_cache][key]
41
+ when 'Symbol'
42
+ Thread.current[:adorn_cache][key.to_s]
43
+ else
44
+ raise 'Unable to read key'
45
+ end
46
+
47
+ end
48
+
49
+ # Remove all values from the Adorn::Cache.
50
+ #
51
+ # @return [Hash]
19
52
  def clear!
20
53
  semaphore.synchronize do
21
54
  Thread.current[:adorn_cache].clear
@@ -1,6 +1,18 @@
1
+ # Provides decorator descendants with the #extensions macro, which returns a module.
2
+ # Useful for endowing decorated object with methods from modules
3
+ # set the modules to extend the decorated object with the :with option.
4
+ #
5
+ # Adorn::Delegate.extensions { {with: [AwesomePower, GreatResponsibility]} }
6
+ #
1
7
  module Adorn
2
8
  module Delegate
3
9
 
10
+ # Creates and anonymous module and passes the result of the given block
11
+ # back to the context, extending the named @object instance variable.
12
+ # Provide an interface to append to Adorn::Cache during request
13
+ #
14
+ # @param [Module, Object, Proc]
15
+ # @return [Module]
4
16
  def self.extensions(m=Module.new, context=self, &block)
5
17
  return unless block_given?
6
18
  m.instance_eval do
@@ -1,6 +1,8 @@
1
1
  module Adorn
2
2
  class DisplayContext
3
3
 
4
+ # @private
5
+ # @return [SimpleDelegator]
4
6
  def initialize(context)
5
7
  @context = SimpleDelegator.new(context)
6
8
  end
@@ -1,26 +1,35 @@
1
- # API: Use this method to present objects from the application to the view that require the presenter abstraction. The
2
- # method will accept single or multiple objects for dynamic presentation.
3
- #
4
- # Ex: Single Object, dynamic presenter loading
5
- # views/homes/foo.html.haml
6
- #
7
- # presenting @bar do |bar_presenter|
8
- # responds to methods found in BarPresenter class
9
- # end
10
- #
11
- # Ex: Single Object, explicit Presenter Class
12
- #
13
- # presenting @bar, BazPresenter do |bar_presenter|
14
- # responds to methods found in BazPresenter class
15
- # end
16
- #
17
- # Ex: Multiple Objects NOTE: implicit detection only
18
- #
19
- # presenting([@foo, @bar]) do |foo_presenter, bar_presenter|
20
- # end
21
1
  module Adorn
22
2
  module Helper
23
3
 
4
+ # A helper method to present objects with an implicit or explicilty set Adorn::Decorator
5
+ # Accepts single or multiple objects and Decorators for presentation.
6
+ #
7
+ # ex: Single Object, dynamic presenter loading
8
+ # presenting @bar do |bar_presenter|
9
+ #
10
+ # bar_presenter will respond to methods from the BarPresenter
11
+ #
12
+ # end
13
+ #
14
+ # ex: Single Object, explicit Decorator Class
15
+ #
16
+ # presenting @bar, BazPresenter do |bar_presenter|
17
+ #
18
+ # bar_presenter will respond to methods from the BazPresenter
19
+ #
20
+ # end
21
+ #
22
+ # ex: Multiple Objects NOTE: implicit detection only
23
+ #
24
+ # presenting([@foo, @bar]) do |foo_presenter, bar_presenter|
25
+ #
26
+ # @foo will respond to methods from the FooPresenter
27
+ # @bar will respond to methods from the BarPresenter
28
+ #
29
+ # end
30
+ #
31
+ # @param [Object, Class, Hash]
32
+ # @return [Adorn::Decorator]
24
33
  def presenting(object, klass=nil, options={})
25
34
  if object.is_a?(Array)
26
35
  object.each do |presentable|
@@ -1,4 +1,5 @@
1
1
  require 'adorn/cache'
2
+ require 'adorn/railtie' if defined?(Rails::Railtie)
2
3
 
3
4
  module Adorn
4
5
  class Middleware
@@ -1,6 +1,8 @@
1
1
  module Adorn
2
2
  class ObjectDecorator
3
3
 
4
+ # @private
5
+ # @return [SimpleDelegator]
4
6
  def initialize(object)
5
7
  @object = SimpleDelegator.new(object)
6
8
  end
@@ -1,7 +1,29 @@
1
+ # Provides interface for subclassing decorators. Methods on the decorated object will
2
+ # be passed through the method that is defined using the ::presents method. Every object must
3
+ # provide an object to decorate and a context object, options are passed through having no effect
4
+ # on the presented object. The context object is implicit, when using the helper method #presenting
5
+ # provided by Adorn::Helpers
6
+ #
7
+ # ex. Interface
8
+ #
9
+ # require 'adorn'
10
+ # class WelcomePresernter < Adorn::Presenter
11
+ # Adorn::Delegate.extensions { {with: [ImportantModule, NeededBehavior]} }
12
+ #
13
+ # presents :welcomer
14
+ #
15
+ # def welcome
16
+ # welomer.english_greeting # => 'Hello there!'
17
+ # end
18
+ # end
19
+
1
20
  module Adorn
2
21
  class Presenter < AbstractPresenter
3
22
  include Adorn::Delegate
4
23
 
24
+
25
+ #@param [Object, Object, Hash]
26
+ #@return [Adorn::Presenter]
5
27
  def initialize(*args)
6
28
  object, context, options = args
7
29
  @object = ObjectDecorator.new(object)
@@ -2,7 +2,7 @@ module Adorn
2
2
  module Version
3
3
  Major = 0
4
4
  Minor = 0
5
- Tiny = 4
5
+ Tiny = 5
6
6
  Pre = nil
7
7
 
8
8
  String = [Major, Minor, Tiny, Pre].compact.join(".")
@@ -4,7 +4,7 @@ require 'adorn/cache'
4
4
 
5
5
  ImportantObject = Struct.new(:name, :age) do
6
6
  def name
7
- print "I am #{@name}"
7
+ print "my name is #{@name}"
8
8
  end
9
9
 
10
10
  def age
@@ -32,6 +32,18 @@ class TestAdornedCache < Adorn::TestCase
32
32
  assert_same me_or_someone_like_me, Thread.current[:adorn_cache]['person']
33
33
  end
34
34
 
35
+ def test_read_key
36
+ me_or_someone_like_me = ImportantObject.new('rhodee', 42)
37
+ Adorn::Cache.append! :person, me_or_someone_like_me
38
+
39
+ assert_same me_or_someone_like_me, Thread.current[:adorn_cache]['person']
40
+ assert_equal me_or_someone_like_me, Adorn::Cache[:person]
41
+ end
42
+
43
+ def test_read_unknown_key
44
+ assert_raises(NoMethodError) { Adorn::Cache[:unknown_key] }
45
+ end
46
+
35
47
  def test_clear_key
36
48
  another_person = ImportantObject.new('snuggs', 10)
37
49
  Adorn::Cache.append! :peep, another_person
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adorn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - rhodee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-05 00:00:00.000000000 Z
11
+ date: 2014-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard