supa 0.3.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +29 -0
- data/lib/supa.rb +1 -0
- data/lib/supa/builder.rb +9 -3
- data/lib/supa/command.rb +16 -2
- data/lib/supa/commands/attribute.rb +8 -0
- data/lib/supa/commands/attributes.rb +19 -0
- data/lib/supa/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dca557ba59684f5f970ada4fd9349cbf1f158133
|
4
|
+
data.tar.gz: 1bdc567595c7dd9d3d78413525d598326b4eb71e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a06d61f42e4d96488a0ae35babfa4bbddeb5a39ebf95d9e1f8b1e02c44636e6f6c9714b86e65c64d9d2e324bcb2ee24d690afd40cb3f8bd24dd7f7ce317fb680
|
7
|
+
data.tar.gz: 1bd11c1fd35d5ccf2935a60404622762ad3e239c4faa16776e9b35c63e9b447378c8215b7cf384a16a62f072d624ba15feff18dfd0c92bae29554eb5d1a27e41
|
data/README.md
CHANGED
@@ -256,6 +256,35 @@ ExampleRepresenter.new(example).to_hash
|
|
256
256
|
|
257
257
|
```
|
258
258
|
|
259
|
+
### `attributes`
|
260
|
+
|
261
|
+
It retrieves attributes from correspondingly named instance methods or hash keys when the represented object is a `Hash`.
|
262
|
+
|
263
|
+
```ruby
|
264
|
+
class ExampleRepresenter
|
265
|
+
include Supa::Representable
|
266
|
+
|
267
|
+
define do
|
268
|
+
attributes :first_name, :last_name, hide_when_empty: true
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
class Person
|
273
|
+
attr_accessor :first_name, :last_name
|
274
|
+
end
|
275
|
+
|
276
|
+
example = Person.new
|
277
|
+
example.first_name = 'Heidi'
|
278
|
+
example.last_name = 'Shepherd'
|
279
|
+
|
280
|
+
ExampleRepresenter.new(example).to_hash
|
281
|
+
|
282
|
+
#=> {
|
283
|
+
#=> first_name: 'Heidi',
|
284
|
+
#= last_name: 'Shepherd'
|
285
|
+
#=> }
|
286
|
+
```
|
287
|
+
|
259
288
|
### `virtual`
|
260
289
|
|
261
290
|
Virtual is an attribute that doesn't exist in representing object and defind as `string`.
|
data/lib/supa.rb
CHANGED
data/lib/supa/builder.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Supa
|
2
2
|
class Builder
|
3
|
-
|
3
|
+
COMMANDS_WITH_DEFAULT_INTERFACE = %w(attribute virtual object namespace collection append).freeze
|
4
4
|
|
5
5
|
def initialize(subject, representer:, tree:)
|
6
6
|
@subject = subject
|
@@ -8,10 +8,10 @@ module Supa
|
|
8
8
|
@tree = tree
|
9
9
|
end
|
10
10
|
|
11
|
-
|
11
|
+
COMMANDS_WITH_DEFAULT_INTERFACE.each do |command|
|
12
12
|
klass = Supa::Commands.const_get(command.capitalize)
|
13
13
|
|
14
|
-
define_method(command) do |name, options
|
14
|
+
define_method(command) do |name, **options, &block|
|
15
15
|
klass.new(@subject,
|
16
16
|
representer: @representer,
|
17
17
|
tree: @tree,
|
@@ -21,6 +21,12 @@ module Supa
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
+
def attributes(*names, **options)
|
25
|
+
Supa::Commands::Attributes.new(
|
26
|
+
@subject, representer: @representer, tree: @tree, name: names, options: options
|
27
|
+
).represent
|
28
|
+
end
|
29
|
+
|
24
30
|
def to_hash
|
25
31
|
@tree.to_hash
|
26
32
|
end
|
data/lib/supa/command.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module Supa
|
2
2
|
class Command
|
3
|
+
UnsupportedModifier = Class.new(StandardError)
|
4
|
+
|
3
5
|
def initialize(subject, representer:, tree:, name:, options: {}, &block)
|
4
6
|
@subject = subject
|
5
7
|
@representer = representer
|
@@ -16,8 +18,20 @@ module Supa
|
|
16
18
|
private
|
17
19
|
|
18
20
|
def value
|
19
|
-
return @
|
20
|
-
apply_render_flags(raw_value)
|
21
|
+
return @value if defined?(@value)
|
22
|
+
value = apply_render_flags(raw_value)
|
23
|
+
@value = modifier ? @representer.send(modifier, value) : value
|
24
|
+
end
|
25
|
+
|
26
|
+
def modifier
|
27
|
+
return @modifier if defined?(@modifier)
|
28
|
+
|
29
|
+
if @options.key?(:modifier) && !@options[:modifier].is_a?(Symbol)
|
30
|
+
raise UnsupportedModifier,
|
31
|
+
"Object #{@options[:modifier].inspect} is not a valid modifier. Please provide symbolized method name."
|
32
|
+
end
|
33
|
+
|
34
|
+
@modifier = @options[:modifier]
|
21
35
|
end
|
22
36
|
|
23
37
|
def apply_render_flags(val)
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Supa
|
2
|
+
module Commands
|
3
|
+
class Attributes < Supa::Command
|
4
|
+
def represent
|
5
|
+
names.each do |name|
|
6
|
+
Supa::Commands::Attribute.new(
|
7
|
+
@subject, representer: @representer, tree: @tree, name: name, options: @options
|
8
|
+
).represent
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def names
|
15
|
+
@name
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/supa/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: supa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- dasnotme
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: exe
|
13
13
|
cert_chain: []
|
14
|
-
date: 2017-01
|
14
|
+
date: 2017-03-01 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: bundler
|
@@ -167,6 +167,7 @@ files:
|
|
167
167
|
- lib/supa/command.rb
|
168
168
|
- lib/supa/commands/append.rb
|
169
169
|
- lib/supa/commands/attribute.rb
|
170
|
+
- lib/supa/commands/attributes.rb
|
170
171
|
- lib/supa/commands/collection.rb
|
171
172
|
- lib/supa/commands/namespace.rb
|
172
173
|
- lib/supa/commands/object.rb
|
@@ -194,7 +195,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
194
195
|
version: '0'
|
195
196
|
requirements: []
|
196
197
|
rubyforge_project:
|
197
|
-
rubygems_version: 2.
|
198
|
+
rubygems_version: 2.5.1
|
198
199
|
signing_key:
|
199
200
|
specification_version: 4
|
200
201
|
summary: Ruby object → JSON serialization.
|