diff --git a/lib/virtus.rb b/lib/virtus.rb index b6103f1f..86de6eb7 100644 --- a/lib/virtus.rb +++ b/lib/virtus.rb @@ -40,6 +40,16 @@ def self.extended(object) end private_class_method :extended + # Setup coercer + # + # @example + # + # Virtus.coercer do |config| + # config.string.boolea_map = { true => '1', false => '0' } + # end + # + # @return [Coercible::Coercer] + # # @api public def self.coercer(&block) @coercer ||= Coercible::Coercer.new(&block) diff --git a/lib/virtus/attribute.rb b/lib/virtus/attribute.rb index 62b35039..82c6910e 100644 --- a/lib/virtus/attribute.rb +++ b/lib/virtus/attribute.rb @@ -57,32 +57,62 @@ def self.build(name, type = Object, options = {}) klass.new(name, accessor) end + # Build coercer wrapper + # + # @example + # + # Virtus::Attribute.coercer # => # + # + # @return [Coercer] + # # @api public def self.coercer(*) Coercer.new(Virtus.coercer, coercion_method) end + # Return default reader class + # + # @return [::Class] + # # @api private def self.reader_class(*) Reader end + # Return default writer class + # + # @param [::Class] attribute type + # @param [::Hash] attribute options + # + # @return [::Class] + # # @api private def self.writer_class(type, options) options[:coerce] ? coercible_writer_class(type, options) : Writer end + # Return default coercible writer class + # + # @param [::Class] attribute type + # @param [::Hash] attribute options + # + # @return [::Class] + # # @api private def self.coercible_writer_class(_type, _options) Writer::Coercible end + # Return default options for writer class + # + # @return [::Hash] + # # @api private def self.reader_options(*) {} end - # Return options accepted by Writer + # Return options accepted by writer class # # @return [Array] # @@ -91,6 +121,10 @@ def self.writer_options(attribute_options) ::Hash[writer_option_names.zip(attribute_options.values_at(*writer_option_names))] end + # Return acceptable option names for write class + # + # @return [Array] + # # @api private def self.writer_option_names [ :coercer, :primitive, :default ] @@ -161,11 +195,27 @@ def initialize(name, accessor) @accessor = accessor end + # Return reader object + # + # @example + # + # attribute.reader # => # + # + # @return [Reader] + # # @api public def reader accessor.reader end + # Return writer object + # + # @example + # + # attribute.writer # => # + # + # @return [Writer] + # # @api public def writer accessor.writer diff --git a/lib/virtus/attribute/accessor.rb b/lib/virtus/attribute/accessor.rb index 44673986..abf77fcd 100644 --- a/lib/virtus/attribute/accessor.rb +++ b/lib/virtus/attribute/accessor.rb @@ -80,7 +80,7 @@ def initialize(reader, writer) # # @return [Object] # - # @api public + # @api private def get(instance) reader.call(instance) end @@ -89,7 +89,7 @@ def get(instance) # # @return [Object] # - # @api public + # @api private def set(*args) writer.call(*args) end @@ -98,7 +98,7 @@ def set(*args) # # @return [Boolean] # - # @api public + # @api private def public_reader? reader.public? end @@ -107,11 +107,15 @@ def public_reader? # # @return [Boolean] # - # @api public + # @api private def public_writer? writer.public? end + # Return if this accessor is lazy + # + # @return [FalseClass] + # # @api private def lazy? false diff --git a/lib/virtus/attribute/accessor/lazy_accessor.rb b/lib/virtus/attribute/accessor/lazy_accessor.rb index a4387f91..431c155b 100644 --- a/lib/virtus/attribute/accessor/lazy_accessor.rb +++ b/lib/virtus/attribute/accessor/lazy_accessor.rb @@ -11,6 +11,8 @@ class LazyAccessor < self # # @see Accessor#get # + # @return [Object] + # # @api private def get(instance) if instance.instance_variable_defined?(reader.instance_variable_name) diff --git a/lib/virtus/attribute/accessor_method.rb b/lib/virtus/attribute/accessor_method.rb index 35ff42a3..a1d4a86a 100644 --- a/lib/virtus/attribute/accessor_method.rb +++ b/lib/virtus/attribute/accessor_method.rb @@ -19,21 +19,21 @@ class AccessorMethod # # @return [Symbol] # - # @api public + # @api private attr_reader :name # Return visibility # # @return [Symbol] # - # @api public + # @api private attr_reader :visibility # Return instance variable name # # @return [Symbol] # - # @api public + # @api private attr_reader :instance_variable_name # Initialize accessor method instance @@ -55,7 +55,7 @@ def initialize(name, options = {}) # # @return [TrueClass,FalseClass] # - # @api public + # @api private def public? visibility == :public end diff --git a/lib/virtus/attribute/collection.rb b/lib/virtus/attribute/collection.rb index 2aa9044d..8ae7fdfe 100644 --- a/lib/virtus/attribute/collection.rb +++ b/lib/virtus/attribute/collection.rb @@ -31,11 +31,19 @@ def self.merge_options(type, _options) merged_options end + # @see Virtus::Attribute.coercible_writer_class + # + # @return [::Class] + # # @api private def self.coercible_writer_class(_type, options) options[:member_type] ? CoercibleWriter : super end + # @see Virtus::Attribute.writer_option_names + # + # @return [Array] + # # @api private def self.writer_option_names super << :member_type diff --git a/lib/virtus/attribute/embedded_value/open_struct_coercer.rb b/lib/virtus/attribute/embedded_value/open_struct_coercer.rb index 55b1a69f..9ab8c60b 100644 --- a/lib/virtus/attribute/embedded_value/open_struct_coercer.rb +++ b/lib/virtus/attribute/embedded_value/open_struct_coercer.rb @@ -2,15 +2,30 @@ module Virtus class Attribute class EmbeddedValue < Object - # EmbeddedValue attribute handling OpenStruct primitive or Virtus object + # EmbeddedValue coercer handling OpenStruct primitive or Virtus object # class OpenStructCoercer + + # Return primitive class + # + # @return [::Class] + # + # @api private attr_reader :primitive + # Initialize coercer object + # + # @return [undefined] + # + # @api private def initialize(primitive) @primitive = primitive end + # Build object from attribute hash + # + # @return [::Object] + # # @api private def call(attributes) if attributes.kind_of?(primitive) @@ -20,7 +35,7 @@ def call(attributes) end end - end # class FromOpenStruct + end # class OpenStructCoercer end # class EmbeddedValue end # class Attribute end # module Virtus diff --git a/lib/virtus/attribute/embedded_value/struct_coercer.rb b/lib/virtus/attribute/embedded_value/struct_coercer.rb index fca7587c..9e7e4698 100644 --- a/lib/virtus/attribute/embedded_value/struct_coercer.rb +++ b/lib/virtus/attribute/embedded_value/struct_coercer.rb @@ -2,15 +2,29 @@ module Virtus class Attribute class EmbeddedValue < Object - # EmbeddedValue attribute handling Struct primitive + # EmbeddedValue coercer handling Struct primitive # class StructCoercer + # Return primitive class + # + # @return [::Class] + # + # @api private attr_reader :primitive + # Initialize coercer instance + # + # @return [undefined] + # + # @api private def initialize(primitive) @primitive = primitive end + # Build a struct object from attributes + # + # @return [Struct] + # # @api private def call(attributes) if attributes.kind_of?(primitive) @@ -20,7 +34,7 @@ def call(attributes) end end - end # class FromStruct + end # class StructCoercer end # class EmbeddedValue end # class Attribute end # module Virtus diff --git a/lib/virtus/attribute/hash.rb b/lib/virtus/attribute/hash.rb index 8dfc5b8b..82951369 100644 --- a/lib/virtus/attribute/hash.rb +++ b/lib/virtus/attribute/hash.rb @@ -17,7 +17,7 @@ class Hash < Object coercion_method :to_hash default primitive.new - # Handles hashes with [key_type => value_type] syntax. + # Handles hashes with [key_type => value_type] syntax # # @param [Class] type # @@ -41,11 +41,19 @@ def self.merge_options(type, _options) merged_options end + # @see Virtus::Attribute.coercible_writer_class + # + # @return [::Class] + # # @api private def self.coercible_writer_class(_type, options) options[:key_type] && options[:value_type] ? CoercibleWriter : super end + # @see Virtus::Attribute.writer_option_names + # + # @return [Array] + # # @api private def self.writer_option_names super << :key_type << :value_type