setsumei 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ module Setsumei
4
4
  module Build
5
5
  def Build.a(klass,options = {})
6
6
  inform_developer "#{klass} should be able to list its attributes" unless klass.respond_to? :defined_attributes
7
- inform_developer "wrong number of arguments, options must include from: data }" unless options.keys.include? :from
7
+ inform_developer "wrong number of arguments, options must include { from: data }" unless options.keys.include? :from
8
8
 
9
9
  klass.new.tap do |object|
10
10
  klass.defined_attributes.each do |_,attribute|
@@ -3,12 +3,15 @@ module Setsumei
3
3
  class BooleanAttribute
4
4
 
5
5
  def BooleanAttribute.named(name, options = {})
6
+ options = options.dup
6
7
  new.tap do |attribute|
7
8
  attribute.name = name
9
+ options.delete(:as_a)
10
+ attribute.options = options
8
11
  end
9
12
  end
10
13
 
11
- attr_accessor :name
14
+ attr_accessor :name, :options
12
15
 
13
16
  def is_an_attribute_of_type?(type)
14
17
  type == :boolean || type == self.class
@@ -5,10 +5,11 @@ module Setsumei
5
5
  def FloatAttribute.named(name, options = {})
6
6
  new.tap do |attribute|
7
7
  attribute.name = name
8
+ attribute.options = options.dup.tap { |o| o.delete :as_a }
8
9
  end
9
10
  end
10
11
 
11
- attr_accessor :name
12
+ attr_accessor :name, :options
12
13
 
13
14
  def is_an_attribute_of_type?(type)
14
15
  type == :float || type == self.class
@@ -2,12 +2,15 @@ module Setsumei
2
2
  module Describable
3
3
  class IntAttribute
4
4
  def IntAttribute.named(name, options = {})
5
+ options = options.dup
5
6
  new.tap do |attribute|
6
7
  attribute.name = name
8
+ options.delete(:as_a)
9
+ attribute.options = options
7
10
  end
8
11
  end
9
12
 
10
- attr_accessor :name
13
+ attr_accessor :name, :options
11
14
 
12
15
  def is_an_attribute_of_type?(type)
13
16
  type == :int || type == self.class
@@ -3,14 +3,16 @@ module Setsumei
3
3
  class ObjectAttribute
4
4
 
5
5
  def ObjectAttribute.named(name,options = {})
6
+ options = options.dup
6
7
  raise ArgumentError.new("you must specify what the object is") unless options.has_key? :as_a
7
8
  new.tap do |attribute|
8
9
  attribute.name = name
9
- attribute.klass = options[:as_a]
10
+ attribute.klass = options.delete(:as_a)
11
+ attribute.options = options
10
12
  end
11
13
  end
12
14
 
13
- attr_accessor :name, :klass
15
+ attr_accessor :name, :klass, :options
14
16
 
15
17
  def initialize
16
18
  self.klass = Object
@@ -18,7 +20,12 @@ module Setsumei
18
20
 
19
21
  def value_for(data)
20
22
  return nil if data.nil? || data.empty?
21
- Build.a self.klass, from: data
23
+
24
+ begin
25
+ self.klass.create_from data
26
+ rescue NoMethodError
27
+ Build.a self.klass, from: data
28
+ end
22
29
  end
23
30
 
24
31
  def is_an_attribute_of_type?(type)
@@ -4,10 +4,11 @@ module Setsumei
4
4
  def StringAttribute.named(name,options = {})
5
5
  new.tap do |attribute|
6
6
  attribute.name = name
7
+ attribute.options = options.dup.tap { |o| o.delete :as_a }
7
8
  end
8
9
  end
9
10
 
10
- attr_accessor :name
11
+ attr_accessor :name, :options
11
12
 
12
13
  def is_an_attribute_of_type?(type)
13
14
  type == :string || type == self.class
@@ -1,3 +1,3 @@
1
1
  module Setsumei
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -12,6 +12,8 @@ module Setsumei
12
12
 
13
13
  it { should be_a BooleanAttribute }
14
14
  its(:name) { should == name }
15
+
16
+ it_should_behave_like "it handles options properly"
15
17
  end
16
18
 
17
19
  describe "#value_for(pre_type_cast_value)" do
@@ -12,6 +12,8 @@ module Setsumei
12
12
 
13
13
  it { should be_a FloatAttribute }
14
14
  its(:name) { should == name }
15
+
16
+ it_should_behave_like "it handles options properly"
15
17
  end
16
18
 
17
19
  describe "#value_for(pre_type_cast_value)" do
@@ -12,6 +12,8 @@ module Setsumei
12
12
 
13
13
  it { should be_a IntAttribute }
14
14
  its(:name) { should == name }
15
+
16
+ it_should_behave_like "it handles options properly"
15
17
  end
16
18
 
17
19
  describe "#value_for(pre_type_cast_value)" do
@@ -20,6 +20,8 @@ module Setsumei
20
20
  subject { ObjectAttribute.named name }
21
21
  specify { expect { subject }.to raise_error ArgumentError }
22
22
  end
23
+
24
+ it_should_behave_like "it handles options properly"
23
25
  end
24
26
 
25
27
  describe "#value_for(pre_type_cast_value)" do
@@ -27,6 +29,10 @@ module Setsumei
27
29
  let(:pre_type_cast_value) { { hash: "with_values" } }
28
30
  let(:object_attribute) { ObjectAttribute.named :name, as_a: klass }
29
31
 
32
+ before do
33
+ klass.stub(:create_from).and_raise(NoMethodError)
34
+ end
35
+
30
36
  subject { object_attribute.value_for pre_type_cast_value }
31
37
 
32
38
  it "should use the Builder to produce a object with klass" do
@@ -44,6 +50,18 @@ module Setsumei
44
50
  it { should be_nil }
45
51
  end
46
52
  end
53
+ context "klass responds to create_from" do
54
+ let(:pre_fab_object) { mock "pre_fab_object" }
55
+
56
+ before do
57
+ klass.unstub(:create_from)
58
+ end
59
+
60
+ it "should build the klass itself" do
61
+ klass.should_receive(:create_from).with(pre_type_cast_value).and_return(pre_fab_object)
62
+ subject.should == pre_fab_object
63
+ end
64
+ end
47
65
  end
48
66
 
49
67
  describe "#is_an_attribute_of_type?" do
@@ -12,6 +12,8 @@ module Setsumei
12
12
 
13
13
  it { should be_a StringAttribute }
14
14
  its(:name) { should == name }
15
+
16
+ it_should_behave_like "it handles options properly"
15
17
  end
16
18
 
17
19
  describe "#value_for(pre_type_cast_value)" do
@@ -0,0 +1,7 @@
1
+ shared_examples_for "it handles options properly" do
2
+ its(:options) { should == {} }
3
+ context "when options are specified" do
4
+ subject { described_class.named name, as_a: described_class.to_s.downcase.gsub(/(.*::|attribute)/,'').to_sym, with_additional: :options }
5
+ its(:options) { should == { with_additional: :options } }
6
+ end
7
+ end
@@ -9,5 +9,11 @@ shared_examples "it creates an attribute of type" do |_type,options|
9
9
  subject.define :field, as_a: _type
10
10
  subject.defined_attributes[:field].should be_an_attribute_of_type(_type)
11
11
  end
12
+ context "where options are specified" do
13
+ it "should set the options upon the attribute" do
14
+ subject.define :field, as_a: _type, with: :options
15
+ subject.defined_attributes[:field].options.should == { with: :options }
16
+ end
17
+ end
12
18
  end
13
19
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: setsumei
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jon Rowe
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-19 00:00:00 +01:00
13
+ date: 2011-05-23 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -95,6 +95,7 @@ files:
95
95
  - spec/setsumei/describable_spec.rb
96
96
  - spec/spec_helper.rb
97
97
  - spec/support/be_an_attribute_of_type_matcher.rb
98
+ - spec/support/shared_examples/attribute_options.rb
98
99
  - spec/support/shared_examples/attribute_type_creation.rb
99
100
  has_rdoc: true
100
101
  homepage: https://github.com/JonRowe/setsumei
@@ -136,4 +137,5 @@ test_files:
136
137
  - spec/setsumei/describable_spec.rb
137
138
  - spec/spec_helper.rb
138
139
  - spec/support/be_an_attribute_of_type_matcher.rb
140
+ - spec/support/shared_examples/attribute_options.rb
139
141
  - spec/support/shared_examples/attribute_type_creation.rb