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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c505299fc6b248cacaf21cf35e16926910ed3a01
4
- data.tar.gz: 8205aab26db3faedb469ba110d11f939170b56c7
3
+ metadata.gz: e716ded139c4ff28b6f4029c5db454fb438227cf
4
+ data.tar.gz: e2a14fd0f614b0ad3bd561fbeaefc9f60386babf
5
5
  SHA512:
6
- metadata.gz: cfeb28202d4884b18814727e1cdc900e6fafff375fade6b0e6257ecb01cb5df1c334adee9dae44fd3f40b53ea3b8337e47cfd9342d73beea05ccb4a365b52516
7
- data.tar.gz: 9f9265e735800f2b412f405e3d6302a2a367c7424c48cb99d3bdfb4bbd0e4880f5b9d6c77e7ead6ca45c1f50d013f1a5d8693287aa65d0cafbf202744b355a10
6
+ metadata.gz: 25196e1c60dfee280fbd1062e030b7d883332a66b089204dd2efff1c06f0944169aaeaf62a9c3d525358d391838db7e42d9d744fd59fcdccb4030249aaf36b6c
7
+ data.tar.gz: d999c5e670f6c11a7c7d25594368c463902fd7afd23550dcd0c08e9f8946df544c434b7b97c6729e085bcbf2340f13dc741510436eb17d4e66fc268e6af6adc8
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # BottledDecorators
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/bottled_decorators`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ ## The best thing to happen since bottled water
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
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
- new(*args)
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
 
@@ -1,3 +1,3 @@
1
1
  module BottledDecorators
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
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.3
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-01-18 00:00:00.000000000 Z
11
+ date: 2017-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties