GithubHelp home page GithubHelp logo

lean-attributes's Introduction

Lean::Attributes

Lean::Attributes is inspired by gems like Virtus or FastAttributes. It allows one to define typed attributes on arbitrary Ruby classes. Lean::Attributes aims to be roughly as fast as FastAttributes but has a few syntactical differences in addition to support for default values.

Status

Gem Version Build Status Test Coverage Code Climate Depfu Inline docs

Installation

Add this line to your Gemfile:

gem 'lean-attributes', '~> 0.3'

And then execute:

$ bundle

Or install it yourself with:

$ gem install lean-attributes

Usage

require 'lean-attributes'

class Book
  include Lean::Attributes

  attribute :title,     String
  attribute :name,      String
  attribute :pages,     Integer
  attribute :authors,   Array
  attribute :published, Date
  attribute :sold,      Time,     default: ->{ Time.now }
  attribute :finished,  DateTime
  attribute :format,    Symbol,   default: :hardcover
end

book = Book.new(
  title:      'There and Back Again',
  name:       'The Hobbit',
  pages:      '200',
  authors:    'Tolkien',
  published:  '1937-09-21',
)
book.finished = '1937-08-20 12:35'

book.format # => :hardcover
book.sold # => 2015-09-15 23:06:34 UTC
book # =>
# #<Book:0x007fcb7613b610
#   @authors=["Tolkien"],
#   @finished=
#     #<DateTime: 1937-08-20T12:35:00+00:00 ((2428766j,45300s,0n),+0s,2299161j)>,
#   @format=:hardcover,
#   @name="The Hobbit",
#   @pages=200,
#   @published=#<Date: 1937-09-21 ((2428798j,0s,0n),+0s,2299161j)>,
#   @sold=2015-09-15 23:06:34 UTC,
#   @title="There and Back Again">

Coercion

The coercion capabilities included in this gem are poor by design and will only handle trivial use cases. Whenever you set an attribute, generated coercion methods are called. You can override these methods to create your own coercion rules.

class ReadingProgress
  include Lean::Attributes

  attribute :current_page, Integer, default: 1

  private

  def coerce_current_page(value)
    value = begin
      value.to_i
    rescue NoMethodError
      1
    end

    value = 1 if value < 1
    value
  end
end

# progress = ReadingProgress.new(current_page: 0)
# progress.current_page # => 1

Another way to contain such behavior is to create a CurrentPage class to represent your attribute instead of an Integer. The power is yours.

Benchmarks

Lean::Attributes is meant to be relatively lightweight and fast.

FastAttributes: without values                          :  3830507.7 i/s
Lean::Attributes: without values                        :  3670545.2 i/s - 1.04x slower
ActiveAttr: without values                              :  1818005.4 i/s - 2.11x slower
Lean::Attributes: integer values for integer attributes :   191991.7 i/s - 19.95x slower
Lean::Attributes: string values for integer attributes  :   167686.5 i/s - 22.84x slower
dry-types: integer values for integer attributes        :   160914.8 i/s - 23.80x slower
FastAttributes: integer values for integer attributes   :   160370.0 i/s - 23.89x slower
dry-types: string values for integer attributes         :   144054.6 i/s - 26.59x slower
FastAttributes: string values for integer attributes    :   136264.1 i/s - 28.11x slower
ActiveRecord: without values                            :    56582.9 i/s - 67.70x slower
Virtus: integer values for integer attributes           :    36116.4 i/s - 106.06x slower
ActiveAttr: integer values for integer attributes       :    27098.4 i/s - 141.36x slower
ActiveAttr: string values for integer attributes        :    26135.1 i/s - 146.57x slower
Attrio: integer values for integer attributes           :    21036.0 i/s - 182.09x slower
ActiveRecord: integer values for integer attributes     :    19806.9 i/s - 193.39x slower
ActiveRecord: string values for integer attributes      :    19789.4 i/s - 193.56x slower
Virtus: without values                                  :    19297.2 i/s - 198.50x slower
Attrio: string values for integer attributes            :    19294.7 i/s - 198.53x slower
Attrio: without values                                  :    14396.8 i/s - 266.07x slower
Virtus: string values for integer attributes            :     6548.1 i/s - 584.98x slower

Versioning

Lean:Attributes uses Semantic Versioning 2.0.0

Contributing

  1. Fork it ( https://github.com/elliottmason/lean-attributes/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Copyright

Copyright ยฉ 2015 R. Elliott Mason โ€“ Released under MIT License

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.