active_attr

What ActiveModel left out

Github星跟踪图

ActiveAttr

Build History
Code Climate

ActiveAttr is a set of modules that makes it easy to create plain old Ruby
models with functionality found in ORMs, like ActiveRecord, without
reinventing the wheel. Think of ActiveAttr as the stuff ActiveModel left out.

ActiveAttr is distributed as a Ruby gem on rubygems.org.

ActiveAttr Railscast

Modules

Attributes

Including the Attributes module into your class gives you a DSL for defining
the attributes of your model.

class Person
  include ActiveAttr::Attributes

  attribute :first_name
  attribute :last_name
end

person = Person.new
person.first_name = "Chris"
person.last_name = "Griego"
person.attributes #=> {"first_name"=>"Chris", "last_name"=>"Griego"}

AttributeDefaults

Including the AttributeDefaults module into your class builds on Attributes by
allowing defaults to be declared with attributes.

class Person
  include ActiveAttr::AttributeDefaults

  attribute :first_name, :default => "John"
  attribute :last_name, :default => "Doe"
end

person = Person.new
person.first_name #=> "John"
person.last_name #=> "Doe"

QueryAttributes

Including the QueryAttributes module into your class builds on Attributes by
providing instance methods for querying your attributes.

class Person
  include ActiveAttr::QueryAttributes

  attribute :first_name
  attribute :last_name
end

person = Person.new
person.first_name = "Chris"
person.first_name? #=> true
person.last_name? #=> false

TypecastedAttributes

Including the TypecastedAttributes module into your class builds on Attributes
by providing type conversion for your attributes.

class Person
  include ActiveAttr::TypecastedAttributes
  attribute :age, :type => Integer
end

person = Person.new
person.age = "29"
person.age #=> 29

BasicModel

Including the BasicModel module into your class gives you the bare minimum
required for your model to meet the ActiveModel API requirements.

class Person
  include ActiveAttr::BasicModel
end

Person.model_name.plural #=> "people"
person = Person.new
person.valid? #=> true
person.errors.full_messages #=> []

BlockInitialization

Including the BlockInitialization module into your class will yield the model
instance to a block passed to when creating a new instance.

class Person
  include ActiveAttr::BlockInitialization
  attr_accessor :first_name, :last_name
end

person = Person.new do, p, p.first_name = "Chris"
  p.last_name = "Griego"
end

person.first_name #=> "Chris"
person.last_name #=> "Griego"

Logger

Including the Logger module into your class will give you access to a
configurable logger in model classes and instances. Your preferred logger can
be configured on an instance, subclass, class, parent class, and globally by
setting ActiveAttr::Logger.logger. When using Rails, the Rails framework
logger will be configured by default.

class Person
  include ActiveAttr::Logger
end

Person.logger = Logger.new(STDOUT)
Person.logger? #=> true
Person.logger.info "Logging an informational message"

person = Person.new
person.logger? #=> true
person.logger = Logger.new(STDERR)
person.logger.warn "Logging a warning message"

MassAssignment

Including the MassAssignment module into your class gives you methods for bulk
initializing and updating the attributes of your model. Any unknown attributes
are silently ignored.

class Person
  include ActiveAttr::MassAssignment
  attr_accessor :first_name, :last_name, :age
end

person = Person.new(:first_name => "Christopher", :last_name => "Griego")
person.attributes = { :first_name => "Chris", :age => 21 }
person.first_name #=> "Chris"
person.last_name #=> "Griego"

MassAssignment supports mass assignment security/sanitization if a sanitizer
is included in the model. If using Rails 4.0, include ActiveModel's forbidden
attributes protection module to get support for strong parameters.

class Person
  include ActiveAttr::MassAssignment
  include ActiveModel::ForbiddenAttributesProtection
  attr_accessor :first_name, :last_name
end

person = Person.new(ActionController::Parameters.new({
  :first_name => "Chris",
  :last_name => "Griego",
}).permit(:first_name))
person.first_name #=> "Chris"
person.last_name #=> nil

If using Rails 3.x or the Protected Attributes gem,
include ActiveModel's mass assignment security module to get support for
protected attributes, including support for mass assignment roles.

class Person
  include ActiveAttr::MassAssignment
  include ActiveModel::MassAssignmentSecurity
  attr_accessor :first_name, :last_name
  attr_protected :last_name
end

person = Person.new(:first_name => "Chris", :last_name => "Griego")
person.first_name #=> "Chris"
person.last_name #=> nil

If using the Strong Parameters gem with Rails 3.2,
include the forbidden attributes protection module after including
the mass assignment security module.

class Person
  include ActiveAttr::MassAssignment
  include ActiveModel::MassAssignmentSecurity
  include ActiveModel::ForbiddenAttributesProtection
end

Serialization

The Serialization module is a shortcut for incorporating ActiveModel's
serialization functionality into your model with one include.

class Person
  include ActiveAttr::Serialization
end

Model

The Model module is a shortcut for incorporating the most common model
functionality into your model with one include. All of the above modules
are included when you include Model.

class Person
  include ActiveAttr::Model
end

Integrations

Ruby on Rails

When using ActiveAttr inside a Rails application, ActiveAttr will configure
your models' default logger to use the Rails logger automatically. Just
include ActiveAttr in your Gemfile.

gem "active_attr"

RSpec

ActiveAttr comes with matchers and RSpec integration to assist you in testing
your models. The matchers also work with compatible frameworks like Shoulda.

require "active_attr/rspec"

describe Person do
  it do
    should have_attribute(:first_name).
      of_type(String).
      with_default_value_of("John")
  end
end

主要指标

概览
名称与所有者cgriego/active_attr
主编程语言Ruby
编程语言Ruby (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2011-09-29 20:14:06
推送于2024-12-26 22:50:34
最后一次提交2024-12-26 14:50:34
发布数37
最新版本名称v0.17.1 (发布于 2024-11-12 15:56:06)
第一版名称v0.1.0 (发布于 2011-09-30 11:24:18)
用户参与
星数1.2k
关注者数18
派生数90
提交数375
已启用问题?
问题数134
打开的问题数42
拉请求数36
打开的拉请求数8
关闭的拉请求数31
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?