百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

attrio

> 编程语言
开源

用于普通老式 Ruby 对象的属性。没有依赖性,只有简单性和清晰性。

187 stars0 点赞4 次浏览
访问官网GitHub

工具介绍

用于普通老式 Ruby 对象的属性。没有依赖性,只有简单性和清晰性。

Attrio

Attributes for plain Ruby objects. The goal is to provide an ability to define attributes for your models without reinventing the wheel all over again. Attrio doesn't have any third-party dependencies like Virtus or ActiveAttr and does not redefine any methods inside your class, unless you want it to.

Installation

Add this line to your application's Gemfile:

gem 'attrio'

And then execute:

$ bundle

Or install it yourself as:

$ gem install attrio

Usage

Include Attrio into your class and then use #define_attributes block to declare you attributes.

ruby
class User
  include Attrio

  define_attributes do
  	attr :name, String
    attr :age, Integer
    attr :birthday, DateTime
  end
end

Accessing attributes

By default Attrio defines #attributes accessor which contains Hash with attributes names as keys and instances of Attrio::Attribute as values. Each instance of Attrio::Attribute contains following information:

  • type
  • writer method name
  • writer method visibility
  • reader method name
  • reader method visibility
  • instance variable name
  • additional options
…

Attributes can be filtered.

ruby
user.attributes([:name, :age, :not_existing_attribute]).keys
# => [:name, :age]

Accessor name can be easily overridden by passing :as option to define_attributes block.

ruby
class User
  include Attrio

  define_attributes :as => 'api_attributes' do
  	attr :name, String
    attr :age, Integer
    attr :birthday, DateTime
  end

  define_attributes :as => 'settings' do
    attr :receives_notifications, Boolean, :default => true
  end

end
ruby
user = User.new
user.api_attributes # => {...}

Default values

Attrio supports all the ways to setup default values that Virtus has.

…

You can does your attribute still has default value or not.

ruby
p = Page.new
=> #
p.attributes[:editor_title].default?
=> true
p.editor_title = 'PUBLISHED'
=> "PUBLISHED"
p.attributes[:editor_title].default?
=> false

Embed Value

You can embed values in Attrio just like you do it in Virtus.

…

Methods visibility

Don't want your accessors to be public? Visibility can be overridden easily.

ruby
class User
  include Attrio

  define_attributes do
  	attr :name, String, :writer => :protected
    attr :secret_rating, Integer, :reader => :private
  end
end

Types

Any Ruby class can be passed as type to Attrio. If this class responds to typecast and typecasted? methods then they will be called, else new will be called.

ruby
class Klass
  include Attrio

  define_attributes do
  	attr :custom_attribute, CustomClass
  end
end

Built-in Types

Boolean

By default boolean typecasts 'yes', '1', 1, 'true' as TrueClass and all other values as FalseClass, but you easily modify this behaviour.

ruby
class Klass
  include Attrio

  define_attributes do
  	attr :boolean_attribute, Boolean

  	attr :custom_boolean_attribute, Boolean, :yes => ['ja', '1', 1]
  	# attr :custom_boolean_attribute, Boolean, :yes_values => ['ja', '1', 1]
  	# attr :custom_boolean_attribute, Boolean, :no => ['nein', '0', 0]
  	# attr :custom_boolean_attribute, Boolean, :no_values => ['nein', '0', 0]
  end
end

Date, Time and DateTime

These three class have similar behaviour and options. By passing :format option you can setup how strftime method will try to parse your string.

ruby
class Klass
  include Attrio

  define_attributes do
  	attr :date_attribute, Date
  	attr :time_attribute, Time
  	attr :date_time_attribute, DateTime

	attr :custom_date_time_attribute, DateTime, :format => '%m/%d/%y-%H:%M:%S-%z'
  end
end

Float

Attribute will be typecasted using to_f method.

ruby
class Klass
  include Attrio

  define_attributes do
  	attr :float_attribute, Float
  end
end

Integer

Attribute will be typecasted using to_i method.

Optional :base parameter can be passed, during the typecast attribute will be assumed to be in specified base and will always be translated to decimal base.

ruby
class Klass
  include Attrio

  define_attributes do
  	attr :integer_attribute, Integer
  	attr :custom_integer_attribute, Integer, :base => 2
  end
end

Symbol

Attribute will be typecasted using to_sym method.

If Optional :underscore parameter is passed, then attribute value will be downcased and underscored before calling to_sym.

ruby
class Klass
  include Attrio

  define_attributes do
  	attr :symbol_attribute, Symbol
  	attr :custom_symbol_attribute, Symbol, :underscore => true
  end
end

Array

Arrays are designed to automatically handle collections of objects (that also can be typecasted)

If value that should be typecasted responds to split, then this method is called with default (or overriden) attributes, else Array is wrapped on value. You can easily handle types and options of collection elements.

ruby
class Klass
  include Attrio

  define_attributes do
	attr :array_attribute, Array
    attr :custom_array_attribute, Array, :split => ', ', :element => { :type => Date, :options => { :format => '%m/%d/%y' } }
  end
end

Inspect

Attrio adds its own #inspect method when included to the class. This overridden method prints object attributes in easy to read manner. To disable this feature pass :inspect => false to define_arguments block.

ruby
class Klass
  include Attrio

  define_attributes :inspect => false do
  	attr :attribute, String
  end
end

Note on Patches / Pull Requests

  • Fork the project.
  • 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, version, or history. (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.

Credits

Webmaster is maintained by JetRockets.

Contributors:

  • Igor Alexandrov
  • Julia Egorova
  • Dmitry Radionov

License

It is free software, and may be redistributed under the terms specified in the LICENSE file.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言