Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

works on ruby 1.9.2

  • Loading branch information...
commit 900f903e8198e1ff5e3443643321eced8174d8b0 1 parent c0ead77
@kristianmandrup authored
View
4 Gemfile
@@ -1,5 +1,9 @@
source 'https://rubygems.org'
+group :test do
+ gem 'rspec'
+end
+
gemspec
group :metrics do
View
12 README.md
@@ -37,20 +37,24 @@ You can create classes extended with virtus and define attributes:
class User
include Virtus
- attributes :name, :title, String
+ attributes :first_name, :last_name, String
+ attributes :allow_pets, :allow_smoking, :allow_drinking, Boolean, :as => :rule_options
+
attribute :age, Integer
attribute :birthday, DateTime
end
-user = User.new(:name => 'Piotr', :age => 28)
-user.attribute_set # => { :name => "Piotr", :age => 28 }
+user = User.new(:first_name => 'Piotr', :age => 28)
+user.attribute_set # => { :first_name => "Piotr", :age => 28 }
-user.name # => "Piotr"
+user.first_name # => "Piotr"
user.age = '28' # => 28
user.age.class # => Fixnum
user.birthday = 'November 18th, 1983' # => #<DateTime: 1983-11-18T00:00:00+00:00 (4891313/2,0/1,2299161)>
+
+User.rule_options # => [:allow_pets, :allow_smoking, :allow_drinking]
```
### Using Virtus with Modules
View
2  Rakefile
@@ -1,6 +1,6 @@
require 'rake'
-FileList['tasks/**/*.rake'].each { |task| import task }
+FileList['tasks/**/spec.rake'].each { |task| import task }
desc 'Default: run all specs'
task :default => :spec
View
9 lib/virtus/attribute.rb
@@ -60,10 +60,13 @@ class Attribute
#
# @api private
def self.build(*args)
+ # puts "build: #{args}"
options = args.extract_options!
type_arg = args.last
- type = case type_arg.class.to_s
- when 'Symbol' #, 'String'
+ type = if type_arg.kind_of?(Class)
+ args.pop
+ # if no type argument, default to Object
+ elsif type_arg.class.to_s == 'Symbol'
Object
else
args.pop
@@ -71,7 +74,7 @@ def self.build(*args)
attrs = args.map do |name|
build_one name, type, options
end
- attrs.size == 1 ? attrs.first : attrs
+ attrs.size == 1 ? attrs.first : attrs.flatten
end
def self.build_one(name, type = Object, options = {})
View
18 lib/virtus/extensions.rb
@@ -49,11 +49,25 @@ def self.extended(object)
#
# @api public
def attribute(*args)
- attributes = Attribute.build(*args)
- [attributes].flatten.each {|attribute| virtus_add_attribute(attribute) }
+ opts = args.dup.extract_options!
+
+ attributes = [Attribute.build(*args)].flatten
+ attributes.each {|attribute| virtus_add_attribute(attribute) }
+
+ clazz_meth = opts.delete(:as) if opts[:as]
+ create_attribute_group clazz_meth, attributes if clazz_meth
+
self
end
+ def create_attribute_group name, *attrs
+ singleton_class.class_eval do
+ define_method name do
+ attrs.flatten.map &:name
+ end
+ end
+ end
+
# The list of writer methods that can be mass-assigned to in #attributes=
#
# @return [Set]
View
21 spec/integration/defining_attributes_spec.rb
@@ -7,6 +7,8 @@ module Examples
class Person
include Virtus
+ attributes :allow_pets, :allow_smoking, Boolean, :as => :rule_options
+
attributes :name, :title, String
attribute :age, Integer
attribute :doctor, Boolean
@@ -41,7 +43,17 @@ class Manager < Person
end
context 'with attributes' do
- let(:attributes) { {:name => 'Jane', :title => 'Mr', :age => 45, :doctor => true, :salary => BigDecimal.new(4500) } }
+ let(:attributes) do
+ {
+ :allow_pets => true,
+ :allow_smoking => false,
+ :name => 'Jane',
+ :title => 'Mr',
+ :age => 45,
+ :doctor => true,
+ :salary => BigDecimal.new('4500.0')
+ }
+ end
subject { Examples::Person.new(attributes) }
specify "#attributes returns the object's attributes as a hash" do
@@ -53,6 +65,13 @@ class Manager < Person
end
end
+ context 'attribute group :rule_options defined' do
+ subject { Examples::Person }
+ specify ":as otion creates class method that returns attributes in rule group" do
+ subject.rule_options.should == [:allow_pets, :allow_smoking]
+ end
+ end
+
context 'inheritance' do
specify 'inherits all the attributes from the base class' do
fred = Examples::Manager.new(:name => 'Fred', :age => 29)

0 comments on commit 900f903

Please sign in to comment.
Something went wrong with that request. Please try again.