AttrCoerced

Gem Version Build Status Dependency Status Code Climate Coverage Inline docs

Defines attr_coerced class helper to create coercible PORO attributes.

Usage

class Foo
  extend AttrCoerced
  attr_coerced :bar, Bar, default: :baz
end

class Bar < SimpleDelegator

  def initialize(value)
    super value.to_s
  end
  # ... methods extending the string
end

Ensure the constructor to accept instances of its own - this is necessary for the getter.

After this both the getter and setter will be redefined:

foo = Foo.new

# Coerced default value is used
foo.bar.class    # => Bar
foo.bar == "baz" # => true

# Assigned value is coerced
foo.bar = :bar
foo.bar.class    # => Bar
foo.bar == "bar" # => true

# Reset to coerced default value
foo.bar = nil
foo.bar.class    # => Bar
foo.bar == "baz" # => true

You can redefine getter and setter only with attr_coerced_reader and attr_coerced_writer.

class Foo
  extend AttrCoerced
  attr_coerced_writer :bar, Bar, default: :baz
end

foo = Foo.new

# By default value is not coerced
foo.bar # => nil

# The setter assigns coerced default value instead of nil
foo.bar = nil
foo.bar.class    # => Bar
foo.bar == "baz" # => true

Notice, that if a default values isn't set or is set to nil, no nil values are coerced, because nil stands for nothing, not something.

class Foo
  extend AttrCoerced
  att_coerced :bar, Bar
end

foo = Foo.new
# The getter doesn't coerce nil
foo.bar # => nil

# Neither the setter does
foo.bar = nil
foo.bar # => nil

Focus

The module does nothing except for the getter and setter definitions.

You're free to define initializers for coerced attributes by youself. For example, you can use the module along with Virtus, that exploits a different coersion mechanism, but provides many other useful features.

class MyModule
  extend AttrCoerced
  include Virtus.model

  attribute :name           # This adds :name to attributes, initializer etc.
  attr_coerced :name, Name  # This reloads getter and setter only

end

Installation

Add this line to your application's Gemfile:

# Gemfile
gem "attr_coerced"

Then execute:

bundle

Or add it manually:

gem install attr_coerced

Changelog

v1.0.0

  • All methods makes nil not coerced because it always stands for nothing.
  • #attr_coerced_writer makes attribute setter to assign default value instead of nil.

Compatibility

Tested under rubies compatible to API 2.0+:

  • MRI 2.0+
  • Rubinius-2 (mode 2.0+)
  • JRuby-9000 (mode 2.0+)

Uses RSpec 3.0+ for testing and hexx-suit for dev/test tools collection.

Contributing

  • Fork the project.
  • Read the STYLEGUIDE.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with Rakefile or version (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

License

See the MIT LICENSE.