representable 3.0.4 → 3.1.1

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.
Files changed (98) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ci.yml +17 -0
  3. data/CHANGES.md +10 -0
  4. data/Gemfile +4 -7
  5. data/LICENSE +1 -1
  6. data/README.md +4 -3
  7. data/Rakefile +1 -6
  8. data/lib/representable.rb +18 -25
  9. data/lib/representable/binding.rb +32 -12
  10. data/lib/representable/cached.rb +1 -1
  11. data/lib/representable/coercion.rb +8 -6
  12. data/lib/representable/config.rb +8 -3
  13. data/lib/representable/debug.rb +23 -15
  14. data/lib/representable/declarative.rb +8 -3
  15. data/lib/representable/decorator.rb +1 -1
  16. data/lib/representable/definition.rb +7 -2
  17. data/lib/representable/deserializer.rb +4 -3
  18. data/lib/representable/for_collection.rb +1 -1
  19. data/lib/representable/hash.rb +6 -2
  20. data/lib/representable/hash/allow_symbols.rb +9 -11
  21. data/lib/representable/hash/binding.rb +1 -0
  22. data/lib/representable/hash/collection.rb +4 -2
  23. data/lib/representable/hash_methods.rb +3 -2
  24. data/lib/representable/insert.rb +1 -1
  25. data/lib/representable/json.rb +5 -7
  26. data/lib/representable/json/collection.rb +3 -0
  27. data/lib/representable/json/hash.rb +1 -0
  28. data/lib/representable/object.rb +1 -1
  29. data/lib/representable/object/binding.rb +5 -1
  30. data/lib/representable/option.rb +19 -0
  31. data/lib/representable/pipeline.rb +3 -2
  32. data/lib/representable/pipeline_factories.rb +4 -2
  33. data/lib/representable/populator.rb +1 -1
  34. data/lib/representable/represent.rb +1 -0
  35. data/lib/representable/serializer.rb +2 -1
  36. data/lib/representable/version.rb +1 -1
  37. data/lib/representable/virtus_coercion.rb +38 -0
  38. data/lib/representable/xml.rb +7 -10
  39. data/lib/representable/xml/binding.rb +2 -3
  40. data/lib/representable/yaml.rb +3 -3
  41. data/lib/representable/yaml/binding.rb +1 -0
  42. data/representable.gemspec +3 -3
  43. data/test/as_test.rb +4 -4
  44. data/test/binding_test.rb +10 -10
  45. data/test/cached_test.rb +19 -19
  46. data/test/class_test.rb +7 -7
  47. data/test/coercion_test.rb +33 -22
  48. data/test/config/inherit_test.rb +14 -14
  49. data/test/config_test.rb +18 -18
  50. data/test/decorator_scope_test.rb +3 -3
  51. data/test/decorator_test.rb +17 -17
  52. data/test/default_test.rb +7 -7
  53. data/test/definition_test.rb +32 -32
  54. data/test/{example.rb → examples/example.rb} +0 -0
  55. data/test/exec_context_test.rb +6 -6
  56. data/test/features_test.rb +3 -3
  57. data/test/filter_test.rb +6 -6
  58. data/test/for_collection_test.rb +2 -2
  59. data/test/generic_test.rb +3 -3
  60. data/test/getter_setter_test.rb +5 -5
  61. data/test/hash_test.rb +19 -19
  62. data/test/heritage_test.rb +4 -4
  63. data/test/if_test.rb +6 -6
  64. data/test/include_exclude_test.rb +12 -12
  65. data/test/inherit_test.rb +15 -15
  66. data/test/inline_test.rb +11 -11
  67. data/test/instance_test.rb +29 -29
  68. data/test/is_representable_test.rb +10 -10
  69. data/test/json_test.rb +7 -7
  70. data/test/lonely_test.rb +16 -16
  71. data/test/nested_test.rb +7 -7
  72. data/test/object_test.rb +7 -7
  73. data/test/option_test.rb +36 -0
  74. data/test/parse_pipeline_test.rb +3 -3
  75. data/test/pipeline_test.rb +43 -43
  76. data/test/populator_test.rb +15 -15
  77. data/test/prepare_test.rb +2 -2
  78. data/test/private_options_test.rb +2 -2
  79. data/test/reader_writer_test.rb +2 -2
  80. data/test/render_nil_test.rb +2 -2
  81. data/test/represent_test.rb +4 -4
  82. data/test/representable_test.rb +27 -27
  83. data/test/schema_test.rb +5 -5
  84. data/test/serialize_deserialize_test.rb +2 -2
  85. data/test/skip_test.rb +10 -10
  86. data/test/stringify_hash_test.rb +3 -3
  87. data/test/test_helper.rb +4 -2
  88. data/test/uncategorized_test.rb +10 -10
  89. data/test/user_options_test.rb +4 -4
  90. data/test/virtus_coercion_test.rb +52 -0
  91. data/test/wrap_test.rb +11 -11
  92. data/test/xml_namespace_test.rb +1 -1
  93. data/test/xml_test.rb +6 -6
  94. data/test/yaml_test.rb +20 -20
  95. metadata +81 -15
  96. data/.travis.yml +0 -16
  97. data/lib/representable/autoload.rb +0 -14
  98. data/test/mongoid_test.rb +0 -31
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 04ca3309b8af78dd1eaa8b4d47c457323180a881
4
- data.tar.gz: c7d43dc372fc53576a6b64f22d3fc0bd1bff5a4e
2
+ SHA256:
3
+ metadata.gz: 7fac946d38143671ea43e0a6981d35dc7e8e8a0026af631fe53bb67d30f413c0
4
+ data.tar.gz: 3f98fb8a4ff2ea929ecd3afd9c97bc32fa7b7959d696a5c4ea4d5c9aecfac4d4
5
5
  SHA512:
6
- metadata.gz: 528696ff8d8eae7b409566d9831a0117a90a361fdabe5a0345d0e5accac5cfbb2f3bfae5a34589000b5bdbf2632f7951356861215d210172c602b581694e68dc
7
- data.tar.gz: 4d7409a62a9a25d31fc1c67d20e2b32b9cd0a1d8e9987b2cbe118bd4e069fe271dd82127f3e7656b92f01e8f1266524965def4089e439b4a1eac370e13a06bff
6
+ metadata.gz: 781a63fe0c5a650bc54d007dd6c16b20dec422b8312fa831b938ad0b78a1ffe5c6cbf640de1406574943035cbbc03925446e9ddd0cc640c1a0c9e9212b75d71e
7
+ data.tar.gz: fbedd21d3373cca798829c164357cc8c6d1470c9fe16b4d404b67a1df0f9eeebdb13a1ae262f585d54c4a2c4209573b23f211b4ca1430e801c21dca4c9451b4f
@@ -0,0 +1,17 @@
1
+ name: CI
2
+ on: [push, pull_request]
3
+ jobs:
4
+ test:
5
+ strategy:
6
+ fail-fast: false
7
+ matrix:
8
+ # Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
9
+ ruby: [2.5, 2.6, 2.7, '3.0', head]
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - uses: actions/checkout@v2
13
+ - uses: ruby/setup-ruby@v1
14
+ with:
15
+ ruby-version: ${{ matrix.ruby }}
16
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
17
+ - run: bundle exec rake
data/CHANGES.md CHANGED
@@ -1,3 +1,13 @@
1
+ # 3.1.1
2
+
3
+ * Upgrade `trailblazer-option` to `0.1.1` which supports passing an empty `keyword_arguments`.
4
+
5
+ # 3.1.0
6
+ * Remove circular require
7
+ * Use Dry-types 1.0.0+ as coercion library
8
+ * Renamed Coercion to VirtusCoercion to support old codebases
9
+ * Replace `declarative-option` with [`trailblazer-option`](https://github.com/trailblazer/trailblazer-option)
10
+
1
11
  # 3.0.4
2
12
 
3
13
  * Add proper XML namespace support.
data/Gemfile CHANGED
@@ -1,14 +1,11 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
5
  group :test do
6
- gem 'nokogiri', '~> 1.6.8', require: false
7
- gem "multi_json", require: false
8
6
  gem "minitest-line"
7
+ gem "multi_json", require: false
8
+ gem "nokogiri", require: false
9
9
  end
10
+ gem 'pry-byebug'
10
11
 
11
- # gem "declarative", path: "../declarative"
12
- # gem "declarative", github: "apotonick/declarative"
13
-
14
- # gem "uber","0.0.15" #, path: "../uber"
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2011 - 2013 Nick Sutterer and the roar contributors
1
+ Copyright (c) 2011 - 2021 Nick Sutterer and the roar contributors
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -130,6 +130,7 @@ class AlbumRepresenter < Representable::Decorator
130
130
  property :track
131
131
  collection :composers
132
132
  end
133
+ end
133
134
  ```
134
135
 
135
136
  ## More
@@ -141,8 +142,8 @@ Please check the [official documentation for more](http://trailblazer.to/gems/re
141
142
 
142
143
  ## Installation
143
144
 
144
- The representable gem runs with all Ruby versions >= 1.9.3.
145
-
145
+ The representable gem runs with all Ruby versions >= 2.4.0.
146
+ t
146
147
  ```ruby
147
148
  gem 'representable'
148
149
  ```
@@ -168,7 +169,7 @@ gem 'nokogiri'
168
169
 
169
170
  Representable started as a heavily simplified fork of the ROXML gem. Big thanks to Ben Woosley for his extremely inspiring work.
170
171
 
171
- * Copyright (c) 2011-2016 Nick Sutterer <apotonick@gmail.com>
172
+ * Copyright (c) 2011-2020 Nick Sutterer <apotonick@gmail.com>
172
173
  * ROXML is Copyright (c) 2004-2009 Ben Woosley, Zak Mandhro and Anders Engstrom.
173
174
 
174
175
  Representable is released under the [MIT License](http://www.opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,3 +1,4 @@
1
+ require "bundler/gem_tasks"
1
2
  require 'bundler/setup'
2
3
  require 'rake/testtask'
3
4
 
@@ -9,9 +10,3 @@ Rake::TestTask.new(:test) do |test|
9
10
  test.test_files = FileList['test/**/*_test.rb']
10
11
  test.verbose = true
11
12
  end
12
-
13
- Rake::TestTask.new(:dtest) do |test|
14
- test.libs << 'test-with-deprecations'
15
- test.test_files = FileList['test-with-deprecations/**/*_test.rb']
16
- test.verbose = true
17
- end
data/lib/representable.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  require "uber/delegates"
2
2
  require "uber/callable"
3
- require "declarative/option"
4
3
  require "declarative/schema"
5
4
 
5
+ require "representable/option"
6
6
  require "representable/config"
7
7
  require "representable/definition"
8
8
  require "representable/declarative"
@@ -16,6 +16,17 @@ require "representable/for_collection"
16
16
  require "representable/represent"
17
17
 
18
18
  module Representable
19
+ autoload :Binding, 'representable/binding'
20
+ autoload :HashMethods, 'representable/hash_methods'
21
+ autoload :Decorator, 'representable/decorator'
22
+
23
+ autoload :Hash, 'representable/hash'
24
+ autoload :JSON, 'representable/json'
25
+ autoload :Object, 'representable/object'
26
+ autoload :YAML, 'representable/yaml'
27
+ autoload :XML, 'representable/xml'
28
+
29
+
19
30
  attr_writer :representable_attrs
20
31
 
21
32
  def self.included(base)
@@ -32,7 +43,7 @@ module Representable
32
43
  private
33
44
  # Reads values from +doc+ and sets properties accordingly.
34
45
  def update_properties_from(doc, options, format)
35
- propagated_options = normalize_options(options)
46
+ propagated_options = normalize_options(**options)
36
47
 
37
48
  representable_map!(doc, propagated_options, format, :uncompile_fragment)
38
49
  represented
@@ -40,25 +51,12 @@ private
40
51
 
41
52
  # Compiles the document going through all properties.
42
53
  def create_representation_with(doc, options, format)
43
- propagated_options = normalize_options(options)
54
+ propagated_options = normalize_options(**options)
44
55
 
45
56
  representable_map!(doc, propagated_options, format, :compile_fragment)
46
57
  doc
47
58
  end
48
59
 
49
- class Binding::Map < Array
50
- def call(method, options)
51
- each do |bin|
52
- options[:binding] = bin # this is so much faster than options.merge().
53
- bin.send(method, options)
54
- end
55
- end
56
-
57
- # TODO: Merge with Definitions.
58
- def <<(binding) # can be slow. this is compile time code.
59
- (existing = find { |bin| bin.name == binding.name }) ? self[index(existing)] = binding : super(binding)
60
- end
61
- end
62
60
 
63
61
  def representable_map(options, format)
64
62
  Binding::Map.new(representable_bindings_for(format, options))
@@ -74,9 +72,8 @@ private
74
72
  representable_attrs.collect {|definition| format.build(definition) }
75
73
  end
76
74
 
77
- def normalize_options(options)
78
- return options if options.any?
79
- {user_options: {}}.merge(options) # TODO: use keyword args once we drop 2.0.
75
+ def normalize_options(user_options: {}, **options)
76
+ { user_options: user_options }.merge(options)
80
77
  end
81
78
 
82
79
  # Prepares options for a particular nested representer.
@@ -96,8 +93,8 @@ private
96
93
  @representable_attrs ||= self.class.definitions
97
94
  end
98
95
 
99
- def representation_wrap(*args)
100
- representable_attrs.wrap_for(represented, *args)
96
+ def representation_wrap(options = {})
97
+ representable_attrs.wrap_for(represented, options)
101
98
  end
102
99
 
103
100
  def represented
@@ -118,8 +115,4 @@ private
118
115
  represented.extend(self)
119
116
  end
120
117
  end
121
-
122
- # require "representable/deprecations"
123
118
  end
124
-
125
- require 'representable/autoload'
@@ -1,8 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uber/delegates'
4
+
1
5
  module Representable
2
6
  # The Binding provides methods to read/write the fragment for one property.
3
7
  #
4
8
  # Actually parsing the fragment from the document happens in Binding#read, everything after that is generic.
5
9
  class Binding
10
+ class Map < Array
11
+ def call(method, options)
12
+ each do |bin|
13
+ options[:binding] = bin # this is so much faster than options.merge().
14
+ bin.send(method, options)
15
+ end
16
+ end
17
+
18
+ # TODO: Merge with Definitions.
19
+ def <<(binding) # can be slow. this is compile time code.
20
+ (existing = find { |bin| bin.name == binding.name }) ? self[index(existing)] = binding : super(binding)
21
+ end
22
+ end
23
+
6
24
  class FragmentNotFound
7
25
  end
8
26
 
@@ -29,12 +47,12 @@ module Representable
29
47
  module Deprecatable
30
48
  # Retrieve value and write fragment to the doc.
31
49
  def compile_fragment(options)
32
- render_pipeline(nil, options).(nil, options)
50
+ render_pipeline(nil, options).call(nil, options)
33
51
  end
34
52
 
35
53
  # Parse value from doc and update the model property.
36
54
  def uncompile_fragment(options)
37
- parse_pipeline(options[:doc], options).(options[:doc], options)
55
+ parse_pipeline(options[:doc], options).call(options[:doc], options)
38
56
  end
39
57
  end
40
58
  include Deprecatable
@@ -43,7 +61,7 @@ module Representable
43
61
  def evaluate_option(name, input, options)
44
62
  proc = self[name]
45
63
  # puts "@@@@@ #{self.inspect}, #{name}...... #{self[name]}"
46
- proc.(send(:exec_context, options), options.merge(user_options: options[:options][:user_options], input: input)) # from Uber::Options::Value. # NOTE: this can also be the Proc object if it's not wrapped by Uber:::Value.
64
+ proc.call(exec_context: send(:exec_context, options), keyword_arguments: options.merge(user_options: options[:options][:user_options], input: input)) # from Uber::Options::Value. # NOTE: this can also be the Proc object if it's not wrapped by Uber:::Value.
47
65
  end
48
66
  end
49
67
  include EvaluateOption
@@ -53,29 +71,30 @@ module Representable
53
71
  end
54
72
 
55
73
  def skipable_empty_value?(value)
56
- value.nil? and not self[:render_nil]
74
+ value.nil? and !(self[:render_nil])
57
75
  end
58
76
 
59
77
  def default_for(value)
60
78
  return self[:default] if skipable_empty_value?(value)
79
+
61
80
  value
62
81
  end
63
82
 
64
83
  attr_accessor :cached_representer
65
84
 
66
- require "representable/pipeline_factories"
85
+ require 'representable/pipeline_factories'
67
86
  include Factories
68
87
 
69
- private
88
+ private
70
89
 
71
90
  def setup_exec_context!
72
- @exec_context = ->(options) { options[:represented] } unless self[:exec_context]
73
- @exec_context = ->(options) { self } if self[:exec_context] == :binding
74
- @exec_context = ->(options) { options[:decorator] } if self[:exec_context] == :decorator
91
+ @exec_context = ->(options) { options[:represented] } unless self[:exec_context]
92
+ @exec_context = ->(_options) { self } if self[:exec_context] == :binding
93
+ @exec_context = ->(options) { options[:decorator] } if self[:exec_context] == :decorator
75
94
  end
76
95
 
77
96
  def exec_context(options)
78
- @exec_context.(options)
97
+ @exec_context.call(options)
79
98
  end
80
99
 
81
100
  def parse_pipeline(input, options)
@@ -90,8 +109,9 @@ module Representable
90
109
  module Collection
91
110
  def skipable_empty_value?(value)
92
111
  # TODO: this can be optimized, again.
93
- return true if value.nil? and not self[:render_nil] # FIXME: test this without the "and"
94
- return true if self[:render_empty] == false and value and value.size == 0 # TODO: change in 2.0, don't render emtpy.
112
+ return true if value.nil? && !(self[:render_nil]) # FIXME: test this without the "and"
113
+
114
+ true if (self[:render_empty] == false) && value && value.empty? # TODO: change in 2.0, don't render emtpy.
95
115
  end
96
116
  end
97
117
  end
@@ -19,4 +19,4 @@ module Representable
19
19
  self.class.map
20
20
  end
21
21
  end
22
- end
22
+ end
@@ -1,16 +1,18 @@
1
- require "virtus"
1
+ gem 'dry-types', '>= 1.0.0'
2
+ require "dry-types"
2
3
 
3
4
  module Representable
4
5
  module Coercion
6
+ module Types
7
+ include Dry::Types()
8
+ end
5
9
  class Coercer
6
10
  def initialize(type)
7
11
  @type = type
8
12
  end
9
13
 
10
- # This gets called when the :render_filter or :parse_filter option is evaluated.
11
- # Usually the Coercer instance is an element in a Pipeline to allow >1 filters per property.
12
- def call(input, options)
13
- Virtus::Attribute.build(@type).coerce(input)
14
+ def call(input, _options)
15
+ @type.call(input)
14
16
  end
15
17
  end
16
18
 
@@ -34,4 +36,4 @@ module Representable
34
36
  end
35
37
  end
36
38
  end
37
- end
39
+ end
@@ -1,4 +1,8 @@
1
+ require 'declarative/definitions'
2
+
1
3
  module Representable
4
+ autoload :Option, 'representable/option'
5
+
2
6
  # Stores Definitions from ::property. It preserves the adding order (1.9+).
3
7
  # Same-named properties get overridden, just like in a Hash.
4
8
  #
@@ -20,16 +24,17 @@ module Representable
20
24
 
21
25
  def wrap=(value)
22
26
  value = value.to_s if value.is_a?(Symbol)
23
- @wrap = ::Declarative::Option(value, instance_exec: true, callable: Uber::Callable)
27
+ @wrap = ::Representable::Option(value)
24
28
  end
25
29
 
26
30
  # Computes the wrap string or returns false.
27
- def wrap_for(represented, *args, &block)
31
+ def wrap_for(represented, options = {}, &block)
28
32
  return unless @wrap
29
33
 
30
- value = @wrap.(represented, *args)
34
+ value = @wrap.(exec_context: represented, keyword_arguments: options.to_hash)
31
35
 
32
36
  return value if value != true
37
+
33
38
  infer_name_for(represented.class.name)
34
39
  end
35
40
 
@@ -1,16 +1,24 @@
1
+ require 'logger'
2
+
1
3
  module Representable
2
4
  module Debug
5
+ module_function def _representable_logger
6
+ @logger ||= Logger.new(STDOUT)
7
+ end
8
+
9
+ module_function def representable_log(message)
10
+ _representable_logger.debug { message }
11
+ end
12
+
3
13
  def update_properties_from(doc, options, format)
4
- puts
5
- puts "[Deserialize]........."
6
- puts "[Deserialize] document #{doc.inspect}"
14
+ representable_log "[Deserialize]........."
15
+ representable_log "[Deserialize] document #{doc.inspect}"
7
16
  super
8
17
  end
9
18
 
10
19
  def create_representation_with(doc, options, format)
11
- puts
12
- puts "[Serialize]........."
13
- puts "[Serialize]"
20
+ representable_log "[Serialize]........."
21
+ representable_log "[Serialize]"
14
22
  super
15
23
  end
16
24
 
@@ -22,13 +30,13 @@ module Representable
22
30
 
23
31
  module Binding
24
32
  def evaluate_option(name, *args, &block)
25
- puts "=====#{self[name]}" if name ==:prepare
26
- puts (evaled = self[name]) ?
33
+ Debug.representable_log "=====#{self[name]}" if name ==:prepare
34
+ Debug.representable_log (evaled = self[name]) ?
27
35
  " #evaluate_option [#{name}]: eval!!!" :
28
36
  " #evaluate_option [#{name}]: skipping"
29
37
  value = super
30
- puts " #evaluate_option [#{name}]: --> #{value}" if evaled
31
- puts " #evaluate_option [#{name}]: -->= #{args.first}" if name == :setter
38
+ Debug.representable_log " #evaluate_option [#{name}]: --> #{value}" if evaled
39
+ Debug.representable_log " #evaluate_option [#{name}]: -->= #{args.first}" if name == :setter
32
40
  value
33
41
  end
34
42
 
@@ -45,17 +53,17 @@ module Representable
45
53
 
46
54
  module Pipeline::Debug
47
55
  def call(input, options)
48
- puts "Pipeline#call: #{inspect}"
49
- puts " input: #{input.inspect}"
56
+ Debug.representable_log "Pipeline#call: #{inspect}"
57
+ Debug.representable_log " input: #{input.inspect}"
50
58
  super
51
59
  end
52
60
 
53
61
  def evaluate(block, memo, options)
54
62
  block.extend(Pipeline::Debug) if block.is_a?(Collect)
55
63
 
56
- puts " Pipeline : -> #{_inspect_function(block)} "
64
+ Debug.representable_log " Pipeline : -> #{_inspect_function(block)} "
57
65
  super.tap do |res|
58
- puts " Pipeline : result: #{res.inspect}"
66
+ Debug.representable_log " Pipeline : result: #{res.inspect}"
59
67
  end
60
68
  end
61
69
 
@@ -70,8 +78,8 @@ module Representable
70
78
  def _inspect_function(func)
71
79
  return func.extend(Pipeline::Debug).inspect if func.is_a?(Collect)
72
80
  return func unless func.is_a?(Proc)
81
+
73
82
  File.readlines(func.source_location[0])[func.source_location[1]-1].match(/^\s+(\w+)/)[1]
74
83
  end
75
84
  end
76
85
  end
77
-