lib/lazy_mapper.rb in lazy_mapper-0.1.0 vs lib/lazy_mapper.rb in lazy_mapper-0.1.1
- old
+ new
@@ -120,33 +120,39 @@
@attributes ||= {}
end
# = ::one
#
- # Defines an attribute
+ # Defines an attribute and creates a reader and a writer for it.
+ # The writer verifies the type of it's supplied value.
#
# == Arguments
#
- # +name+ - The name of the attribue
+ # +name+ - The name of the attribue
#
- # +type+ - The type of the attribute. If the wrapped value is already of
- # that type, the mapper is bypassed.
+ # +type+ - The type of the attribute. If the wrapped value is already of
+ # that type, the mapper is bypassed.
+ # If the type is allowed be one of several, use an Array to
+ # to specify which ones
#
- # +from:+ - Specifies the name of the wrapped value in the JSON object.
- # Defaults to camelCased version of +name+.
+ # +from:+ - Specifies the name of the wrapped value in the JSON object.
+ # Defaults to camelCased version of +name+.
#
- # +map:+ - Specifies a custom mapper to apply to the wrapped value. Must be
- # a Callable. If unspecified, it defaults to the default mapper for the
- # specified +type+ or simply the identity mapper if no default mapper exists.
+ # +map:+ - Specifies a custom mapper to apply to the wrapped value. Must be
+ # a Callable. If unspecified, it defaults to the default mapper for the
+ # specified +type+ or simply the identity mapper if no default mapper exists.
#
- # +default:+ - The default value to use, if the wrapped value is not present
+ # +default:+ - The default value to use, if the wrapped value is not present
# in the wrapped JSON object.
#
+ # +allow_nil:+ - If true, allows the mapped value to be nil. Defaults to true.
+ #
# == Example
#
# class Foo < LazyMapper
# one :boss, Person, from: "supervisor", map: ->(p) { Person.new(p) }
+ # one :weapon, [BladedWeapon, Firearm], default: Sixshooter.new
# # ...
# end
#
def self.one(name, type, from: map_name(name), allow_nil: true, **args)
@@ -257,19 +263,25 @@
def add_mapper_for(type, &block)
mappers[type] = block
end
def inspect
+ @__under_inspection__ ||= 0
+ return "<#{ self.class.name } ... >" if @__under_inspection__ > 0
+ @__under_inspection__ += 1
attributes = self.class.attributes
if self.class.superclass.respond_to? :attributes
attributes = self.class.superclass.attributes.merge attributes
end
present_attributes = attributes.keys.each_with_object({}) {|name, memo|
value = self.send name
memo[name] = value unless value.nil?
}
"<#{ self.class.name } #{ present_attributes.map {|k,v| k.to_s + ': ' + v.inspect }.join(', ') } >"
+ res = "<#{ self.class.name } #{ present_attributes.map {|k,v| k.to_s + ': ' + v.inspect }.join(', ') } >"
+ @__under_inspection__ -= 1
+ res
end
protected
def json
@@ -279,14 +291,14 @@
##
# Defines how to map an attribute name
# to the corresponding name in the unmapped
# JSON object.
#
- # Defaults to #camelize
+ # Defaults to CAMELIZE
#
def self.map_name(name)
- camelize(name)
+ CAMELIZE[name]
end
private
attr_writer :json
@@ -330,12 +342,7 @@
send WRITER[name], yield unless instance_variable_defined?(ivar)
instance_variable_get(ivar)
end
SNAKE_CASE_PATTERN = /(_[a-z])/
-
- # "foo_bar_baz" -> "fooBarBaz"
- # "foo_bar?" -> "fooBar"
- def self.camelize(name)
- name.to_s.gsub(SNAKE_CASE_PATTERN) { |x| x[1].upcase }.gsub('?', '')
- end
+ CAMELIZE = -> name { name.to_s.gsub(SNAKE_CASE_PATTERN) { |x| x[1].upcase }.gsub('?', '') }
end