view_composer 0.1.6
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 +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +76 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/view_composer/base_composer.rb +136 -0
- data/lib/view_composer/version.rb +3 -0
- data/lib/view_composer.rb +5 -0
- data/view_composer.gemspec +34 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3c23d5629b4776fc2cb676d9f8932f48f960b5d1
|
4
|
+
data.tar.gz: 6dd23939c1f09978e3dbfdb1286f1cd7a9ec2bf3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 749e79cc3046d4f7aaccd6b252832a4218190cd76cc2273604d6ad7c52c8708c8525ba9d52c1ff04f72da8c34a98a73fcdb5d51fc12d494abd3f4d659398e323
|
7
|
+
data.tar.gz: 5a0fd538b83a520602f0bfd892bae48535caac04ed73b932edecc20173653ad998bfc3fd66dc5e79f73c0b9ec34bdb5ea0429df2e94d9ae8a8dcd597d6335477
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at korytegman@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Kory Tegman
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# ViewComposer
|
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.
|
4
|
+
|
5
|
+
This is still pre 1.0 software and the api will change.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'view_composer'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install view_composer
|
22
|
+
|
23
|
+
## Usage
|
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.
|
26
|
+
|
27
|
+
``` ruby
|
28
|
+
class PostComposer < BaseComposer
|
29
|
+
attributes :id, :name, :body
|
30
|
+
end
|
31
|
+
|
32
|
+
post_composer = PostCompser.new(model: Post.new(name: "a post") )
|
33
|
+
post_composer.name #=> "a post"
|
34
|
+
post_composer.id #=> 1
|
35
|
+
post_composer.hash_attrs #=> {id: 1, name: "a post", body: nil}
|
36
|
+
post_composer.to_json #=> "{\"id\":\"1\",\"name\":\"a post\", \"body\": \"\"}"
|
37
|
+
```
|
38
|
+
|
39
|
+
if you would like to override the model's value you can define it as a method
|
40
|
+
|
41
|
+
``` ruby
|
42
|
+
class PostComposer < BaseComposer
|
43
|
+
attributes :id, :name, :body
|
44
|
+
|
45
|
+
def name
|
46
|
+
"special super #{@model.name}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
post_composer.name #=> special super a post
|
50
|
+
```
|
51
|
+
|
52
|
+
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
|
+
your composer would look like this:
|
55
|
+
``` ruby
|
56
|
+
post_composer = PostComposer.new(model: post, composable_objects: [AdminStats])
|
57
|
+
post_composer.total_reads #=> 1000
|
58
|
+
post_composer.referrers #=> ["bily", "bob", "jane"]
|
59
|
+
post_composer.to_json #=> {"id": "1", "name": "a post", "body": "", "total_reads": "1000", "referrers": ["bily", "bob", "jane"] }
|
60
|
+
```
|
61
|
+
|
62
|
+
## Development
|
63
|
+
|
64
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
65
|
+
|
66
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
67
|
+
|
68
|
+
## Contributing
|
69
|
+
|
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
|
+
|
73
|
+
## License
|
74
|
+
|
75
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
76
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "view_composer"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
require 'json'
|
2
|
+
EXCLUDED_METHODS = [:to_json, :hash_attrs,
|
3
|
+
:set_inherited_methods_list,
|
4
|
+
:instance_methods,
|
5
|
+
:inherited_methods,
|
6
|
+
:attributes,
|
7
|
+
:instance_attributes,
|
8
|
+
:set_model_methods,
|
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
|
33
|
+
|
34
|
+
def self.attributes(*attrs)
|
35
|
+
self._instance_attrs = attrs
|
36
|
+
Array(attrs).each {|attr| self._attributes << attr}
|
37
|
+
end
|
38
|
+
|
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
|
45
|
+
|
46
|
+
def hash_attrs
|
47
|
+
@json_hash
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_json
|
51
|
+
@json_hash.to_json
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
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
|
60
|
+
|
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
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
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
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def instance_attributes
|
77
|
+
@instance_attributes ||= self.class._instance_attrs || []
|
78
|
+
end
|
79
|
+
|
80
|
+
def super_model_methods
|
81
|
+
return [] if self.class.superclass === Object
|
82
|
+
@super_model ||= self.class.superclass._model_methods || []
|
83
|
+
end
|
84
|
+
|
85
|
+
def attributes
|
86
|
+
@attributes ||= self.class._attributes
|
87
|
+
end
|
88
|
+
|
89
|
+
def instance_methods
|
90
|
+
@instance_methods ||= self.class.instance_methods(false)
|
91
|
+
end
|
92
|
+
|
93
|
+
def inherited_methods
|
94
|
+
@inherted_methods ||= self.class._inherited_methods
|
95
|
+
end
|
96
|
+
|
97
|
+
def get_all_methods
|
98
|
+
(attributes + inherited_methods + instance_methods).uniq
|
99
|
+
end
|
100
|
+
|
101
|
+
def set_inherited_methods_list
|
102
|
+
_methods = self.class.superclass.instance_methods(false) - EXCLUDED_METHODS
|
103
|
+
self.class._inherited_methods += _methods
|
104
|
+
end
|
105
|
+
|
106
|
+
def methods_to_hash
|
107
|
+
methods = get_all_methods - EXCLUDED_METHODS
|
108
|
+
methods.each do |method|
|
109
|
+
@json_hash[method] = self.send(method)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def set_attributes_methods
|
114
|
+
define_methods(definable_methods, @model)
|
115
|
+
end
|
116
|
+
|
117
|
+
def definable_methods
|
118
|
+
self.class._instance_defined_methods + self.class._model_methods
|
119
|
+
end
|
120
|
+
|
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
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
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)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'view_composer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "view_composer"
|
8
|
+
spec.version = ViewComposer::VERSION
|
9
|
+
spec.authors = ["Kory Tegman"]
|
10
|
+
spec.email = ["korytegman@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{View composer helps you compose objects and serialize
|
13
|
+
them. like a mix between AMS and Draper}
|
14
|
+
spec.description = %q{ view composer is a dynamic way to compose view objects}
|
15
|
+
spec.homepage = "http://github.com/koryteg/view_composer"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
19
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
20
|
+
if spec.respond_to?(:metadata)
|
21
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
22
|
+
else
|
23
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
33
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: view_composer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kory Tegman
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: " view composer is a dynamic way to compose view objects"
|
56
|
+
email:
|
57
|
+
- korytegman@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/view_composer.rb
|
73
|
+
- lib/view_composer/base_composer.rb
|
74
|
+
- lib/view_composer/version.rb
|
75
|
+
- view_composer.gemspec
|
76
|
+
homepage: http://github.com/koryteg/view_composer
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata:
|
80
|
+
allowed_push_host: https://rubygems.org
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.5.1
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: View composer helps you compose objects and serialize them. like a mix between
|
101
|
+
AMS and Draper
|
102
|
+
test_files: []
|