setsumei 0.0.1

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.
@@ -0,0 +1,92 @@
1
+ require 'spec_helper'
2
+
3
+ module Setsumei
4
+ module Describable
5
+ describe StringAttribute do
6
+ its(:name) { should == nil }
7
+
8
+ describe ".named(name)" do
9
+ let(:name) { :my_string_field }
10
+
11
+ subject { StringAttribute.named(name) }
12
+
13
+ it { should be_a StringAttribute }
14
+ its(:name) { should == name }
15
+ end
16
+
17
+ describe "#value_for(pre_type_cast_value)" do
18
+ let(:string_attribute) { StringAttribute.new }
19
+
20
+ subject { string_attribute.value_for pre_type_cast_value }
21
+
22
+ context "where the value is a string" do
23
+ let(:pre_type_cast_value) { "I'm already a string" }
24
+
25
+ it { should == pre_type_cast_value }
26
+ end
27
+
28
+ context "where the value isn't a string" do
29
+ let(:to_s_value) { "stringified value of thing thats a string" }
30
+ let(:pre_type_cast_value) { mock "something thats not a string", to_s: to_s_value }
31
+
32
+ it { should == to_s_value }
33
+ end
34
+ end
35
+
36
+ describe "#is_an_attribute_of_type?" do
37
+ let(:string_attribute) { StringAttribute.new }
38
+
39
+ subject { string_attribute.is_an_attribute_of_type? type }
40
+
41
+ context "where type is :string" do
42
+ let(:type) { :string }
43
+ it { should be_true }
44
+ end
45
+
46
+ context "where type is StringAttribute" do
47
+ let(:type) { StringAttribute }
48
+ it { should be_true }
49
+ end
50
+
51
+ context "where type is unknown" do
52
+ let(:type) { mock "an unknown type" }
53
+ it { should be_false }
54
+ end
55
+ end
56
+
57
+ describe "set_value_on object, from_value_in: hash" do
58
+ let(:hash) { Hash.new }
59
+ let(:key) { "key" }
60
+ let(:hash_keys) { mock "hash_keys" }
61
+ let(:value_in_hash) { mock "value_in_hash" }
62
+ let(:converted_value) { mock "converted_value" }
63
+
64
+ let(:object) { mock "object", :my_string_attribute= => nil }
65
+
66
+ let(:string_attribute) { StringAttribute.named :my_string_attribute }
67
+
68
+ before do
69
+ Build::Key.stub(:for).and_return(key)
70
+ hash[key] = value_in_hash
71
+ string_attribute.stub(:value_for).and_return(converted_value)
72
+ end
73
+
74
+ subject { string_attribute.set_value_on object, from_value_in: hash }
75
+
76
+ it "should detect the key it should use to retrieve the value from the hash" do
77
+ hash.should_receive(:keys).and_return(hash_keys)
78
+ Build::Key.should_receive(:for).with(:my_string_attribute, given: hash_keys ).and_return(key)
79
+ subject
80
+ end
81
+ it "should convert the value" do
82
+ string_attribute.should_receive(:value_for).with(value_in_hash).and_return(converted_value)
83
+ subject
84
+ end
85
+ it "should pass object a value to the attribute described by this class" do
86
+ object.should_receive(:my_string_attribute=).with(converted_value)
87
+ subject
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,132 @@
1
+ require 'spec_helper'
2
+
3
+ module Setsumei
4
+ describe Describable do
5
+ let(:klass) { Class.new }
6
+
7
+ before do
8
+ klass.class_eval do
9
+ include Setsumei::Describable
10
+ end
11
+ end
12
+
13
+ describe ".defined_attributes" do
14
+ it "should have a list of defined attributes" do
15
+ klass.should respond_to :defined_attributes
16
+ klass.defined_attributes.should be_a Hash
17
+ end
18
+ it "should have a definition method" do
19
+ klass.should respond_to :define
20
+ end
21
+ it "should return defined attributes as a duplicated hash" do
22
+ klass.define :field
23
+ klass.defined_attributes[:field].should_not be_nil
24
+ klass.defined_attributes.delete(:field)
25
+ klass.defined_attributes[:field].should_not be_nil
26
+ end
27
+ end
28
+
29
+ describe ".define" do
30
+ it "should allow defining of attributes" do
31
+ klass.define :field
32
+ klass.defined_attributes[:field].should_not be_nil
33
+ end
34
+ it "should create string attributes by default" do
35
+ klass.define :field
36
+ klass.defined_attributes[:field].should be_a Describable::StringAttribute
37
+ end
38
+ it "should have set the name on the attribute to be the field name" do
39
+ klass.define :field
40
+ klass.defined_attributes[:field].name.should == :field
41
+ end
42
+
43
+ context "where setting a specific type" do
44
+ subject { klass }
45
+
46
+ it_should_behave_like "it creates an attribute of type", :boolean, creating_a: Describable::BooleanAttribute
47
+ it_should_behave_like "it creates an attribute of type", :string, creating_a: Describable::StringAttribute
48
+ it_should_behave_like "it creates an attribute of type", :float, creating_a: Describable::FloatAttribute
49
+ it_should_behave_like "it creates an attribute of type", :int, creating_a: Describable::IntAttribute
50
+
51
+ it_should_behave_like "it creates an attribute of type", Class.new, creating_a: Describable::ObjectAttribute
52
+ end
53
+ end
54
+
55
+ describe "instances with defined attributes" do
56
+ before do
57
+ klass.class_eval do
58
+ define :my_string_attribute
59
+ end
60
+ end
61
+
62
+ it "should allow me set to and retrieve values for my attribute" do
63
+ klass.new.tap do |object|
64
+ object.my_string_attribute = "Hey Everybody!"
65
+ object.my_string_attribute.should == "Hey Everybody!"
66
+ end
67
+ end
68
+ end
69
+
70
+ describe ".collection_of(klass,options)" do
71
+ let(:another_klass) { Class.new }
72
+ let(:options) { mock "options" }
73
+ let(:element) { mock "element" }
74
+ let(:element_2) { mock "element_2" }
75
+ let(:instance) { klass.new }
76
+ let(:collection) { mock "collection" }
77
+
78
+ before do
79
+ Describable::Collection.stub(:of).and_return(collection)
80
+ end
81
+
82
+ subject { klass.collection_of another_klass, options }
83
+
84
+ context "a klass with collection_of invoked" do
85
+ context "should behave like an array" do
86
+ before do
87
+ subject
88
+ instance << element
89
+ end
90
+
91
+ it "should accept elements with shovel" do
92
+ true
93
+ end
94
+ it "should be testable with include" do
95
+ instance.should include element
96
+ end
97
+ it "should be accessible with []" do
98
+ instance[0].should == element
99
+ end
100
+ it "should settable with []=" do
101
+ instance[0] = element_2
102
+ instance[0].should == element_2
103
+ end
104
+ it "should be accessible with first" do
105
+ instance.first.should == element
106
+ end
107
+ it "should be iterable over" do
108
+ values = []
109
+ instance.each { |value| values << value }
110
+ values.should == [element]
111
+ end
112
+ it "should be injectable" do
113
+ instance.inject({}) { |b,c| b[b.size.to_s] = c; b }.should == { "0" => element }
114
+ end
115
+ it "should be mappable" do
116
+ instance << element_2
117
+ instance.map { |a| [a] }.should == [[element],[element_2]]
118
+ end
119
+ it "should be comparable to an array" do
120
+ instance.should == [element]
121
+ end
122
+ end
123
+ end
124
+
125
+ it "should setup a describable collection" do
126
+ Describable::Collection.should_receive(:of).with(another_klass,options).and_return(collection)
127
+ subject
128
+ klass.defined_attributes.values.should include collection
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,4 @@
1
+ require 'ruby-debug'
2
+ require 'setsumei'
3
+
4
+ Dir[File.join(File.dirname(__FILE__),"support/**/*.rb")].each { |f| require f }
@@ -0,0 +1,5 @@
1
+ RSpec::Matchers.define :be_an_attribute_of_type do |type|
2
+ match do |subject|
3
+ subject.is_an_attribute_of_type? type
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ shared_examples "it creates an attribute of type" do |_type,options|
2
+ _klass = options[:creating_a]
3
+ context "definition of #{_type} attributes" do
4
+ it "should allow defining #{_type} attributes" do
5
+ subject.define :field, as_a: _type
6
+ subject.defined_attributes[:field].should be_a _klass
7
+ end
8
+ it "should have defined an attribute testable as :#{_type}" do
9
+ subject.define :field, as_a: _type
10
+ subject.defined_attributes[:field].should be_an_attribute_of_type(_type)
11
+ end
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: setsumei
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Jon Rowe
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-19 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: json
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: autotest-standalone
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: ruby-debug19
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ type: :development
59
+ version_requirements: *id004
60
+ description: A tool for describing API's as ruby objects. Currently supports building these objects from JSON
61
+ email:
62
+ - hello@jonrowe.co.uk
63
+ executables: []
64
+
65
+ extensions: []
66
+
67
+ extra_rdoc_files: []
68
+
69
+ files:
70
+ - .gitignore
71
+ - .rspec
72
+ - Gemfile
73
+ - README.md
74
+ - Rakefile
75
+ - lib/setsumei.rb
76
+ - lib/setsumei/build.rb
77
+ - lib/setsumei/build/key.rb
78
+ - lib/setsumei/describable.rb
79
+ - lib/setsumei/describable/boolean_attribute.rb
80
+ - lib/setsumei/describable/collection.rb
81
+ - lib/setsumei/describable/float_attribute.rb
82
+ - lib/setsumei/describable/int_attribute.rb
83
+ - lib/setsumei/describable/object_attribute.rb
84
+ - lib/setsumei/describable/string_attribute.rb
85
+ - lib/setsumei/version.rb
86
+ - setsumei.gemspec
87
+ - spec/setsumei/build/key_spec.rb
88
+ - spec/setsumei/build_spec.rb
89
+ - spec/setsumei/describable/boolean_attribute_spec.rb
90
+ - spec/setsumei/describable/collection_spec.rb
91
+ - spec/setsumei/describable/float_attribute_spec.rb
92
+ - spec/setsumei/describable/int_attribute_spec.rb
93
+ - spec/setsumei/describable/object_attribute_spec.rb
94
+ - spec/setsumei/describable/string_attribute_spec.rb
95
+ - spec/setsumei/describable_spec.rb
96
+ - spec/spec_helper.rb
97
+ - spec/support/be_an_attribute_of_type_matcher.rb
98
+ - spec/support/shared_examples/attribute_type_creation.rb
99
+ has_rdoc: true
100
+ homepage: https://github.com/JonRowe/setsumei
101
+ licenses: []
102
+
103
+ post_install_message:
104
+ rdoc_options: []
105
+
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: "0"
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: "0"
120
+ requirements: []
121
+
122
+ rubyforge_project:
123
+ rubygems_version: 1.6.2
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: A tool for describing API's as ruby objects
127
+ test_files:
128
+ - spec/setsumei/build/key_spec.rb
129
+ - spec/setsumei/build_spec.rb
130
+ - spec/setsumei/describable/boolean_attribute_spec.rb
131
+ - spec/setsumei/describable/collection_spec.rb
132
+ - spec/setsumei/describable/float_attribute_spec.rb
133
+ - spec/setsumei/describable/int_attribute_spec.rb
134
+ - spec/setsumei/describable/object_attribute_spec.rb
135
+ - spec/setsumei/describable/string_attribute_spec.rb
136
+ - spec/setsumei/describable_spec.rb
137
+ - spec/spec_helper.rb
138
+ - spec/support/be_an_attribute_of_type_matcher.rb
139
+ - spec/support/shared_examples/attribute_type_creation.rb