Permalink
Browse files

Add DumpWrapper to avoid hashes as api.

* DumpWrapper decorator is configured while adding mappings
* Integrate shared behaviours from veritas
* Deduplicate Transformator::{Loader,Dumper} as far as possible
* Yeah there is a design error in Transformator#transformation(name)
* This will be addressed by defiving operations from AttributeSet
  • Loading branch information...
1 parent d3e6a48 commit 699274de5e8d30b87de1678bb18cc83e9946facb Markus Schirp committed Jun 8, 2012
Showing with 555 additions and 292 deletions.
  1. +4 −6 TODO
  2. +1 −1 config/flog.yml
  3. +5 −3 config/site.reek
  4. +1 −0 lib/mapper.rb
  5. +1 −1 lib/mapper/attribute.rb
  6. +19 −41 lib/mapper/attribute/object.rb
  7. +22 −1 lib/mapper/attribute_set.rb
  8. +6 −24 lib/mapper/class_methods.rb
  9. +69 −6 lib/mapper/transformer.rb
  10. +40 −0 lib/mapper/transformer/dump_wrapper.rb
  11. +10 −36 lib/mapper/transformer/dumper.rb
  12. +4 −37 lib/mapper/transformer/loader.rb
  13. +4 −4 spec/integration/spike_spec.rb
  14. +7 −0 spec/shared/command_method_behavior.rb
  15. +15 −0 spec/shared/each_method_behaviour.rb
  16. +17 −0 spec/shared/hash_method_behavior.rb
  17. +7 −0 spec/shared/idempotent_method_behavior.rb
  18. +9 −0 spec/shared/invertible_method_behaviour.rb
  19. +13 −4 spec/spec_helper.rb
  20. +3 −0 spec/unit/mapper/attribute/class_methods/const_name_spec.rb
  21. +4 −0 spec/unit/mapper/attribute/class_methods/determine_type_spec.rb
  22. +3 −1 spec/unit/mapper/attribute/class_methods/handles_spec.rb
  23. +4 −2 spec/unit/mapper/attribute/custom/define_dumper_spec.rb
  24. +4 −2 spec/unit/mapper/attribute/custom/define_loader_spec.rb
  25. +1 −1 spec/unit/mapper/attribute/embedded_collection/load_spec.rb
  26. +1 −1 spec/unit/mapper/attribute/embedded_document/load_spec.rb
  27. +41 −0 spec/unit/mapper/attribute/object/define_dump_reader_spec.rb
  28. +22 −7 spec/unit/mapper/attribute/object/define_dumper_spec.rb
  29. +6 −6 spec/unit/mapper/attribute/object/define_loader_spec.rb
  30. +0 −31 spec/unit/mapper/attribute/object/dumper_method_source_spec.rb
  31. +7 −0 spec/unit/mapper/attribute/object/key_ques_spec.rb
  32. +2 −2 spec/unit/mapper/attribute/object/load_spec.rb
  33. +0 −31 spec/unit/mapper/attribute/object/loader_method_source_spec.rb
  34. +4 −0 spec/unit/mapper/attribute_set/add_spec.rb
  35. +44 −0 spec/unit/mapper/attribute_set/dump_key_names_spec.rb
  36. +2 −0 spec/unit/mapper/attribute_set/empty_ques_spec.rb
  37. +2 −0 spec/unit/mapper/attribute_set/fetch_dump_name_spec.rb
  38. +2 −0 spec/unit/mapper/attribute_set/fetch_load_name_spec.rb
  39. +2 −0 spec/unit/mapper/class_methods/attributes_spec.rb
  40. +3 −0 spec/unit/mapper/class_methods/const_missing_spec.rb
  41. +1 −5 spec/unit/mapper/class_methods/dump_spec.rb
  42. +1 −1 spec/unit/mapper/class_methods/dumper_spec.rb
  43. +1 −1 spec/unit/mapper/class_methods/loader_spec.rb
  44. +57 −0 spec/unit/mapper/transformer/class_methods/define_reader_spec.rb
  45. +3 −0 spec/unit/mapper/transformer/class_methods/mapper_spec.rb
  46. +1 −1 spec/unit/mapper/transformer/class_methods/reader_method_source_spec.rb
  47. +26 −0 spec/unit/mapper/transformer/dump_wrapper/read_spec.rb
  48. +5 −10 spec/unit/mapper/transformer/dumper/dump_spec.rb
  49. +25 −0 spec/unit/mapper/transformer/dumper/key_spec.rb
  50. +0 −12 spec/unit/mapper/transformer/dumper/object_spec.rb
  51. +2 −0 spec/unit/mapper/transformer/loader/attributes_spec.rb
  52. +0 −12 spec/unit/mapper/transformer/loader/dump_spec.rb
  53. +2 −0 spec/unit/mapper/transformer/loader/object_spec.rb
  54. +6 −2 spec/unit/mapper/transformer/mapper_spec.rb
  55. +14 −0 spec/unit/mapper/transformer/source_spec.rb
View
@@ -1,6 +1,4 @@
-* Remove ::Mapper::Mapper namespacing uglyness
-* Assert database root documents have a key
-* Add specs for load_key and dump_key
-* Create a mapper level identity map to allow
- loading an object tree where an object appears twice.
-* Create Loader and Dumper classes, to allow mapper level identity map.
+* Prevent Transformer::Loader#{key,attributes,object} to be overriden by mapping definitions
+* Prevent Transformer::Dumper#{key,dump} to be overriden by mapping definitions
+* Expose Transformations generated from AttributeSet to Transformator
+* Finalize the state of DumpWrapper in inheritance tree
View
@@ -1,2 +1,2 @@
---
-threshold: 9.0
+threshold: 13.9
View
@@ -7,10 +7,12 @@ UncommunicativeParameterName:
- !ruby/regexp /[0-9]$/
- !ruby/regexp /[A-Z]/
LargeClass:
- max_methods: 13
+# AttributeSet will be broken than this can be lowered
+ max_methods: 15
enabled: true
exclude: []
- max_instance_variables: 3
+# AttributeSet will be broken than this can be lowered
+ max_instance_variables: 5
UncommunicativeMethodName:
accept: []
exclude: []
@@ -51,7 +53,7 @@ NestedIterators:
enabled: true
max_allowed_nesting: 1
LongMethod:
- max_statements: 4
+ max_statements: 5
exclude: []
enabled: true
Duplication:
View
@@ -46,6 +46,7 @@ def self.new_mapper
require 'mapper/transformer'
require 'mapper/transformer/loader'
require 'mapper/transformer/dumper'
+require 'mapper/transformer/dump_wrapper'
require 'mapper/attribute'
require 'mapper/attribute/object'
require 'mapper/attribute/embedded'
@@ -73,7 +73,7 @@ def self.handle?(class_or_name)
# @api private
#
def self.handles
- [self,const_name]
+ @handles ||= [self,const_name]
end
end
end
@@ -16,24 +16,6 @@ def initialize(load_name,options={})
@key = !!options.fetch(:key,false)
end
- # Define reader on transformer
- #
- # @param [Class] klass
- # the loader or dumper class to define the reader on
- #
- # @param [String] source
- # the source of the method to define
- #
- # @api private
- #
- # @return [self]
- #
- def define_reader(klass,source)
- klass.class_eval(source,__FILE__,__LINE__+1)
-
- self
- end
-
public
# Return wheather this attribute is a key
@@ -46,25 +28,6 @@ def key?
@key
end
- # Return loader method source
- #
- # @return [String]
- #
- # @api private
- #
- def loader_method_source
- Transformer.reader_method_source(@load_name)
- end
-
- # Return dumper method source
- #
- # @return [String]
- #
- # @api private
- def dumper_method_source
- Transformer.reader_method_source(@dump_name)
- end
-
# Return names of domain object attributes this attribute loads
#
# @return [Array<Symbol>]
@@ -94,7 +57,7 @@ def dump_names
# @api private
#
def define_loader(klass)
- define_reader(klass,loader_method_source)
+ klass.define_reader(@load_name)
self
end
@@ -108,7 +71,23 @@ def define_loader(klass)
# @api private
#
def define_dumper(klass)
- define_reader(klass,dumper_method_source)
+ klass.define_reader(@dump_name)
+
+ self
+ end
+
+ # Define reader method on dump wrapper class
+ #
+ # @param [Class] klass
+ #
+ # @return [self]
+ #
+ # @api private
+ #
+ def define_dump_reader(klass)
+ dump_names.each do |name|
+ klass.define_reader(name)
+ end
self
end
@@ -133,9 +112,8 @@ def dump(object)
#
# @api private
#
- # TODO: Introduce dump accessor object.
def load(dump)
- dump.fetch(@dump_name)
+ dump.send(@dump_name)
end
end
end
@@ -36,6 +36,16 @@ def dump_names
dump_map.keys
end
+ # Return all dump names of attributes that are keys
+ #
+ # @return [Array<Symbol>]
+ #
+ # @api private
+ #
+ def dump_key_names
+ @dump_key_names ||= key_attributes.map(&:dump_names).flatten
+ end
+
# Return all names of domain object attributes in set
#
# @return [Array<Symbol>]
@@ -88,6 +98,17 @@ def initialize()
@set = Set.new
end
+ # Return all attributes that hold part of key
+ #
+ # @return [Enumerable]
+ #
+ # @api private
+ #
+ def key_attributes
+ @set.select(&:key?)
+ end
+
+
# Return map of load names to attribute
#
# @return [Hash<Symbol,Attribute>]
@@ -167,7 +188,7 @@ def map_entries(method)
# @api private
#
def reset
- @dump_map = @load_map = nil
+ @dump_map = @load_map = @dump_key_names = nil
self
end
end
@@ -33,7 +33,7 @@ def load(dump)
#
# @api private
def loader(dump)
- loader_klass.new(dump)
+ self::Loader.new(dump)
end
# Transform domain object into dump
@@ -59,7 +59,7 @@ def dump(object)
# @api private
#
def dumper(object)
- dumper_klass.new(object)
+ self::Dumper.new(object)
end
# Return attributes mapper is handling
@@ -160,32 +160,13 @@ def const_missing(name)
#
def add_attribute(attribute)
attributes.add(attribute)
- attribute.define_loader(loader_klass)
- attribute.define_dumper(dumper_klass)
+ attribute.define_loader(self::Loader)
+ attribute.define_dumper(self::Dumper)
+ attribute.define_dump_reader(self::DumpWrapper)
self
end
- # Return mappers dumper class
- #
- # @return [Class<Dumper>]
- #
- # @api private
- #
- def dumper_klass
- const_get(:Dumper)
- end
-
- # Return mappers dumper class
- #
- # @return [Class<Loader>]
- #
- # @api private
- #
- def loader_klass
- const_get(:Loader)
- end
-
# Read mappers model or raise
#
# @return [Model]
@@ -205,6 +186,7 @@ def read_model
def setup
create(Transformer::Dumper,:Dumper)
create(Transformer::Loader,:Loader)
+ create(Transformer::DumpWrapper,:DumpWrapper)
self
end
@@ -1,6 +1,14 @@
module Mapper
# Base class for loader and dumper classes
class Transformer
+ # Return source of transformation
+ #
+ # @return [Object]
+ #
+ # @api private
+ #
+ attr_reader :source
+
# Access transformers class mapper
#
# @raise [RuntimeError]
@@ -25,14 +33,30 @@ def self.mapper
# @api private
#
def self.reader_method_source(name)
- # comment to keep vim syntax happy
+ # comment to keep vim ruby syntax happy
<<-RUBY
def #{name}
- memonized(:#{name})
+ read(:#{name})
end
RUBY
end
+ # Define reader on class
+ #
+ # @param [Symbol] name
+ # the name of the reader to define
+ #
+ # @return [self]
+ #
+ # @api private
+ #
+ def self.define_reader(name)
+ source = reader_method_source(name)
+ class_eval(source,__FILE__,__LINE__)
+
+ self
+ end
+
# Access transformers mapper
#
# @return [Mapper]
@@ -43,8 +67,21 @@ def mapper
self.class.mapper
end
+
private
+ # Initialize transformer with source
+ #
+ # @param [Object] source
+ #
+ # @return [undefined]
+ #
+ # @api private
+ #
+ def initialize(source,operation)
+ @source,@operation = source,operation
+ end
+
# Access mappers attribute set
#
# @return [Mapper::AttributeSet]
@@ -70,19 +107,45 @@ def map(names)
end
end
- # Access transformer value via memonization guard
+ # Return transformed value via memonization guard
#
- # @param [Symbol] name of value to access
+ # @param [Symbol] name of value to return
#
# @return [Object]
#
# @api private
#
- def memonized(name)
+ def read(name)
@memonized ||= {}
@memonized.fetch(name) do
- @memonized[name]=access(name)
+ @memonized[name]=read_nocache(name)
end
end
+
+ # Return transformed value
+ #
+ # @param [Symbol] name of value to return
+ #
+ # @return [Object]
+ #
+ # @api private
+ #
+ def read_nocache(name)
+ transformation(name).send(@operation,@source)
+ end
+
+ # Return transformation
+ #
+ # (Currently an Attribute)
+ #
+ # @return [Transformation]
+ #
+ # @api private
+ #
+ # TODO: Will be replaced by real transformation
+ #
+ def transformation(name)
+ attribute_set.send("fetch_#{@operation}_name",name)
+ end
end
end
Oops, something went wrong.

0 comments on commit 699274d

Please sign in to comment.