grape-entity 0.2.0 → 0.3.0
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/CHANGELOG.markdown +14 -1
- data/README.markdown +6 -8
- data/grape-entity.gemspec +1 -1
- data/lib/grape_entity/entity.rb +22 -2
- data/lib/grape_entity/version.rb +1 -1
- data/spec/grape_entity/entity_spec.rb +56 -0
- metadata +43 -67
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b398596f88871f8170eb2a73142bfdc08ca91370
|
4
|
+
data.tar.gz: 32aec8564a0c5f46522ffae2ac6167083440b8b0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e6ced06ae9b0a8bada53aae31a4eba8d5a58b717797b72adc2953ed4431dc99b21e711b370f027b45c19ac1b27f312a1d1300e29da64964e2b7e70fbc1f3a96c
|
7
|
+
data.tar.gz: 58fb3f143bffa542f8a8cfe1ce3f19f1f924588a670697c1ae596e27a80dd28eefea8ee4a3031d4c7cf89eeea85f6fa7912bdf6400d29474c0273e358bf594f6
|
data/CHANGELOG.markdown
CHANGED
@@ -1,8 +1,21 @@
|
|
1
1
|
Next Release
|
2
2
|
============
|
3
3
|
* Your contribution here.
|
4
|
+
* Fixed bug with to_json.
|
4
5
|
|
5
|
-
0.
|
6
|
+
0.3.0 (2013-03-29)
|
7
|
+
==================
|
8
|
+
|
9
|
+
* Adds `with_options` for block-level exposure setting
|
10
|
+
* DSL instance.entity now optionally accepts options to instantiate on the entity
|
11
|
+
* You can pass symbols to `:if` and `:unless` to simply check for truthiness/falsiness of the specified options key
|
12
|
+
|
13
|
+
0.2.0 (2013-01-11)
|
14
|
+
==================
|
15
|
+
|
16
|
+
* Moved the namespace back to Grape::Entity to preserve compatibility with Grape (via @dblock).
|
17
|
+
|
18
|
+
0.1.0 (2013-01-11)
|
6
19
|
==================
|
7
20
|
|
8
21
|
* Initial Release
|
data/README.markdown
CHANGED
@@ -6,15 +6,13 @@
|
|
6
6
|
|
7
7
|
This gem adds Entity support to API frameworks, such as [Grape](https://github.com/intridea/grape). Grape's Entity is an API focussed facade that sits on top of an object model.
|
8
8
|
|
9
|
-
##
|
9
|
+
## What's New
|
10
10
|
|
11
|
-
|
11
|
+
We are currently working on a set of "shoulda-style matchers" (sorry, RSpec only right now -- although they've been done in a way that can support test-unit in the future).
|
12
12
|
|
13
|
-
|
13
|
+
[Grape Entity Matchers](https://github.com/agileanimal/grape-entity-matchers).
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
In this spirit I want to give Entities a life of their own.
|
15
|
+
This is still a work in progress but worth checking out.
|
18
16
|
|
19
17
|
## Reusable Responses with Entities
|
20
18
|
|
@@ -35,7 +33,7 @@ array.
|
|
35
33
|
* define a list of fields which will always be exposed
|
36
34
|
* `expose SYMBOLS, HASH`
|
37
35
|
* HASH keys include `:if`, `:unless`, `:proc`, `:as`, `:using`, `:format_with`, `:documentation`
|
38
|
-
* `:if` and `:unless` accept hashes (passed during runtime)
|
36
|
+
* `:if` and `:unless` accept hashes (passed during runtime), procs (arguments are object and options), or symbols (checks for presence of the specified key on the options hash)
|
39
37
|
* `expose SYMBOL, { :format_with => :formatter }`
|
40
38
|
* expose a value, formatting it first
|
41
39
|
* `:format_with` can only be applied to one exposure at a time
|
@@ -196,4 +194,4 @@ MIT License. See LICENSE for details.
|
|
196
194
|
|
197
195
|
## Copyright
|
198
196
|
|
199
|
-
Copyright (c) 2010-
|
197
|
+
Copyright (c) 2010-2013 Michael Bleigh, and Intridea, Inc.
|
data/grape-entity.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.platform = Gem::Platform::RUBY
|
8
8
|
s.authors = ["Michael Bleigh"]
|
9
9
|
s.email = ["michael@intridea.com"]
|
10
|
-
s.homepage = "https://github.com/
|
10
|
+
s.homepage = "https://github.com/intridea/grape-entity"
|
11
11
|
s.summary = %q{A simple facade for managing the relationship between your model and API.}
|
12
12
|
s.description = %q{Extracted from Grape, A Ruby framework for rapid API development with great conventions.}
|
13
13
|
s.license = "MIT"
|
data/lib/grape_entity/entity.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'multi_json'
|
2
|
+
|
1
3
|
module Grape
|
2
4
|
# An Entity is a lightweight structure that allows you to easily
|
3
5
|
# represent data from your application in a consistent and abstracted
|
@@ -90,8 +92,8 @@ module Grape
|
|
90
92
|
end
|
91
93
|
|
92
94
|
# Instantiates an entity version of this object.
|
93
|
-
def entity
|
94
|
-
self.class.entity_class.new(self)
|
95
|
+
def entity(options = {})
|
96
|
+
self.class.entity_class.new(self, options)
|
95
97
|
end
|
96
98
|
end
|
97
99
|
|
@@ -122,6 +124,7 @@ module Grape
|
|
122
124
|
# field, typically the value is a hash with two fields, type and desc.
|
123
125
|
def self.expose(*args, &block)
|
124
126
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
127
|
+
options = (@block_options ||= []).inject({}){|final, step| final.merge!(step)}.merge(options)
|
125
128
|
|
126
129
|
if args.size > 1
|
127
130
|
raise ArgumentError, "You may not use the :as option on multi-attribute exposures." if options[:as]
|
@@ -137,6 +140,21 @@ module Grape
|
|
137
140
|
end
|
138
141
|
end
|
139
142
|
|
143
|
+
# Set options that will be applied to any exposures declared inside the block.
|
144
|
+
#
|
145
|
+
# @example Multi-exposure if
|
146
|
+
#
|
147
|
+
# class MyEntity < Grape::Entity
|
148
|
+
# with_options :if => {:awesome => true} do
|
149
|
+
# expose :awesome, :sweet
|
150
|
+
# end
|
151
|
+
# end
|
152
|
+
def self.with_options(options)
|
153
|
+
(@block_options ||= []).push(options)
|
154
|
+
yield
|
155
|
+
@block_options.pop
|
156
|
+
end
|
157
|
+
|
140
158
|
# Returns a hash of exposures that have been declared for this Entity or ancestors. The keys
|
141
159
|
# are symbolized references to methods on the containing object, the values are
|
142
160
|
# the options that were passed into expose.
|
@@ -376,11 +394,13 @@ module Grape
|
|
376
394
|
case if_condition
|
377
395
|
when Hash; if_condition.each_pair{|k,v| return false if options[k.to_sym] != v }
|
378
396
|
when Proc; return false unless if_condition.call(object, options)
|
397
|
+
when Symbol; return false unless options[if_condition]
|
379
398
|
end
|
380
399
|
|
381
400
|
case unless_condition
|
382
401
|
when Hash; unless_condition.each_pair{|k,v| return false if options[k.to_sym] == v}
|
383
402
|
when Proc; return false if unless_condition.call(object, options)
|
403
|
+
when Symbol; return false if options[unless_condition]
|
384
404
|
end
|
385
405
|
|
386
406
|
true
|
data/lib/grape_entity/version.rb
CHANGED
@@ -105,6 +105,40 @@ describe Grape::Entity do
|
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
108
|
+
describe '.with_options' do
|
109
|
+
it 'should apply the options to all exposures inside' do
|
110
|
+
subject.class_eval do
|
111
|
+
with_options(:if => {:awesome => true}) do
|
112
|
+
expose :awesome_thing, :using => 'Awesome'
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
subject.exposures[:awesome_thing].should == {:if => {:awesome => true}, :using => 'Awesome'}
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'should allow for nested .with_options' do
|
120
|
+
subject.class_eval do
|
121
|
+
with_options(:if => {:awesome => true}) do
|
122
|
+
with_options(:using => 'Something') do
|
123
|
+
expose :awesome_thing
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
subject.exposures[:awesome_thing].should == {:if => {:awesome => true}, :using => 'Something'}
|
129
|
+
end
|
130
|
+
|
131
|
+
it 'should allow for overrides' do
|
132
|
+
subject.class_eval do
|
133
|
+
with_options(:if => {:awesome => true}) do
|
134
|
+
expose :less_awesome_thing, :if => {:awesome => false}
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
subject.exposures[:less_awesome_thing].should == {:if => {:awesome => false}}
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
108
142
|
describe '.represent' do
|
109
143
|
it 'returns a single entity if called with one object' do
|
110
144
|
subject.represent(Object.new).should be_kind_of(subject)
|
@@ -502,6 +536,24 @@ describe Grape::Entity do
|
|
502
536
|
subject.send(:conditions_met?, exposure_options, :condition1 => true, :condition2 => true, :other => true).should be_true
|
503
537
|
end
|
504
538
|
|
539
|
+
it 'looks for presence/truthiness if a symbol is passed' do
|
540
|
+
exposure_options = {:if => :condition1}
|
541
|
+
|
542
|
+
subject.send(:conditions_met?, exposure_options, {}).should be_false
|
543
|
+
subject.send(:conditions_met?, exposure_options, {:condition1 => true}).should be_true
|
544
|
+
subject.send(:conditions_met?, exposure_options, {:condition1 => false}).should be_false
|
545
|
+
subject.send(:conditions_met?, exposure_options, {:condition1 => nil}).should be_false
|
546
|
+
end
|
547
|
+
|
548
|
+
it 'looks for absence/falsiness if a symbol is passed' do
|
549
|
+
exposure_options = {:unless => :condition1}
|
550
|
+
|
551
|
+
subject.send(:conditions_met?, exposure_options, {}).should be_true
|
552
|
+
subject.send(:conditions_met?, exposure_options, {:condition1 => true}).should be_false
|
553
|
+
subject.send(:conditions_met?, exposure_options, {:condition1 => false}).should be_true
|
554
|
+
subject.send(:conditions_met?, exposure_options, {:condition1 => nil}).should be_true
|
555
|
+
end
|
556
|
+
|
505
557
|
it 'only passes through proc :if exposure if it returns truthy value' do
|
506
558
|
exposure_options = {:if => lambda{|_,opts| opts[:true]}}
|
507
559
|
|
@@ -571,6 +623,10 @@ describe Grape::Entity do
|
|
571
623
|
it 'has an object of itself' do
|
572
624
|
instance.entity.object.should == instance
|
573
625
|
end
|
626
|
+
|
627
|
+
it 'should instantiate with options if provided' do
|
628
|
+
instance.entity(:awesome => true).options.should == {:awesome => true}
|
629
|
+
end
|
574
630
|
end
|
575
631
|
end
|
576
632
|
end
|
metadata
CHANGED
@@ -1,126 +1,111 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-entity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
version: 0.2.0
|
4
|
+
version: 0.3.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Michael Bleigh
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
11
|
+
date: 2013-03-29 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
15
20
|
type: :runtime
|
16
21
|
prerelease: false
|
17
|
-
name: activesupport
|
18
22
|
version_requirements: !ruby/object:Gem::Requirement
|
19
|
-
none: false
|
20
23
|
requirements:
|
21
|
-
- -
|
24
|
+
- - '>='
|
22
25
|
- !ruby/object:Gem::Version
|
23
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: multi_json
|
24
29
|
requirement: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
30
|
requirements:
|
27
|
-
- -
|
31
|
+
- - '>='
|
28
32
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
30
|
-
- !ruby/object:Gem::Dependency
|
33
|
+
version: 1.3.2
|
31
34
|
type: :runtime
|
32
35
|
prerelease: false
|
33
|
-
name: multi_json
|
34
36
|
version_requirements: !ruby/object:Gem::Requirement
|
35
|
-
none: false
|
36
37
|
requirements:
|
37
|
-
- -
|
38
|
+
- - '>='
|
38
39
|
- !ruby/object:Gem::Version
|
39
40
|
version: 1.3.2
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
40
43
|
requirement: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
44
|
requirements:
|
43
|
-
- -
|
45
|
+
- - '>='
|
44
46
|
- !ruby/object:Gem::Version
|
45
|
-
version:
|
46
|
-
- !ruby/object:Gem::Dependency
|
47
|
+
version: '0'
|
47
48
|
type: :development
|
48
49
|
prerelease: false
|
49
|
-
name: rake
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
none: false
|
52
51
|
requirements:
|
53
|
-
- -
|
52
|
+
- - '>='
|
54
53
|
- !ruby/object:Gem::Version
|
55
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: maruku
|
56
57
|
requirement: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
|
-
- !ruby/object:Gem::Dependency
|
63
62
|
type: :development
|
64
63
|
prerelease: false
|
65
|
-
name: maruku
|
66
64
|
version_requirements: !ruby/object:Gem::Requirement
|
67
|
-
none: false
|
68
65
|
requirements:
|
69
|
-
- -
|
66
|
+
- - '>='
|
70
67
|
- !ruby/object:Gem::Version
|
71
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: yard
|
72
71
|
requirement: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
72
|
requirements:
|
75
|
-
- -
|
73
|
+
- - '>='
|
76
74
|
- !ruby/object:Gem::Version
|
77
75
|
version: '0'
|
78
|
-
- !ruby/object:Gem::Dependency
|
79
76
|
type: :development
|
80
77
|
prerelease: false
|
81
|
-
name: yard
|
82
78
|
version_requirements: !ruby/object:Gem::Requirement
|
83
|
-
none: false
|
84
79
|
requirements:
|
85
|
-
- -
|
80
|
+
- - '>='
|
86
81
|
- !ruby/object:Gem::Version
|
87
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
88
85
|
requirement: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
86
|
requirements:
|
91
|
-
- -
|
87
|
+
- - ~>
|
92
88
|
- !ruby/object:Gem::Version
|
93
|
-
version: '
|
94
|
-
- !ruby/object:Gem::Dependency
|
89
|
+
version: '2.9'
|
95
90
|
type: :development
|
96
91
|
prerelease: false
|
97
|
-
name: rspec
|
98
92
|
version_requirements: !ruby/object:Gem::Requirement
|
99
|
-
none: false
|
100
93
|
requirements:
|
101
94
|
- - ~>
|
102
95
|
- !ruby/object:Gem::Version
|
103
96
|
version: '2.9'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: bundler
|
104
99
|
requirement: !ruby/object:Gem::Requirement
|
105
|
-
none: false
|
106
100
|
requirements:
|
107
|
-
- -
|
101
|
+
- - '>='
|
108
102
|
- !ruby/object:Gem::Version
|
109
|
-
version: '
|
110
|
-
- !ruby/object:Gem::Dependency
|
103
|
+
version: '0'
|
111
104
|
type: :development
|
112
105
|
prerelease: false
|
113
|
-
name: bundler
|
114
106
|
version_requirements: !ruby/object:Gem::Requirement
|
115
|
-
none: false
|
116
107
|
requirements:
|
117
|
-
- -
|
118
|
-
- !ruby/object:Gem::Version
|
119
|
-
version: '0'
|
120
|
-
requirement: !ruby/object:Gem::Requirement
|
121
|
-
none: false
|
122
|
-
requirements:
|
123
|
-
- - ! '>='
|
108
|
+
- - '>='
|
124
109
|
- !ruby/object:Gem::Version
|
125
110
|
version: '0'
|
126
111
|
description: Extracted from Grape, A Ruby framework for rapid API development with
|
@@ -148,38 +133,29 @@ files:
|
|
148
133
|
- lib/grape_entity/version.rb
|
149
134
|
- spec/grape_entity/entity_spec.rb
|
150
135
|
- spec/spec_helper.rb
|
151
|
-
homepage: https://github.com/
|
136
|
+
homepage: https://github.com/intridea/grape-entity
|
152
137
|
licenses:
|
153
138
|
- MIT
|
139
|
+
metadata: {}
|
154
140
|
post_install_message:
|
155
141
|
rdoc_options: []
|
156
142
|
require_paths:
|
157
143
|
- lib
|
158
144
|
required_ruby_version: !ruby/object:Gem::Requirement
|
159
|
-
none: false
|
160
145
|
requirements:
|
161
|
-
- -
|
146
|
+
- - '>='
|
162
147
|
- !ruby/object:Gem::Version
|
163
|
-
segments:
|
164
|
-
- 0
|
165
|
-
hash: -1479047909720369637
|
166
148
|
version: '0'
|
167
149
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
168
|
-
none: false
|
169
150
|
requirements:
|
170
|
-
- -
|
151
|
+
- - '>='
|
171
152
|
- !ruby/object:Gem::Version
|
172
|
-
segments:
|
173
|
-
- 0
|
174
|
-
hash: -1479047909720369637
|
175
153
|
version: '0'
|
176
154
|
requirements: []
|
177
155
|
rubyforge_project: grape-entity
|
178
|
-
rubygems_version:
|
156
|
+
rubygems_version: 2.0.0
|
179
157
|
signing_key:
|
180
|
-
specification_version:
|
158
|
+
specification_version: 4
|
181
159
|
summary: A simple facade for managing the relationship between your model and API.
|
182
|
-
test_files:
|
183
|
-
- spec/grape_entity/entity_spec.rb
|
184
|
-
- spec/spec_helper.rb
|
160
|
+
test_files: []
|
185
161
|
has_rdoc:
|