Skip to content

Commit

Permalink
Add missing docs
Browse files Browse the repository at this point in the history
  • Loading branch information
solnic committed Mar 9, 2013
1 parent d8ee43f commit 5c7cbcf
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 14 deletions.
10 changes: 10 additions & 0 deletions lib/virtus.rb
Expand Up @@ -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)
Expand Down
52 changes: 51 additions & 1 deletion lib/virtus/attribute.rb
Expand Up @@ -57,32 +57,62 @@ def self.build(name, type = Object, options = {})
klass.new(name, accessor)
end

# Build coercer wrapper
#
# @example
#
# Virtus::Attribute.coercer # => #<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<Symbol>]
#
Expand All @@ -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<Symbol>]
#
# @api private
def self.writer_option_names
[ :coercer, :primitive, :default ]
Expand Down Expand Up @@ -161,11 +195,27 @@ def initialize(name, accessor)
@accessor = accessor
end

# Return reader object
#
# @example
#
# attribute.reader # => #<Virtus::Attribute::Reader ...>
#
# @return [Reader]
#
# @api public
def reader
accessor.reader
end

# Return writer object
#
# @example
#
# attribute.writer # => #<Virtus::Attribute::Writer ...>
#
# @return [Writer]
#
# @api public
def writer
accessor.writer
Expand Down
12 changes: 8 additions & 4 deletions lib/virtus/attribute/accessor.rb
Expand Up @@ -80,7 +80,7 @@ def initialize(reader, writer)
#
# @return [Object]
#
# @api public
# @api private
def get(instance)
reader.call(instance)
end
Expand All @@ -89,7 +89,7 @@ def get(instance)
#
# @return [Object]
#
# @api public
# @api private
def set(*args)
writer.call(*args)
end
Expand All @@ -98,7 +98,7 @@ def set(*args)
#
# @return [Boolean]
#
# @api public
# @api private
def public_reader?
reader.public?
end
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/virtus/attribute/accessor/lazy_accessor.rb
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions lib/virtus/attribute/accessor_method.rb
Expand Up @@ -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
Expand All @@ -55,7 +55,7 @@ def initialize(name, options = {})
#
# @return [TrueClass,FalseClass]
#
# @api public
# @api private
def public?
visibility == :public
end
Expand Down
8 changes: 8 additions & 0 deletions lib/virtus/attribute/collection.rb
Expand Up @@ -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<Symbol>]
#
# @api private
def self.writer_option_names
super << :member_type
Expand Down
19 changes: 17 additions & 2 deletions lib/virtus/attribute/embedded_value/open_struct_coercer.rb
Expand Up @@ -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)
Expand All @@ -20,7 +35,7 @@ def call(attributes)
end
end

end # class FromOpenStruct
end # class OpenStructCoercer
end # class EmbeddedValue
end # class Attribute
end # module Virtus
18 changes: 16 additions & 2 deletions lib/virtus/attribute/embedded_value/struct_coercer.rb
Expand Up @@ -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)
Expand All @@ -20,7 +34,7 @@ def call(attributes)
end
end

end # class FromStruct
end # class StructCoercer
end # class EmbeddedValue
end # class Attribute
end # module Virtus
10 changes: 9 additions & 1 deletion lib/virtus/attribute/hash.rb
Expand Up @@ -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
#
Expand All @@ -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<Symbol>]
#
# @api private
def self.writer_option_names
super << :key_type << :value_type
Expand Down

0 comments on commit 5c7cbcf

Please sign in to comment.