model_view 0.1.1 → 0.1.2

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: 3d2669fe111931e97ab7797c72e6e2c378d9725e
4
- data.tar.gz: 5651183f0f88dc42ed84bcb83eebf16bfd0c589d
3
+ metadata.gz: 71429c06ee7aa9db651f524b4c91ad9c339ae689
4
+ data.tar.gz: f44a76b5e2fe0847f6ea86da4729278fa9296b14
5
5
  SHA512:
6
- metadata.gz: 32d9f88924bd18f90d0b883996bf9d15ff7c16d551097f33f46a8bccd8c60395fec14850e44337d98787baed113730e1904012573cca071baef10ed9a8b11ba5
7
- data.tar.gz: 56489585b9e4fbf2194dd006dc22fe47b41b62eb849ba328f9602a08ac760874b22e678ade00719b548e873e32ba1d8df2c078fa343aaa8ed43e0c551447d9fe
6
+ metadata.gz: '01014719b0d56562ff129f4dd5f1a8eb2f54210b46bd365b89abf4e8ff0360035edbcb3a00fc6515aa222b061ee1f57259c1fce34c8cbd5d215669bf98f718ed'
7
+ data.tar.gz: 76f2f35fc561f8afaa2195f79f1257df20c1c68d4f2d7d9d861b0a8cc72e39985bfae54d4ac77cdcb34883818e720ddd9d7584973e1aba488c94b2d75b028c78
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ # 0.1.2
4
+ * Cleaned up ModelView.as_hash arguments
5
+ * Update readme
6
+
7
+
3
8
  # 0.1.1
4
9
  * Fix default scope value
5
10
 
data/README.md CHANGED
@@ -1,2 +1,103 @@
1
1
  # model_view
2
- Composable serialisation for models
2
+ ## Composable serialisation for models
3
+
4
+ ModelView let's you define views for your models in one place.
5
+
6
+ ### Why ModelView?
7
+
8
+ At OfferZen, most of our rails models can be presented in al least two ways. For example, an Interview Request
9
+ is presented differently to a candidate than to a company. But, there is also a lot of fields that get presented
10
+ the same to both candidates and companies. We found ourselves duplicating a lot of our serialisation code in our
11
+ controllers and the code started to get out of hand.
12
+
13
+ ModelView works with Rails, but Rails is definitely not a requirement
14
+
15
+ ### How to use ModelView
16
+
17
+ 1) Include the yem in your gemfile
18
+ ```ruby
19
+ gem "model_view", '~> 0.1'
20
+ ```
21
+ 2) Create a view class (we put these in `app/model_views`)
22
+
23
+ ```ruby
24
+ class PersonView
25
+ extend ModelView
26
+
27
+ end
28
+ ```
29
+ 3) Define fields
30
+
31
+ ```ruby
32
+ field :name
33
+
34
+ field(:first_name) { |person| person.name.split(' ').first }
35
+
36
+ field :is_current_user, context[:current_user] do |person|
37
+ person.name.split(' ').first
38
+ end
39
+ ```
40
+ 4) Let ModelView serialise an instance
41
+
42
+ ```ruby
43
+ person = Person.find(1)
44
+ PersonView.as_hash(person, context: {current_user: current_user})
45
+ => {
46
+ name: "Billy Bob",
47
+ first_name: "Billy",
48
+ is_current_user: false
49
+ }
50
+ ```
51
+
52
+ #### Scopes
53
+
54
+ Scopes allows you to create serialisation snippets that can be composed
55
+
56
+ Example:
57
+ ```ruby
58
+ field :id
59
+
60
+ scope :demographics do
61
+ field :name
62
+ field(:first_name) { |person| person.name.split(' ').first }
63
+ field(:last_name) { |person| person.name.split(' ').first }
64
+ end
65
+
66
+ scope :status do
67
+ field :is_current_user, context[:current_user] do |person|
68
+ person.name.split(' ').first
69
+ end
70
+ end
71
+
72
+ scope :all do
73
+ extend_scope :demographics
74
+ extend_scope :status
75
+ end
76
+ ```
77
+
78
+ ```ruby
79
+ person = Person.find(1)
80
+
81
+ PersonView.as_hash(person, context: {current_user: current_user})
82
+ => {
83
+ id: 1
84
+ }
85
+
86
+
87
+ PersonView.as_hash(person, context: {current_user: current_user, scope: :demographics})
88
+ => {
89
+ id: 1,
90
+ name: "Billy Bob",
91
+ first_name: "Billy",
92
+ last_name: "Bob",
93
+ }
94
+
95
+ PersonView.as_hash(person, context: {current_user: current_user, scope: :all})
96
+ => {
97
+ id: 1,
98
+ name: "Billy Bob",
99
+ first_name: "Billy",
100
+ last_name: "Bob",
101
+ is_current_user: true
102
+ }
103
+ ```
@@ -36,8 +36,10 @@ module ModelView
36
36
  @scopes
37
37
  end
38
38
 
39
- def as_hash(object, scope=nil, context={})
40
- ModelView::Resolver.resolve(object, @scopes, scope || ROOT, context)
39
+ def as_hash(object, opts={} )
40
+ scope = opts[:scope] || ROOT
41
+ context = opts[:context] || {}
42
+ ModelView::Resolver.resolve(object, @scopes, scope , context)
41
43
  end
42
44
 
43
45
  private
@@ -1,3 +1,3 @@
1
1
  module ModelView
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_view
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Pretorius