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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8368930840dc4d4f13caeebed1c39874729f171b
4
- data.tar.gz: f43ae08a91fc09e14b3e8094c8441402971461a5
3
+ metadata.gz: dca557ba59684f5f970ada4fd9349cbf1f158133
4
+ data.tar.gz: 1bdc567595c7dd9d3d78413525d598326b4eb71e
5
5
  SHA512:
6
- metadata.gz: 7440feb89f238add3e25ea2d0530827ab43bc58c8a23fffb0ae7e38348867d89f5159cadd7d5d29d10e72fdc6cc9fbdbe8863aecf3a5b76be77f5ed73cefa5dd
7
- data.tar.gz: fee29a4bb5555ad0653fe08fe5cc1d7a4250b15100c11a8648fee5daf3ad79b748e9c583dce6676953691c73e55d4eeb411e4bdf033e482c58d72b498e3e1dbc
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
@@ -2,6 +2,7 @@ require 'supa/version'
2
2
  require 'supa/representable'
3
3
  require 'supa/command'
4
4
  require 'supa/commands/attribute'
5
+ require 'supa/commands/attributes'
5
6
  require 'supa/commands/virtual'
6
7
  require 'supa/commands/object'
7
8
  require 'supa/commands/namespace'
data/lib/supa/builder.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Supa
2
2
  class Builder
3
- COMMANDS = %w(attribute virtual object namespace collection append).freeze
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
- COMMANDS.each do |command|
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 = {}, &block|
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 @representer.send(@options[:modifier], apply_render_flags(raw_value)) if @options[:modifier]
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)
@@ -2,8 +2,16 @@ module Supa
2
2
  module Commands
3
3
  class Attribute < Supa::Command
4
4
  def represent
5
+ return if hide?
6
+
5
7
  @tree[@name] = value
6
8
  end
9
+
10
+ private
11
+
12
+ def hide?
13
+ value.nil? && hide_when_empty?
14
+ end
7
15
  end
8
16
  end
9
17
  end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Supa
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '0.4.1'.freeze
3
3
  end
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.3.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-26 00:00:00.000000000 Z
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.4.6
198
+ rubygems_version: 2.5.1
198
199
  signing_key:
199
200
  specification_version: 4
200
201
  summary: Ruby object → JSON serialization.