attribute_extensions 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/attribute_extensions.rb +18 -10
- data/lib/attribute_extensions/version.rb +1 -1
- data/spec/attribute_extensions_spec.rb +13 -1
- metadata +2 -2
data/lib/attribute_extensions.rb
CHANGED
@@ -12,22 +12,30 @@ module AttributeExtensions
|
|
12
12
|
module ClassMethods
|
13
13
|
include Typecasting
|
14
14
|
|
15
|
-
def attribute_reader(
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
def attribute_reader(*names)
|
16
|
+
options = names.extract_options!
|
17
|
+
|
18
|
+
names.each do |name|
|
19
|
+
define_method(name.to_sym) do
|
20
|
+
instance_variable_set("@#{name}", options[:default]) unless instance_variable_defined?("@#{name}")
|
21
|
+
instance_variable_get("@#{name}")
|
22
|
+
end
|
19
23
|
end
|
20
24
|
end
|
21
25
|
|
22
|
-
def attribute_writer(
|
23
|
-
|
24
|
-
|
26
|
+
def attribute_writer(*names)
|
27
|
+
options = names.extract_options!
|
28
|
+
|
29
|
+
names.each do |name|
|
30
|
+
define_method("#{name}=") do |value|
|
31
|
+
instance_variable_set("@#{name}", self.class.typecast(value, options[:type]))
|
32
|
+
end
|
25
33
|
end
|
26
34
|
end
|
27
35
|
|
28
|
-
def attribute(
|
29
|
-
attribute_reader(
|
30
|
-
attribute_writer(
|
36
|
+
def attribute(*args)
|
37
|
+
attribute_reader(*args)
|
38
|
+
attribute_writer(*args)
|
31
39
|
end
|
32
40
|
end
|
33
41
|
end
|
@@ -9,7 +9,7 @@ describe AttributeExtensions do
|
|
9
9
|
include AttributeExtensions
|
10
10
|
|
11
11
|
attribute_reader :color
|
12
|
-
attribute_reader :make,
|
12
|
+
attribute_reader :make, default: nil
|
13
13
|
attribute_reader :gears, default: 5
|
14
14
|
attribute_reader :diesel, default: false
|
15
15
|
attribute_reader :petrol, default: true
|
@@ -21,6 +21,12 @@ describe AttributeExtensions do
|
|
21
21
|
its(:gears) { should eq 5 }
|
22
22
|
its(:diesel) { should be_false }
|
23
23
|
its(:petrol) { should be_true }
|
24
|
+
|
25
|
+
it 'accepts multiple attributes' do
|
26
|
+
model.new.should_not respond_to(:foo, :bar)
|
27
|
+
model.attribute_reader :foo, :bar
|
28
|
+
model.new.should respond_to(:foo, :bar)
|
29
|
+
end
|
24
30
|
end
|
25
31
|
|
26
32
|
describe 'attribute_writer' do
|
@@ -38,6 +44,12 @@ describe AttributeExtensions do
|
|
38
44
|
end
|
39
45
|
}
|
40
46
|
|
47
|
+
it 'accepts multiple attributes' do
|
48
|
+
model.new.should_not respond_to(:foo=, :bar=)
|
49
|
+
model.attribute_writer :foo, :bar
|
50
|
+
model.new.should respond_to(:foo=, :bar=)
|
51
|
+
end
|
52
|
+
|
41
53
|
it 'calls typecast with the value and type' do
|
42
54
|
subject.class.should_receive(:typecast).with(1, :integer)
|
43
55
|
subject.doors = 1
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribute_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|