Sha256: fe344b69072c848bba28d94fd1b57fc05a3ad97f74fc0e51469f2e3fa1724f4c
Contents?: true
Size: 1.34 KB
Versions: 1
Compression:
Stored size: 1.34 KB
Contents
class Class def coerced_attr_writer(*attributes) setter_methods = _coerced_attr_writer_setters attributes.each do |attribute| java_setter_name = _coerced_setter_name(attribute) coercion_type = "#{setter_methods[java_setter_name]}" class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{attribute}=(val) unless val.is_a?(#{coercion_type}) val = val.to_java(#{coercion_type}) end set_#{attribute}(val) end RUBY end end alias_method :attr_writer_with_coercion, :coerced_attr_writer alias_method :coercible_attr_writer, :coerced_attr_writer alias_method :coercable_attr_writer, :coerced_attr_writer alias_method :attr_writer_coerced, :coerced_attr_writer alias_method :coerced_setters, :coerced_attr_writer alias_method :coerce_setters, :coerced_attr_writer private def _coerced_attr_writer_setters @_coerced_attr_writer_setters ||= java_class.java_instance_methods.inject({}) do |method_hash, method| if method.arity == 1 && method.name =~ /\Aset.*\Z/ method_hash[method.name] = method.parameter_types.first end method_hash end end def _coerced_setter_name(attribute) string = attribute.to_s.dup string = string.split(/[^a-z0-9]/i).map { |word| word.capitalize }.join return "set#{string}" end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
coerced_attr_writer-0.0.1-java | lib/coerced_attr_writer/core_ext/class.rb |