chozo 0.4.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.rbenv-version +1 -1
- data/lib/chozo.rb +3 -0
- data/lib/chozo/clean_room.rb +79 -0
- data/lib/chozo/clean_room_base.rb +36 -0
- data/lib/chozo/config/abstract.rb +1 -1
- data/lib/chozo/config/json.rb +1 -1
- data/lib/chozo/core_ext/hash/dotted_paths.rb +5 -5
- data/lib/chozo/mixin.rb +10 -0
- data/lib/chozo/mixin/from_file.rb +54 -0
- data/lib/chozo/varia_model.rb +27 -20
- data/lib/chozo/version.rb +1 -1
- data/spec/unit/chozo/clean_room_spec.rb +119 -0
- data/spec/unit/chozo/config/abstract_spec.rb +3 -7
- data/spec/unit/chozo/core_ext/hash/dotted_paths_spec.rb +7 -1
- data/spec/unit/chozo/mixin/from_file_spec.rb +76 -0
- data/spec/unit/chozo/varia_model_spec.rb +37 -6
- metadata +12 -4
data/.rbenv-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.9.3-
|
1
|
+
1.9.3-p327
|
data/lib/chozo.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
# @author Jamie Winsor <jamie@vialstudios.com>
|
2
2
|
module Chozo
|
3
|
+
autoload :CleanRoom, 'chozo/clean_room'
|
4
|
+
autoload :CleanRoomBase, 'chozo/clean_room_base'
|
3
5
|
autoload :Config, 'chozo/config'
|
4
6
|
autoload :Errors, 'chozo/errors'
|
7
|
+
autoload :Mixin, 'chozo/mixin'
|
5
8
|
autoload :Platform, 'chozo/platform'
|
6
9
|
autoload :RubyEngine, 'chozo/ruby_engine'
|
7
10
|
autoload :VariaModel, 'chozo/varia_model'
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Chozo
|
2
|
+
# @author Jamie Winsor <jamie@vialstudios.com>
|
3
|
+
module CleanRoom
|
4
|
+
module ClassMethods
|
5
|
+
@noisy_clean_room = false
|
6
|
+
|
7
|
+
# @return [CleanRoomBase]
|
8
|
+
def clean_room
|
9
|
+
@clean_room ||= CleanRoom.fabricate(self)
|
10
|
+
end
|
11
|
+
|
12
|
+
# @param [#to_s] filepath
|
13
|
+
#
|
14
|
+
# @return [Cookbook::Metadata]
|
15
|
+
def from_file(filepath)
|
16
|
+
filepath = filepath.to_s
|
17
|
+
|
18
|
+
if File.extname(filepath) =~ /\.json/
|
19
|
+
from_json_file(filepath)
|
20
|
+
else
|
21
|
+
from_ruby_file(filepath)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def from_json_file(filepath)
|
26
|
+
json_metadata = JSON.parse(File.read(filepath))
|
27
|
+
|
28
|
+
obj = new
|
29
|
+
obj.clean_eval do
|
30
|
+
json_metadata.each { |key, val| send(key.to_sym, val) }
|
31
|
+
end
|
32
|
+
obj
|
33
|
+
end
|
34
|
+
|
35
|
+
def from_ruby_file(filepath)
|
36
|
+
obj = new
|
37
|
+
obj.clean_eval(File.read(filepath), filepath, 1)
|
38
|
+
obj
|
39
|
+
end
|
40
|
+
|
41
|
+
def set_clean_room(klass)
|
42
|
+
@clean_room = klass
|
43
|
+
end
|
44
|
+
|
45
|
+
def noisy_clean_room(value = nil)
|
46
|
+
if value.nil?
|
47
|
+
@noisy_clean_room
|
48
|
+
else
|
49
|
+
@noisy_clean_room = value
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class << self
|
55
|
+
def included(base)
|
56
|
+
base.extend(ClassMethods)
|
57
|
+
base.send(:include, Chozo::VariaModel)
|
58
|
+
end
|
59
|
+
|
60
|
+
def fabricate(klass)
|
61
|
+
Class.new(CleanRoomBase) do
|
62
|
+
klass.attributes.each do |name, _|
|
63
|
+
dsl_attr_writer name.to_sym
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# @param [String, Proc] data
|
70
|
+
def clean_eval(*args, &block)
|
71
|
+
data = args.shift
|
72
|
+
unless data.nil?
|
73
|
+
block = data.is_a?(Proc) ? data : proc { eval(data, binding, *args) }
|
74
|
+
end
|
75
|
+
|
76
|
+
self.class.clean_room.new(self).instance_eval(&block)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Chozo
|
2
|
+
# @author Jamie Winsor <jamie@vialstudios.com>
|
3
|
+
class CleanRoomBase
|
4
|
+
class << self
|
5
|
+
# Create a DSL writer function that will assign the a given value
|
6
|
+
# to the real object of this clean room.
|
7
|
+
#
|
8
|
+
# @param [Symbol] attribute
|
9
|
+
def dsl_attr_writer(attribute)
|
10
|
+
class_eval do
|
11
|
+
define_method(attribute) do |value|
|
12
|
+
set_attribute(attribute, value)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(real_model)
|
19
|
+
@real_model = real_model
|
20
|
+
@noisy = real_model.class.noisy_clean_room
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
attr_reader :noisy
|
26
|
+
attr_reader :real_model
|
27
|
+
|
28
|
+
def set_attribute(name, value)
|
29
|
+
real_model.send("#{name}=", value)
|
30
|
+
end
|
31
|
+
|
32
|
+
def method_missing(*args)
|
33
|
+
noisy ? super : nil
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/chozo/config/json.rb
CHANGED
@@ -31,15 +31,15 @@ class Hash
|
|
31
31
|
# }
|
32
32
|
# }
|
33
33
|
#
|
34
|
-
# @param [String, Array] dotpath
|
35
|
-
# @param [Object] seed
|
36
|
-
# @param [Hash] target
|
34
|
+
# @param [String, Symbol, Array] dotpath
|
35
|
+
# @param [Object] seed (nil)
|
36
|
+
# @param [Hash] target (self.new)
|
37
37
|
#
|
38
38
|
# @return [Hash]
|
39
39
|
def from_dotted_path(dotpath, seed = nil, target = self.new)
|
40
40
|
case dotpath
|
41
|
-
when String
|
42
|
-
from_dotted_path(dotpath.split("."), seed)
|
41
|
+
when String, Symbol
|
42
|
+
from_dotted_path(dotpath.to_s.split("."), seed)
|
43
43
|
when Array
|
44
44
|
if dotpath.empty?
|
45
45
|
return target
|
data/lib/chozo/mixin.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
module Chozo::Mixin
|
2
|
+
# @author Jamie Winsor <jamie@vialstudios.com>
|
3
|
+
module FromFile
|
4
|
+
module ClassMethods
|
5
|
+
def from_file(filename, *args)
|
6
|
+
new(*args).from_file(filename)
|
7
|
+
end
|
8
|
+
|
9
|
+
def class_from_file(filename, *args)
|
10
|
+
new(*args).class_from_file(filename)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class << self
|
15
|
+
def included(base)
|
16
|
+
base.extend(ClassMethods)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Loads the contents of a file within the context of the current object
|
21
|
+
#
|
22
|
+
# @param [#to_s] filename
|
23
|
+
# path to the file to load
|
24
|
+
#
|
25
|
+
# @raise [IOError] if the file does not exist or cannot be read
|
26
|
+
def from_file(filename)
|
27
|
+
filename = filename.to_s
|
28
|
+
|
29
|
+
if File.exists?(filename) && File.readable?(filename)
|
30
|
+
self.instance_eval(IO.read(filename), filename, 1)
|
31
|
+
self
|
32
|
+
else
|
33
|
+
raise IOError, "Could not open or read: '#{filename}'"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Loads the contents of a file within the context of the current object's class
|
38
|
+
#
|
39
|
+
# @param [#to_s] filename
|
40
|
+
# path to the file to load
|
41
|
+
#
|
42
|
+
# @raise [IOError] if the file does not exist or cannot be read
|
43
|
+
def class_from_file(filename)
|
44
|
+
filename = filename.to_s
|
45
|
+
|
46
|
+
if File.exists?(filename) && File.readable?(filename)
|
47
|
+
self.class_eval(IO.read(filename), filename, 1)
|
48
|
+
self
|
49
|
+
else
|
50
|
+
raise IOError, "Could not open or read: '#{filename}'"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/lib/chozo/varia_model.rb
CHANGED
@@ -11,14 +11,14 @@ module Chozo
|
|
11
11
|
:carefree
|
12
12
|
]
|
13
13
|
|
14
|
-
# @return [
|
14
|
+
# @return [Hashie::Mash]
|
15
15
|
def attributes
|
16
16
|
@attributes ||= Hashie::Mash.new
|
17
17
|
end
|
18
18
|
|
19
|
-
# @return [
|
19
|
+
# @return [Hashie::Mash]
|
20
20
|
def validations
|
21
|
-
@validations ||=
|
21
|
+
@validations ||= Hashie::Mash.new
|
22
22
|
end
|
23
23
|
|
24
24
|
# @return [Symbol]
|
@@ -73,7 +73,7 @@ module Chozo
|
|
73
73
|
matches = false
|
74
74
|
|
75
75
|
types.each do |type|
|
76
|
-
if model.
|
76
|
+
if model.get_attribute(key).is_a?(type)
|
77
77
|
matches = true
|
78
78
|
break
|
79
79
|
end
|
@@ -94,7 +94,7 @@ module Chozo
|
|
94
94
|
#
|
95
95
|
# @return [Array]
|
96
96
|
def validate_required(model, key)
|
97
|
-
if model.
|
97
|
+
if model.get_attribute(key).nil?
|
98
98
|
[ :error, "A value is required for attribute: '#{key}'" ]
|
99
99
|
else
|
100
100
|
[ :ok, "" ]
|
@@ -138,7 +138,7 @@ module Chozo
|
|
138
138
|
|
139
139
|
class_eval do
|
140
140
|
define_method fun_name do
|
141
|
-
|
141
|
+
_attributes_[fun_name]
|
142
142
|
end
|
143
143
|
|
144
144
|
define_method "#{fun_name}=" do |value|
|
@@ -148,7 +148,7 @@ module Chozo
|
|
148
148
|
value
|
149
149
|
end
|
150
150
|
|
151
|
-
|
151
|
+
_attributes_[fun_name] = value
|
152
152
|
end
|
153
153
|
end
|
154
154
|
end
|
@@ -160,11 +160,6 @@ module Chozo
|
|
160
160
|
end
|
161
161
|
end
|
162
162
|
|
163
|
-
# @return [HashWithIndifferentAccess]
|
164
|
-
def attributes
|
165
|
-
@attributes ||= self.class.attributes.dup
|
166
|
-
end
|
167
|
-
|
168
163
|
# @return [HashWithIndifferentAccess]
|
169
164
|
def validate
|
170
165
|
self.class.validations.each do |attr_path, validations|
|
@@ -196,6 +191,16 @@ module Chozo
|
|
196
191
|
@errors ||= HashWithIndifferentAccess.new
|
197
192
|
end
|
198
193
|
|
194
|
+
# Assigns the attributes of a model from a given hash of attributes.
|
195
|
+
#
|
196
|
+
# If the assignment mode is set to `:whitelist`, then only the values of keys which have a
|
197
|
+
# corresponding attribute definition on the model will be set. All other keys will have their
|
198
|
+
# values ignored.
|
199
|
+
#
|
200
|
+
# If the assignment mode is set to `:carefree`, then the attributes hash will be populated
|
201
|
+
# with any key/values that are provided.
|
202
|
+
#
|
203
|
+
# @param [Hash] new_attrs
|
199
204
|
def mass_assign(new_attrs = {})
|
200
205
|
case self.class.assignment_mode
|
201
206
|
when :whitelist
|
@@ -204,20 +209,19 @@ module Chozo
|
|
204
209
|
carefree_assign(new_attrs)
|
205
210
|
end
|
206
211
|
end
|
207
|
-
alias_method :attributes=, :mass_assign
|
208
212
|
|
209
213
|
# @param [#to_s] key
|
210
214
|
#
|
211
215
|
# @return [Object]
|
212
216
|
def get_attribute(key)
|
213
|
-
|
217
|
+
_attributes_.dig(key.to_s)
|
214
218
|
end
|
215
219
|
alias_method :[], :get_attribute
|
216
220
|
|
217
221
|
# @param [#to_s] key
|
218
222
|
# @param [Object] value
|
219
223
|
def set_attribute(key, value)
|
220
|
-
|
224
|
+
_attributes_.deep_merge!(Hashie::Mash.from_dotted_path(key.to_s, value))
|
221
225
|
end
|
222
226
|
alias_method :[]=, :set_attribute
|
223
227
|
|
@@ -237,17 +241,20 @@ module Chozo
|
|
237
241
|
self
|
238
242
|
end
|
239
243
|
|
240
|
-
#
|
241
|
-
|
242
|
-
|
244
|
+
# The storage hash containing all of the key/values for this object's attributes
|
245
|
+
#
|
246
|
+
# @return [Hashie::Mash]
|
247
|
+
def _attributes_
|
248
|
+
@_attributes_ ||= self.class.attributes.dup
|
243
249
|
end
|
250
|
+
alias_method :to_hash, :_attributes_
|
244
251
|
|
245
252
|
# @option options [Boolean] :symbolize_keys
|
246
253
|
# @option options [Class, Symbol, String] :adapter
|
247
254
|
#
|
248
255
|
# @return [String]
|
249
256
|
def to_json(options = {})
|
250
|
-
MultiJson.encode(
|
257
|
+
MultiJson.encode(_attributes_, options)
|
251
258
|
end
|
252
259
|
alias_method :as_json, :to_json
|
253
260
|
|
@@ -263,7 +270,7 @@ module Chozo
|
|
263
270
|
private
|
264
271
|
|
265
272
|
def carefree_assign(new_attrs = {})
|
266
|
-
|
273
|
+
_attributes_.deep_merge!(new_attrs)
|
267
274
|
end
|
268
275
|
|
269
276
|
def whitelist_assign(new_attrs = {})
|
data/lib/chozo/version.rb
CHANGED
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Chozo::CleanRoom do
|
4
|
+
describe "ClassMethods" do
|
5
|
+
subject do
|
6
|
+
Class.new do
|
7
|
+
include Chozo::CleanRoom
|
8
|
+
|
9
|
+
attribute 'one'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "::clean_room" do
|
14
|
+
it "returns an anonymous class" do
|
15
|
+
subject.clean_room.class.should eql(Class)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "has a superlcass of CleanRoomBase" do
|
19
|
+
subject.clean_room.superclass.should eql(Chozo::CleanRoomBase)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "::from_ruby_file" do
|
24
|
+
let(:file) { tmp_path.join('rspec').to_s }
|
25
|
+
|
26
|
+
before(:each) do
|
27
|
+
File.open(file, 'w+') do |f|
|
28
|
+
f.write <<-TXT
|
29
|
+
one 'value'
|
30
|
+
TXT
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it "returns self" do
|
35
|
+
result = subject.from_ruby_file(file)
|
36
|
+
result.should be_a(subject)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "::from_json_file" do
|
41
|
+
pending
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
subject do
|
46
|
+
Class.new do
|
47
|
+
include Chozo::CleanRoom
|
48
|
+
|
49
|
+
attribute 'one', type: String
|
50
|
+
attribute 'two.three'
|
51
|
+
end.new
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "#clean_eval" do
|
55
|
+
it "evaluates a string" do
|
56
|
+
string = <<-RB
|
57
|
+
one 'value'
|
58
|
+
RB
|
59
|
+
|
60
|
+
subject.clean_eval(string)
|
61
|
+
subject.one.should eql("value")
|
62
|
+
end
|
63
|
+
|
64
|
+
it "evaluates a block" do
|
65
|
+
subject.clean_eval do
|
66
|
+
one 'value'
|
67
|
+
end
|
68
|
+
|
69
|
+
subject.one.should eql("value")
|
70
|
+
end
|
71
|
+
|
72
|
+
it "evaluates a proc" do
|
73
|
+
code = proc {
|
74
|
+
one "value"
|
75
|
+
}
|
76
|
+
|
77
|
+
subject.clean_eval(code)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "responds to a message for each class attribute" do
|
81
|
+
subject.class.attributes.each do |message, _|
|
82
|
+
subject.should respond_to(message.to_sym)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context "strict clean room: false" do
|
87
|
+
subject do
|
88
|
+
Class.new do
|
89
|
+
include Chozo::CleanRoom
|
90
|
+
noisy_clean_room(false)
|
91
|
+
end.new
|
92
|
+
end
|
93
|
+
|
94
|
+
it "doesn't raise an error if an unknown attribute is accessed" do
|
95
|
+
subject.clean_eval do
|
96
|
+
one 'hello'
|
97
|
+
three 'asdf'
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context "strict clean room: true" do
|
103
|
+
subject do
|
104
|
+
Class.new do
|
105
|
+
include Chozo::CleanRoom
|
106
|
+
noisy_clean_room(true)
|
107
|
+
end.new
|
108
|
+
end
|
109
|
+
|
110
|
+
it "raises NoMethodError if an unknown attribute is accessed" do
|
111
|
+
expect {
|
112
|
+
subject.clean_eval do
|
113
|
+
one 'hello'
|
114
|
+
end
|
115
|
+
}.to raise_error(NoMethodError)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -9,7 +9,7 @@ describe Chozo::Config::Abstract do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
it "contains all of the attributes" do
|
12
|
-
subject.
|
12
|
+
subject.set_attribute(:something, "value")
|
13
13
|
|
14
14
|
subject.to_hash.should have_key(:something)
|
15
15
|
subject.to_hash[:something].should eql("value")
|
@@ -18,12 +18,8 @@ describe Chozo::Config::Abstract do
|
|
18
18
|
|
19
19
|
describe "#slice" do
|
20
20
|
before(:each) do
|
21
|
-
subject.
|
22
|
-
|
23
|
-
}
|
24
|
-
subject.attributes[:two] = {
|
25
|
-
nested: "other"
|
26
|
-
}
|
21
|
+
subject.set_attribute(:one, nested: "value")
|
22
|
+
subject.set_attribute(:two, nested: "other")
|
27
23
|
@sliced = subject.slice(:one)
|
28
24
|
end
|
29
25
|
|
@@ -9,7 +9,7 @@ describe Hash do
|
|
9
9
|
subject.from_dotted_path("deep.nested.item").should be_a(Hash)
|
10
10
|
end
|
11
11
|
|
12
|
-
it "a hash containing the nested keys" do
|
12
|
+
it "returns a hash containing the nested keys" do
|
13
13
|
obj = subject.from_dotted_path("deep.nested.item")
|
14
14
|
|
15
15
|
obj.should have_key("deep")
|
@@ -23,6 +23,12 @@ describe Hash do
|
|
23
23
|
obj["deep"]["nested"]["item"].should be_nil
|
24
24
|
end
|
25
25
|
|
26
|
+
it "handles a symbol as the dotted path" do
|
27
|
+
obj = subject.from_dotted_path(:"a.b.c", "value")
|
28
|
+
|
29
|
+
obj["a"]["b"]["c"].should == "value"
|
30
|
+
end
|
31
|
+
|
26
32
|
context "when given a seed value" do
|
27
33
|
it "sets the value of the deepest nested item to the seed" do
|
28
34
|
obj = subject.from_dotted_path("deep.nested.item", "seeded_value")
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Chozo::Mixin::FromFile do
|
4
|
+
describe "ClassMethods" do
|
5
|
+
let(:file) { double('file') }
|
6
|
+
let(:args) { double('args') }
|
7
|
+
|
8
|
+
subject do
|
9
|
+
Class.new do
|
10
|
+
include Chozo::Mixin::FromFile
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "::from_file" do
|
15
|
+
it "initializes a new class and delegates to #from_file" do
|
16
|
+
instance = double('instance')
|
17
|
+
subject.should_receive(:new).with(args).and_return(instance)
|
18
|
+
instance.should_receive(:from_file).with(file)
|
19
|
+
|
20
|
+
subject.from_file(file, args)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "::class_from_file" do
|
25
|
+
it "initializes a new class and delegates to #class_from_file" do
|
26
|
+
instance = double('instance')
|
27
|
+
subject.should_receive(:new).with(args).and_return(instance)
|
28
|
+
instance.should_receive(:class_from_file).with(file)
|
29
|
+
|
30
|
+
subject.class_from_file(file, args)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
subject do
|
36
|
+
Class.new do
|
37
|
+
include Chozo::Mixin::FromFile
|
38
|
+
end.new
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "#from_file" do
|
42
|
+
let(:file) { tmp_path.join('rspec-test').to_s }
|
43
|
+
|
44
|
+
before(:each) do
|
45
|
+
File.open(file, 'w+') do |f|
|
46
|
+
f.write <<-CODE
|
47
|
+
def hello
|
48
|
+
1+1
|
49
|
+
end
|
50
|
+
CODE
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
it "evaluates the contents of the file in a new instance of the including class" do
|
55
|
+
subject.from_file(file).should respond_to(:hello)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "returns a new instance of the including class" do
|
59
|
+
subject.from_file(file).should be_a(subject.class)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "it raises IOError if the file cannot be read" do
|
63
|
+
expect {
|
64
|
+
subject.from_file("/doesnot/exist")
|
65
|
+
}.to raise_error(IOError)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "#class_from_file" do
|
70
|
+
it "it raises IOError if the file cannot be read" do
|
71
|
+
expect {
|
72
|
+
subject.class_from_file("/doesnot/exist")
|
73
|
+
}.to raise_error(IOError)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -43,11 +43,23 @@ describe Chozo::VariaModel do
|
|
43
43
|
|
44
44
|
subject.attributes.dig('brooke.winsor').should eql('rhode island')
|
45
45
|
end
|
46
|
+
|
47
|
+
it "allows an attribute called 'attributes'" do
|
48
|
+
subject.attribute 'attributes', default: 'bag of junk'
|
49
|
+
|
50
|
+
subject.attributes.dig('attributes').should eql('bag of junk')
|
51
|
+
end
|
52
|
+
|
53
|
+
it "allows an attribute called 'attribute'" do
|
54
|
+
subject.attribute 'attribute', default: 'some value'
|
55
|
+
|
56
|
+
subject.attributes.dig('attribute').should eql('some value')
|
57
|
+
end
|
46
58
|
end
|
47
59
|
|
48
60
|
describe "::validations" do
|
49
|
-
it "returns a
|
50
|
-
subject.validations.should be_a(
|
61
|
+
it "returns a Hashie::Mash" do
|
62
|
+
subject.validations.should be_a(Hashie::Mash)
|
51
63
|
end
|
52
64
|
|
53
65
|
it "is empty by default" do
|
@@ -380,6 +392,25 @@ describe Chozo::VariaModel do
|
|
380
392
|
subject.nested.two.should eql("2")
|
381
393
|
end
|
382
394
|
end
|
395
|
+
|
396
|
+
context "given an attribute called 'attributes'" do
|
397
|
+
subject do
|
398
|
+
Class.new do
|
399
|
+
include Chozo::VariaModel
|
400
|
+
|
401
|
+
attribute 'attributes', default: Hash.new
|
402
|
+
end.new
|
403
|
+
end
|
404
|
+
|
405
|
+
it "allows the setting and getting of the 'attributes' mimic methods" do
|
406
|
+
subject.attributes.should be_a(Hash)
|
407
|
+
subject.attributes.should be_empty
|
408
|
+
|
409
|
+
new_hash = { something: "here" }
|
410
|
+
subject.attributes = new_hash
|
411
|
+
subject.attributes[:something].should eql("here")
|
412
|
+
end
|
413
|
+
end
|
383
414
|
end
|
384
415
|
|
385
416
|
describe "Validations" do
|
@@ -524,7 +555,7 @@ describe Chozo::VariaModel do
|
|
524
555
|
}
|
525
556
|
|
526
557
|
subject.mass_assign(new_attrs)
|
527
|
-
subject.
|
558
|
+
subject.get_attribute(:undefined_attribute).should be_nil
|
528
559
|
subject.should_not respond_to(:undefined_attribute)
|
529
560
|
end
|
530
561
|
|
@@ -543,7 +574,7 @@ describe Chozo::VariaModel do
|
|
543
574
|
}
|
544
575
|
|
545
576
|
subject.mass_assign(new_attrs)
|
546
|
-
subject.
|
577
|
+
subject.get_attribute(:undefined_attribute).should eql("value")
|
547
578
|
end
|
548
579
|
end
|
549
580
|
end
|
@@ -611,8 +642,8 @@ describe Chozo::VariaModel do
|
|
611
642
|
end
|
612
643
|
|
613
644
|
describe "#to_hash" do
|
614
|
-
it "returns the
|
615
|
-
subject.to_hash.should eql(subject.
|
645
|
+
it "returns all of the varia dattributes" do
|
646
|
+
subject.to_hash.should eql(subject.send(:_attributes_))
|
616
647
|
end
|
617
648
|
end
|
618
649
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chozo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.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-
|
12
|
+
date: 2013-02-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -76,6 +76,8 @@ files:
|
|
76
76
|
- Thorfile
|
77
77
|
- chozo.gemspec
|
78
78
|
- lib/chozo.rb
|
79
|
+
- lib/chozo/clean_room.rb
|
80
|
+
- lib/chozo/clean_room_base.rb
|
79
81
|
- lib/chozo/config.rb
|
80
82
|
- lib/chozo/config/abstract.rb
|
81
83
|
- lib/chozo/config/json.rb
|
@@ -90,6 +92,8 @@ files:
|
|
90
92
|
- lib/chozo/hashie_ext.rb
|
91
93
|
- lib/chozo/hashie_ext/hash.rb
|
92
94
|
- lib/chozo/hashie_ext/mash.rb
|
95
|
+
- lib/chozo/mixin.rb
|
96
|
+
- lib/chozo/mixin/from_file.rb
|
93
97
|
- lib/chozo/platform.rb
|
94
98
|
- lib/chozo/ruby_engine.rb
|
95
99
|
- lib/chozo/varia_model.rb
|
@@ -98,9 +102,11 @@ files:
|
|
98
102
|
- spec/spec_helper.rb
|
99
103
|
- spec/support/helpers.rb
|
100
104
|
- spec/support/matchers/each.rb
|
105
|
+
- spec/unit/chozo/clean_room_spec.rb
|
101
106
|
- spec/unit/chozo/config/abstract_spec.rb
|
102
107
|
- spec/unit/chozo/config/json_spec.rb
|
103
108
|
- spec/unit/chozo/core_ext/hash/dotted_paths_spec.rb
|
109
|
+
- spec/unit/chozo/mixin/from_file_spec.rb
|
104
110
|
- spec/unit/chozo/platform_spec.rb
|
105
111
|
- spec/unit/chozo/ruby_engine_spec.rb
|
106
112
|
- spec/unit/chozo/varia_model_spec.rb
|
@@ -125,10 +131,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
131
|
version: '0'
|
126
132
|
segments:
|
127
133
|
- 0
|
128
|
-
hash:
|
134
|
+
hash: 2558961002303153299
|
129
135
|
requirements: []
|
130
136
|
rubyforge_project:
|
131
|
-
rubygems_version: 1.8.
|
137
|
+
rubygems_version: 1.8.24
|
132
138
|
signing_key:
|
133
139
|
specification_version: 3
|
134
140
|
summary: A collection of supporting libraries and Ruby core extensions
|
@@ -137,9 +143,11 @@ test_files:
|
|
137
143
|
- spec/spec_helper.rb
|
138
144
|
- spec/support/helpers.rb
|
139
145
|
- spec/support/matchers/each.rb
|
146
|
+
- spec/unit/chozo/clean_room_spec.rb
|
140
147
|
- spec/unit/chozo/config/abstract_spec.rb
|
141
148
|
- spec/unit/chozo/config/json_spec.rb
|
142
149
|
- spec/unit/chozo/core_ext/hash/dotted_paths_spec.rb
|
150
|
+
- spec/unit/chozo/mixin/from_file_spec.rb
|
143
151
|
- spec/unit/chozo/platform_spec.rb
|
144
152
|
- spec/unit/chozo/ruby_engine_spec.rb
|
145
153
|
- spec/unit/chozo/varia_model_spec.rb
|