modelling 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/modelling.rb +33 -1
- data/lib/modelling/version.rb +1 -1
- data/spec/modelling_spec.rb +45 -1
- metadata +5 -5
data/lib/modelling.rb
CHANGED
@@ -32,6 +32,10 @@ module Modelling
|
|
32
32
|
def members
|
33
33
|
@members ||= {}
|
34
34
|
end
|
35
|
+
|
36
|
+
def accessors
|
37
|
+
@accessors ||= []
|
38
|
+
end
|
35
39
|
|
36
40
|
private
|
37
41
|
|
@@ -55,6 +59,7 @@ module Modelling
|
|
55
59
|
end
|
56
60
|
|
57
61
|
def create_accessor(name)
|
62
|
+
accessors << name
|
58
63
|
instance_eval { attr_accessor name }
|
59
64
|
end
|
60
65
|
|
@@ -74,5 +79,32 @@ module Modelling
|
|
74
79
|
end
|
75
80
|
args.each { |name, value| send "#{name}=", value }
|
76
81
|
end
|
77
|
-
|
82
|
+
|
83
|
+
def attributes
|
84
|
+
hash = {}
|
85
|
+
self.class.accessors.each do |method_name|
|
86
|
+
hash[method_name] = send(method_name)
|
87
|
+
end
|
88
|
+
hash
|
89
|
+
end
|
90
|
+
|
91
|
+
def inspect
|
92
|
+
attributes_as_nice_string = attributes.collect { |name, value|
|
93
|
+
"#{name}: #{attribute_for_inspect(value)}"
|
94
|
+
}.compact.join(", ")
|
95
|
+
"#<#{self.class} #{attributes_as_nice_string}>"
|
96
|
+
end
|
97
|
+
|
98
|
+
def attribute_for_inspect(value)
|
99
|
+
if value.is_a?(String) && value.length > 50
|
100
|
+
"#{value[0..50]}...".inspect
|
101
|
+
elsif value.is_a?(Date) || value.is_a?(Time)
|
102
|
+
%("#{value.to_s(:db)}")
|
103
|
+
elsif value.class.included_modules.include?(Modelling)
|
104
|
+
"#<#{value.class.to_s}>"
|
105
|
+
else
|
106
|
+
value.inspect
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
78
110
|
end
|
data/lib/modelling/version.rb
CHANGED
data/spec/modelling_spec.rb
CHANGED
@@ -125,7 +125,51 @@ describe Modelling do
|
|
125
125
|
it 'doesnt fail when lambdas with no args are used' do
|
126
126
|
LambdaTest.new.lambda.should eq 'boo'
|
127
127
|
end
|
128
|
-
|
128
|
+
|
129
|
+
specify 'tracks list of accessors' do
|
130
|
+
User.accessors.should include :name, :age
|
131
|
+
end
|
132
|
+
|
133
|
+
specify 'provides a Hash of attributes and values' do
|
134
|
+
User.new.attributes.key?(:name).should be_true
|
135
|
+
User.new.attributes.key?(:age).should be_true
|
136
|
+
User.new(:name => "Joe").attributes[:name].should eq "Joe"
|
137
|
+
end
|
138
|
+
|
139
|
+
specify 'converts the attributes hash to a string for inspect' do
|
140
|
+
u = User.new(:name => "Joe")
|
141
|
+
u.inspect.should == "#<User name: \"Joe\", age: nil, test: 3, fav_colours: [], biggest_gripes: []>"
|
142
|
+
end
|
143
|
+
|
144
|
+
context 'circular references' do
|
145
|
+
|
146
|
+
before do
|
147
|
+
class FirstModel
|
148
|
+
include Modelling
|
149
|
+
attributes :test => lambda { |me| OtherModel.new(me) }
|
150
|
+
end
|
151
|
+
|
152
|
+
class OtherModel
|
153
|
+
include Modelling
|
154
|
+
attributes :owner
|
155
|
+
def initialize(owner)
|
156
|
+
@owner = owner
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
it 'should not raise an error when inspecting' do
|
162
|
+
expect { FirstModel.new.inspect }.should_not raise_error(SystemStackError)
|
163
|
+
end
|
164
|
+
|
165
|
+
it 'should show the class name instead of inspecting referenced models' do
|
166
|
+
FirstModel.new.inspect.should include("#<OtherModel>")
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
|
172
|
+
|
129
173
|
context 'inheritence' do
|
130
174
|
let(:car) { Car.new }
|
131
175
|
let(:super_car) { SuperCar.new }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: modelling
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -12,11 +12,11 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date: 2012-
|
15
|
+
date: 2012-04-23 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: rspec
|
19
|
-
requirement: &
|
19
|
+
requirement: &70222335776840 !ruby/object:Gem::Requirement
|
20
20
|
none: false
|
21
21
|
requirements:
|
22
22
|
- - ! '>='
|
@@ -24,7 +24,7 @@ dependencies:
|
|
24
24
|
version: '0'
|
25
25
|
type: :development
|
26
26
|
prerelease: false
|
27
|
-
version_requirements: *
|
27
|
+
version_requirements: *70222335776840
|
28
28
|
description: We were doing PORO dev on Rails before it was hip.
|
29
29
|
email:
|
30
30
|
- ryan@eden.cc
|
@@ -65,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
65
|
version: '0'
|
66
66
|
requirements: []
|
67
67
|
rubyforge_project: modelling
|
68
|
-
rubygems_version: 1.8.
|
68
|
+
rubygems_version: 1.8.11
|
69
69
|
signing_key:
|
70
70
|
specification_version: 3
|
71
71
|
summary: Wraps some common-ish plain-ruby object modelling junk.
|