model_view 0.1.3 → 0.2.0

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: aef140793172f7e2692c3fb46cf86a5d9ee94acb
4
- data.tar.gz: fbd1d07a10c07fd055db7e126d05112902b80d85
3
+ metadata.gz: 5bbb9e6927b9d2a9a2350d4b2b9fa3bb914a125d
4
+ data.tar.gz: 58d8e67a612acda3b04469af9573ef2f71bfc2bb
5
5
  SHA512:
6
- metadata.gz: d1eba489732401a4448576c68a076ab27a1af43b623108a65fec0056c2c4c6689823595027cce93f55e652427a5eb64db583d1c77a06d3362aa96dfa418a65d9
7
- data.tar.gz: bb8f406b8de03109e1e93061776097b69bac01f6451cf6bd977979e22044420b0c6923ceb58ff4ee1d5d853018511e1ab26eb6b3531d0b14e59c2ae911139ac9
6
+ metadata.gz: 68014a9a7f17ad08e169fed2cb92d2582e886821e51bb7690412cec03263a97204bd1c80f127855f907ef52050abbbf42c5edf0b52e57fe64f99d3ab81233250
7
+ data.tar.gz: 4283cf11f8300868265c6763fef87a6626d0cd61926bfa822fc7b23099848ba4e12d68f60fd8f59d174e05657cec2b2497657090d714aa75373429ccdb13da22
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ # 0.2.0
4
+ * ModelView can add a convenience method to the model class
5
+
6
+
3
7
  # 0.1.3
4
8
  * Readme fixes
5
9
 
data/README.md CHANGED
@@ -49,6 +49,35 @@ PersonView.as_hash(person, context: {current_user: current_user})
49
49
  }
50
50
  ```
51
51
 
52
+ #### 🐒-patching the model
53
+
54
+ ModelView can add a convenience method to the model class
55
+
56
+ Example
57
+ ```ruby
58
+ class PersonModelView
59
+ model Person
60
+ field :id
61
+ end
62
+
63
+ p = Person.find 1
64
+
65
+ p.as_hash(context: {current_user: current_user})
66
+ => {
67
+ id: 1
68
+ }
69
+ ```
70
+
71
+ When using ModelView in Rails, remember to add an initializer that requires your model views.
72
+
73
+ Example initializer:
74
+ ```ruby
75
+ # require.rb
76
+ Dir["#{Rails.root}/app/model_views/*.rb"].each do |file|
77
+ require File.basename(file, File.extname(file))
78
+ end
79
+ ```
80
+
52
81
  #### Scopes
53
82
 
54
83
  Scopes allows you to create serialisation snippets that can be composed
@@ -60,7 +89,7 @@ field :id
60
89
  scope :demographics do
61
90
  field :name
62
91
  field(:first_name) { |person| person.name.split(' ').first }
63
- field(:last_name) { |person| person.name.split(' ').first }
92
+ field(:last_name) { |person| person.name.split(' ').last }
64
93
  end
65
94
 
66
95
  scope :status do
@@ -78,13 +107,13 @@ end
78
107
  ```ruby
79
108
  person = Person.find(1)
80
109
 
81
- PersonView.as_hash(person, context: {current_user: current_user})
110
+ PersonView.as_hash(person, {context: {current_user: current_user}})
82
111
  => {
83
112
  id: 1
84
113
  }
85
114
 
86
115
 
87
- PersonView.as_hash(person, context: {current_user: current_user, scope: :demographics})
116
+ PersonView.as_hash(person, {context: {current_user: current_user}, scope: :demographics})
88
117
  => {
89
118
  id: 1,
90
119
  name: "Billy Bob",
@@ -92,7 +121,7 @@ PersonView.as_hash(person, context: {current_user: current_user, scope: :demogra
92
121
  last_name: "Bob",
93
122
  }
94
123
 
95
- PersonView.as_hash(person, context: {current_user: current_user, scope: :all})
124
+ PersonView.as_hash(person, {context: {current_user: current_user}, scope: :all})
96
125
  => {
97
126
  id: 1,
98
127
  name: "Billy Bob",
@@ -1,3 +1,3 @@
1
1
  module ModelView
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/model_view.rb CHANGED
@@ -36,12 +36,21 @@ module ModelView
36
36
  @scopes
37
37
  end
38
38
 
39
- def as_hash(object, opts={} )
39
+ def as_hash(object, opts={})
40
40
  scope = opts[:scope] || ROOT
41
41
  context = opts[:context] || {}
42
42
  ModelView::Resolver.resolve(object, @scopes, scope , context)
43
43
  end
44
44
 
45
+ def model(model_class)
46
+ model_view_class = self
47
+ model_class.class_eval do
48
+ define_method(:as_hash) do |opts|
49
+ model_view_class.as_hash(self, opts)
50
+ end
51
+ end
52
+ end
53
+
45
54
  private
46
55
 
47
56
  def add_scope(scope_name)
@@ -143,4 +143,29 @@ describe ModelView do
143
143
  end
144
144
  end
145
145
  end
146
+
147
+ describe :model do
148
+ before do
149
+ class MyModel
150
+
151
+ end
152
+ end
153
+
154
+ it "adds an as_hash method to the model" do
155
+ dummy_class.model MyModel
156
+
157
+ expect(MyModel.new).to respond_to(:as_hash)
158
+ end
159
+
160
+ it "adds an as_hash method to the model that defers to the model view's as_hash method" do
161
+ dummy_class.model MyModel
162
+ instance = MyModel.new
163
+
164
+ expect(dummy_class).to receive(:as_hash).with(instance, {})
165
+
166
+ instance.as_hash({})
167
+ end
168
+
169
+ end
170
+
146
171
  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.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Pretorius