view_composer 0.1.6 → 0.1.7

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: 3c23d5629b4776fc2cb676d9f8932f48f960b5d1
4
- data.tar.gz: 6dd23939c1f09978e3dbfdb1286f1cd7a9ec2bf3
3
+ metadata.gz: e46f45af2b4bd79d6069b07847b238671fcbc529
4
+ data.tar.gz: da79b5d6efdd919a71ba06be34c0852735d06dd4
5
5
  SHA512:
6
- metadata.gz: 749e79cc3046d4f7aaccd6b252832a4218190cd76cc2273604d6ad7c52c8708c8525ba9d52c1ff04f72da8c34a98a73fcdb5d51fc12d494abd3f4d659398e323
7
- data.tar.gz: 5a0fd538b83a520602f0bfd892bae48535caac04ed73b932edecc20173653ad998bfc3fd66dc5e79f73c0b9ec34bdb5ea0429df2e94d9ae8a8dcd597d6335477
6
+ metadata.gz: bd5a7e8a0e7212138c2053d753ac8b8cb0495c5dae15c75c07b8c022980f885da0dfcb26da10c285f2d548973df6b378496ac2fc55add20269158bcf21fd6e61
7
+ data.tar.gz: aa248cb57ebadb0302fa5c27cd03e340a98430cfa63e47a70db5163840eed7516afec384403cccd09bea245948e113aa60a91196aa173f5a927531be6d3cda57
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in ViewComposer.gemspec
3
+ # Specify your gem's dependencies in view_composer.gemspec
4
4
  gemspec
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # ViewComposer
1
+ # ViewComposer [![Build Status](https://travis-ci.org/koryteg/view_composer.svg?branch=master)](https://travis-ci.org/koryteg/view_composer)
2
2
 
3
- View Composer makes it easy to compose view objects for ruby apps. Create new composers, pass them a model and classes to merge, and all instance methods of the classes will be available on the composer. The Composer will also serialize these instance methods into `json` for an api. I like to think of it as a mix between Draper and Active Model Serializer but built on ideas of composition from Sandi Metz.
3
+ ViewComposer makes it easy to compose view objects for ruby apps. Create new composers, pass them a model and classes to merge, and all instance methods of the classes will be available on the composer. The Composer will also serialize these instance methods into `json` for an API. I like to think of it as a mix between Draper and ActiveModel Serializer but built on ideas of composition from Sandi Metz.
4
4
 
5
- This is still pre 1.0 software and the api will change.
5
+ This is still pre-1.0 software and the API will change.
6
6
 
7
7
  ## Installation
8
8
 
@@ -22,14 +22,14 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- create a new composer that inherits from `BaseComposer`. and use the attributes api (similar to active model serializer) to let your composer know what methods to respond to.
25
+ create a new composer that inherits from `BaseComposer`. and use the attributes API (similar to ActiveModel::Serializer) to let your composer know what methods to respond to.
26
26
 
27
- ``` ruby
28
- class PostComposer < BaseComposer
29
- attributes :id, :name, :body
27
+ ```ruby
28
+ class PostComposer < ViewComposer::BaseComposer
29
+ attributes :id, :name, :body
30
30
  end
31
31
 
32
- post_composer = PostCompser.new(model: Post.new(name: "a post") )
32
+ post_composer = PostComposer.new(model: Post.new(name: "a post") )
33
33
  post_composer.name #=> "a post"
34
34
  post_composer.id #=> 1
35
35
  post_composer.hash_attrs #=> {id: 1, name: "a post", body: nil}
@@ -38,21 +38,23 @@ post_composer.to_json #=> "{\"id\":\"1\",\"name\":\"a post\", \"body\": \"\"}"
38
38
 
39
39
  if you would like to override the model's value you can define it as a method
40
40
 
41
- ``` ruby
42
- class PostComposer < BaseComposer
43
- attributes :id, :name, :body
44
-
45
- def name
46
- "special super #{@model.name}"
47
- end
41
+ ```ruby
42
+ class PostComposer < ViewComposer::BaseComposer
43
+ attributes :id, :name, :body
44
+
45
+ def name
46
+ "special super #{@model.name}"
47
+ end
48
48
  end
49
+
49
50
  post_composer.name #=> special super a post
50
51
  ```
51
52
 
52
53
  the last part of this (that really makes it a composer) is that you can pass other classes to the composer and it will define those methods on the composer and serialize them into the same json object as well. Say you have `AdminStats` for your post that takes an instance of a post and responds to `total_reads` and `referrers`. ie: `AdminStats.new(post).total_reads` returns `1000`.
53
54
 
54
- your composer would look like this:
55
- ``` ruby
55
+ your composer would look like this:
56
+
57
+ ```ruby
56
58
  post_composer = PostComposer.new(model: post, composable_objects: [AdminStats])
57
59
  post_composer.total_reads #=> 1000
58
60
  post_composer.referrers #=> ["bily", "bob", "jane"]
@@ -67,10 +69,8 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
67
69
 
68
70
  ## Contributing
69
71
 
70
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ViewComposer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
71
-
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/view_composer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
72
73
 
73
74
  ## License
74
75
 
75
76
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
76
-
data/lib/view_composer.rb CHANGED
@@ -1,5 +1,5 @@
1
- require "View_composer/version"
2
- require "View_composer/base_composer"
1
+ require "view_composer/version"
2
+ require "view_composer/base_composer"
3
3
  require 'json'
4
4
  module ViewComposer
5
5
  end
@@ -7,129 +7,131 @@ EXCLUDED_METHODS = [:to_json, :hash_attrs,
7
7
  :instance_attributes,
8
8
  :set_model_methods,
9
9
  :super_model_methods]
10
- class BaseComposer
11
- class << self
12
- attr_accessor :_attributes,
13
- :_instance_attrs,
14
- :_model_methods,
15
- :_instance_defined_methods,
16
- :_inherited_methods
17
- end
18
- self._attributes = []
19
- self._instance_attrs = []
20
- self._inherited_methods = []
21
- self._model_methods = []
22
-
23
- def initialize(model:, composable_objects: [] )
24
- set_inherited_methods_list
25
- @model = model
26
- @json_hash = {}
27
- set_model_methods
28
- set_instance_defined_methods
29
- set_attributes_methods
30
- setup_comp_objs(composable_objects)
31
- methods_to_hash
32
- end
10
+ module ViewComposer
11
+ class BaseComposer
12
+ class << self
13
+ attr_accessor :_attributes,
14
+ :_instance_attrs,
15
+ :_model_methods,
16
+ :_instance_defined_methods,
17
+ :_inherited_methods
18
+ end
19
+ self._attributes = []
20
+ self._instance_attrs = []
21
+ self._inherited_methods = []
22
+ self._model_methods = []
23
+
24
+ def initialize(model:, composable_objects: [] )
25
+ set_inherited_methods_list
26
+ @model = model
27
+ @json_hash = {}
28
+ set_model_methods
29
+ set_instance_defined_methods
30
+ set_attributes_methods
31
+ setup_comp_objs(composable_objects)
32
+ methods_to_hash
33
+ end
33
34
 
34
- def self.attributes(*attrs)
35
- self._instance_attrs = attrs
36
- Array(attrs).each {|attr| self._attributes << attr}
37
- end
35
+ def self.attributes(*attrs)
36
+ self._instance_attrs = attrs
37
+ Array(attrs).each {|attr| self._attributes << attr}
38
+ end
38
39
 
39
- def self.inherited(base)
40
- super
41
- base._attributes = self._attributes.dup
42
- base._inherited_methods = self._inherited_methods.dup
43
- base._model_methods = self._model_methods.dup
44
- end
40
+ def self.inherited(base)
41
+ super
42
+ base._attributes = self._attributes.dup
43
+ base._inherited_methods = self._inherited_methods.dup
44
+ base._model_methods = self._model_methods.dup
45
+ end
45
46
 
46
- def hash_attrs
47
- @json_hash
48
- end
47
+ def hash_attrs
48
+ @json_hash
49
+ end
49
50
 
50
- def to_json
51
- @json_hash.to_json
52
- end
51
+ def to_json
52
+ @json_hash.to_json
53
+ end
53
54
 
54
- private
55
+ private
55
56
 
56
- def set_model_methods
57
- new_model_methods = attributes - instance_methods
58
- new_model_methods = new_model_methods - inherited_methods
59
- new_model_methods = new_model_methods + super_model_methods
57
+ def set_model_methods
58
+ new_model_methods = attributes - instance_methods
59
+ new_model_methods = new_model_methods - inherited_methods
60
+ new_model_methods = new_model_methods + super_model_methods
60
61
 
61
- if self.class.superclass != Object
62
- self.class._model_methods = super_model_methods + new_model_methods
63
- else
64
- self.class._model_methods = new_model_methods
62
+ if self.class.superclass != Object
63
+ self.class._model_methods = super_model_methods + new_model_methods
64
+ else
65
+ self.class._model_methods = new_model_methods
66
+ end
65
67
  end
66
- end
67
68
 
68
- def set_instance_defined_methods
69
- if self.class._instance_defined_methods != nil
70
- self.class._instance_defined_methods += self.class._model_methods
71
- else
72
- self.class._instance_defined_methods = self.class._model_methods
69
+ def set_instance_defined_methods
70
+ if self.class._instance_defined_methods != nil
71
+ self.class._instance_defined_methods += self.class._model_methods
72
+ else
73
+ self.class._instance_defined_methods = self.class._model_methods
74
+ end
73
75
  end
74
- end
75
76
 
76
- def instance_attributes
77
- @instance_attributes ||= self.class._instance_attrs || []
78
- end
77
+ def instance_attributes
78
+ @instance_attributes ||= self.class._instance_attrs || []
79
+ end
79
80
 
80
- def super_model_methods
81
- return [] if self.class.superclass === Object
82
- @super_model ||= self.class.superclass._model_methods || []
83
- end
81
+ def super_model_methods
82
+ return [] if self.class.superclass === Object
83
+ @super_model ||= self.class.superclass._model_methods || []
84
+ end
84
85
 
85
- def attributes
86
- @attributes ||= self.class._attributes
87
- end
86
+ def attributes
87
+ @attributes ||= self.class._attributes
88
+ end
88
89
 
89
- def instance_methods
90
- @instance_methods ||= self.class.instance_methods(false)
91
- end
90
+ def instance_methods
91
+ @instance_methods ||= self.class.instance_methods(false)
92
+ end
92
93
 
93
- def inherited_methods
94
- @inherted_methods ||= self.class._inherited_methods
95
- end
94
+ def inherited_methods
95
+ @inherted_methods ||= self.class._inherited_methods
96
+ end
96
97
 
97
- def get_all_methods
98
- (attributes + inherited_methods + instance_methods).uniq
99
- end
98
+ def get_all_methods
99
+ (attributes + inherited_methods + instance_methods).uniq
100
+ end
100
101
 
101
- def set_inherited_methods_list
102
- _methods = self.class.superclass.instance_methods(false) - EXCLUDED_METHODS
103
- self.class._inherited_methods += _methods
104
- end
102
+ def set_inherited_methods_list
103
+ _methods = self.class.superclass.instance_methods(false) - EXCLUDED_METHODS
104
+ self.class._inherited_methods += _methods
105
+ end
105
106
 
106
- def methods_to_hash
107
- methods = get_all_methods - EXCLUDED_METHODS
108
- methods.each do |method|
109
- @json_hash[method] = self.send(method)
107
+ def methods_to_hash
108
+ methods = get_all_methods - EXCLUDED_METHODS
109
+ methods.each do |method|
110
+ @json_hash[method] = self.send(method)
111
+ end
110
112
  end
111
- end
112
113
 
113
- def set_attributes_methods
114
- define_methods(definable_methods, @model)
115
- end
114
+ def set_attributes_methods
115
+ define_methods(definable_methods, @model)
116
+ end
116
117
 
117
- def definable_methods
118
- self.class._instance_defined_methods + self.class._model_methods
119
- end
118
+ def definable_methods
119
+ self.class._instance_defined_methods + self.class._model_methods
120
+ end
120
121
 
121
- def setup_comp_objs(comp_objs_array)
122
- @comp_objs = comp_objs_array.map do |obj|
123
- object_instance = obj.new(@model)
124
- define_methods(obj.instance_methods(false), object_instance)
125
- return object_instance
122
+ def setup_comp_objs(comp_objs_array)
123
+ @comp_objs = comp_objs_array.map do |obj|
124
+ object_instance = obj.new(@model)
125
+ define_methods(obj.instance_methods(false), object_instance)
126
+ return object_instance
127
+ end
126
128
  end
127
- end
128
129
 
129
- def define_methods(method_names, method_owner)
130
- method_names.uniq.each do |attr|
131
- self.class.send(:define_method, attr) do
132
- method_owner.send(attr)
130
+ def define_methods(method_names, method_owner)
131
+ method_names.uniq.each do |attr|
132
+ self.class.send(:define_method, attr) do
133
+ method_owner.send(attr)
134
+ end
133
135
  end
134
136
  end
135
137
  end
@@ -1,3 +1,3 @@
1
1
  module ViewComposer
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
@@ -9,9 +9,9 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Kory Tegman"]
10
10
  spec.email = ["korytegman@gmail.com"]
11
11
 
12
- spec.summary = %q{View composer helps you compose objects and serialize
12
+ spec.summary = %q{ViewComposer helps you compose objects and serialize
13
13
  them. like a mix between AMS and Draper}
14
- spec.description = %q{ view composer is a dynamic way to compose view objects}
14
+ spec.description = %q{ViewComposer is a dynamic way to compose view objects}
15
15
  spec.homepage = "http://github.com/koryteg/view_composer"
16
16
  spec.license = "MIT"
17
17
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: view_composer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kory Tegman
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
- description: " view composer is a dynamic way to compose view objects"
55
+ description: ViewComposer is a dynamic way to compose view objects
56
56
  email:
57
57
  - korytegman@gmail.com
58
58
  executables: []
@@ -97,6 +97,6 @@ rubyforge_project:
97
97
  rubygems_version: 2.5.1
98
98
  signing_key:
99
99
  specification_version: 4
100
- summary: View composer helps you compose objects and serialize them. like a mix between
100
+ summary: ViewComposer helps you compose objects and serialize them. like a mix between
101
101
  AMS and Draper
102
102
  test_files: []