bottled_decorators 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -2
- data/lib/bottled_decorators/bottled_decorator.rb +39 -1
- data/lib/bottled_decorators/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e716ded139c4ff28b6f4029c5db454fb438227cf
|
4
|
+
data.tar.gz: e2a14fd0f614b0ad3bd561fbeaefc9f60386babf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25196e1c60dfee280fbd1062e030b7d883332a66b089204dd2efff1c06f0944169aaeaf62a9c3d525358d391838db7e42d9d744fd59fcdccb4030249aaf36b6c
|
7
|
+
data.tar.gz: d999c5e670f6c11a7c7d25594368c463902fd7afd23550dcd0c08e9f8946df544c434b7b97c6729e085bcbf2340f13dc741510436eb17d4e66fc268e6af6adc8
|
data/README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# BottledDecorators
|
2
2
|
|
3
|
-
|
3
|
+
## The best thing to happen since bottled water
|
4
4
|
|
5
|
-
|
5
|
+
Wait a minute, I thought that was [bottled_services?](https://github.com/John-Hayes-Reed/bottled_services), well nevermind.
|
6
|
+
bottled_decorators are here to make your life easier, and provide decorators that are actually decorators and not just View Objects / View Helpers with the name 'decorator'. bottled_decorators encourage the use of DRY and reusable code by stopping the direct model relation, and 'my job is to prepare something for the view' mentality seen in some gems and implementations, and to bring decorators back to what they should be, a reusable and stackable extra layer of functionality.
|
7
|
+
Creating your decorators are also as easy as pie with the botted_decorator generator. All you need to worry about is your method logic, let bottled_decorators do the rest for you!
|
6
8
|
|
7
9
|
## Installation
|
8
10
|
|
@@ -5,6 +5,9 @@ module BottledDecorator
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def initialize(component, **options)
|
8
|
+
class << self
|
9
|
+
attr_reader :component
|
10
|
+
end
|
8
11
|
@component = component
|
9
12
|
options.each do |option_key, option_val|
|
10
13
|
self.instance_variable_set("@#{option_key}", option_val)
|
@@ -17,9 +20,44 @@ module BottledDecorator
|
|
17
20
|
raise NoMethodError.new("Method #{method} was not found in the decorator, or the decorated objects", 'NoMethodError')
|
18
21
|
end
|
19
22
|
|
23
|
+
def as_json(**args)
|
24
|
+
to_h(**args).as_json
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_json(**args)
|
28
|
+
to_h(**args).to_json
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_h(**args)
|
32
|
+
{}.tap do |hash|
|
33
|
+
if @component.respond_to?(:component)
|
34
|
+
hash.merge!(@component.to_h(args))
|
35
|
+
else
|
36
|
+
hash.merge!(@component.as_json(args))
|
37
|
+
end
|
38
|
+
self.class.instance_methods(false).each do |m|
|
39
|
+
hash["#{m}"] = send(m)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def respond_to?(tester)
|
45
|
+
response = @component.respond_to?(tester)
|
46
|
+
return true if response
|
47
|
+
self.class.instance_methods(false).map(&:to_s).include?("#{tester}")
|
48
|
+
end
|
49
|
+
|
20
50
|
module ClassMethods
|
21
51
|
def call(*args)
|
22
|
-
|
52
|
+
if args.first.respond_to?(:each)
|
53
|
+
[].tap do |arr|
|
54
|
+
args.first.each do |comp|
|
55
|
+
arr << new(*([comp, args[1..-1]].flatten))
|
56
|
+
end
|
57
|
+
end
|
58
|
+
else
|
59
|
+
new(*args)
|
60
|
+
end
|
23
61
|
end
|
24
62
|
end
|
25
63
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bottled_decorators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John_Hayes-Reed
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-02-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|