Sha256: 1ee24a7ccf00b5d49fc69d4e4d9358dd758e01fc65316575f175dbb8c0ad6c2a
Contents?: true
Size: 1.75 KB
Versions: 1
Compression:
Stored size: 1.75 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) camel_writer_name = _coerced_camel_writer_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 def #{camel_writer_name}=(val) #{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 = _coerced_camelize(attribute.to_s) return "set#{string}" end def _coerced_camel_writer_name(attribute) string = _coerced_camelize(attribute.to_s) string[0] = string[0].downcase return string end def _coerced_camelize(string) local_string = string.to_s.dup local_string = local_string.split(/[^a-z0-9]/i).map { |word| word.capitalize }.join return local_string end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
coerced_attr_writer-0.0.2-java | lib/coerced_attr_writer/core_ext/class.rb |