attribution 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/attribution.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = "attribution"
5
- gem.version = "0.0.2"
5
+ gem.version = "0.1.0"
6
6
  gem.authors = ["Paul Barry"]
7
7
  gem.email = ["mail@paulbarry.com"]
8
8
  gem.description = %q{Add attributes to Ruby objects}
@@ -1,3 +1,3 @@
1
1
  module Attribution
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/attribution.rb CHANGED
@@ -20,6 +20,13 @@ module Attribution
20
20
  end
21
21
  end
22
22
 
23
+ def attributes
24
+ self.class.attributes.inject({}) do |attrs, attr|
25
+ attrs[attr] = send(attr)
26
+ attrs
27
+ end
28
+ end
29
+
23
30
  module ClassMethods
24
31
  def cast(obj)
25
32
  case obj
@@ -29,8 +36,13 @@ module Attribution
29
36
  end
30
37
  end
31
38
 
39
+ def attributes
40
+ @attributes ||= []
41
+ end
42
+
32
43
  # Attribute macros
33
44
  def string(attr)
45
+ attributes << attr.to_sym
34
46
  attr_reader(attr)
35
47
  define_method("#{attr}=") do |arg|
36
48
  instance_variable_set("@#{attr}", arg.to_s)
@@ -38,6 +50,7 @@ module Attribution
38
50
  end
39
51
 
40
52
  def boolean(attr)
53
+ attributes << attr.to_sym
41
54
  attr_reader(attr)
42
55
  define_method("#{attr}=") do |arg|
43
56
  v = case arg
@@ -51,6 +64,7 @@ module Attribution
51
64
  end
52
65
 
53
66
  def integer(attr)
67
+ attributes << attr.to_sym
54
68
  attr_reader(attr)
55
69
  define_method("#{attr}=") do |arg|
56
70
  instance_variable_set("@#{attr}", arg.to_i)
@@ -58,6 +72,7 @@ module Attribution
58
72
  end
59
73
 
60
74
  def float(attr)
75
+ attributes << attr.to_sym
61
76
  attr_reader(attr)
62
77
  define_method("#{attr}=") do |arg|
63
78
  instance_variable_set("@#{attr}", arg.to_f)
@@ -65,6 +80,7 @@ module Attribution
65
80
  end
66
81
 
67
82
  def decimal(attr)
83
+ attributes << attr.to_sym
68
84
  attr_reader(attr)
69
85
  define_method("#{attr}=") do |arg|
70
86
  instance_variable_set("@#{attr}", BigDecimal.new(arg.to_s))
@@ -72,6 +88,7 @@ module Attribution
72
88
  end
73
89
 
74
90
  def date(attr)
91
+ attributes << attr.to_sym
75
92
  attr_reader(attr)
76
93
  define_method("#{attr}=") do |arg|
77
94
  v = case arg
@@ -85,6 +102,8 @@ module Attribution
85
102
  end
86
103
 
87
104
  def time(attr)
105
+ attributes << attr.to_sym
106
+ attributes << attr.to_sym
88
107
  attr_reader(attr)
89
108
  define_method("#{attr}=") do |arg|
90
109
  v = case arg
@@ -98,6 +117,7 @@ module Attribution
98
117
  end
99
118
 
100
119
  def time_zone(attr)
120
+ attributes << attr.to_sym
101
121
  attr_reader(attr)
102
122
  define_method("#{attr}=") do |arg|
103
123
  instance_variable_set("@#{attr}", ActiveSupport::TimeZone[arg.to_s])
@@ -107,6 +127,7 @@ module Attribution
107
127
  # Association macros
108
128
  def belongs_to(association_name)
109
129
  # foo_id
130
+ attributes << "#{association_name}_id".to_sym
110
131
  define_method("#{association_name}_id") do
111
132
  ivar = "@#{association_name}_id"
112
133
  if instance_variable_defined?(ivar)
@@ -79,4 +79,10 @@ class AttributionTest < Test::Unit::TestCase
79
79
  assert_equal 3, book.chapters.size
80
80
  assert_equal book, book.chapters.first.book
81
81
  end
82
+
83
+ def test_attributes
84
+ chapter = Chapter.new(:number => "1")
85
+ assert_equal [:number, :title, :page_number, :book_id], Chapter.attributes
86
+ assert_equal({ :number => 1, :title => nil, :page_number => nil, :book_id => nil }, chapter.attributes)
87
+ end
82
88
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attribution
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
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-02-22 00:00:00.000000000 Z
12
+ date: 2013-02-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport