virtus 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/.gitignore +4 -0
  2. data/.rvmrc +1 -0
  3. data/.travis.yml +6 -0
  4. data/Gemfile +7 -0
  5. data/LICENSE +20 -0
  6. data/README.markdown +83 -0
  7. data/Rakefile +27 -0
  8. data/VERSION +1 -0
  9. data/lib/virtus.rb +61 -0
  10. data/lib/virtus/attributes/array.rb +8 -0
  11. data/lib/virtus/attributes/attribute.rb +214 -0
  12. data/lib/virtus/attributes/boolean.rb +39 -0
  13. data/lib/virtus/attributes/date.rb +44 -0
  14. data/lib/virtus/attributes/date_time.rb +43 -0
  15. data/lib/virtus/attributes/decimal.rb +24 -0
  16. data/lib/virtus/attributes/float.rb +20 -0
  17. data/lib/virtus/attributes/hash.rb +8 -0
  18. data/lib/virtus/attributes/integer.rb +20 -0
  19. data/lib/virtus/attributes/numeric.rb +9 -0
  20. data/lib/virtus/attributes/object.rb +8 -0
  21. data/lib/virtus/attributes/string.rb +11 -0
  22. data/lib/virtus/attributes/time.rb +45 -0
  23. data/lib/virtus/attributes/typecast/numeric.rb +32 -0
  24. data/lib/virtus/attributes/typecast/time.rb +27 -0
  25. data/lib/virtus/class_methods.rb +60 -0
  26. data/lib/virtus/instance_methods.rb +80 -0
  27. data/lib/virtus/support/chainable.rb +15 -0
  28. data/spec/integration/virtus/class_methods/attribute_spec.rb +63 -0
  29. data/spec/integration/virtus/class_methods/attributes_spec.rb +24 -0
  30. data/spec/integration/virtus/class_methods/const_missing_spec.rb +44 -0
  31. data/spec/spec_helper.rb +20 -0
  32. data/spec/unit/shared/attribute.rb +157 -0
  33. data/spec/unit/virtus/attributes/array_spec.rb +9 -0
  34. data/spec/unit/virtus/attributes/attribute_spec.rb +13 -0
  35. data/spec/unit/virtus/attributes/boolean_spec.rb +97 -0
  36. data/spec/unit/virtus/attributes/date_spec.rb +52 -0
  37. data/spec/unit/virtus/attributes/date_time_spec.rb +65 -0
  38. data/spec/unit/virtus/attributes/decimal_spec.rb +98 -0
  39. data/spec/unit/virtus/attributes/float_spec.rb +98 -0
  40. data/spec/unit/virtus/attributes/hash_spec.rb +9 -0
  41. data/spec/unit/virtus/attributes/integer_spec.rb +98 -0
  42. data/spec/unit/virtus/attributes/numeric/class_methods/descendants_spec.rb +15 -0
  43. data/spec/unit/virtus/attributes/object/class_methods/descendants_spec.rb +16 -0
  44. data/spec/unit/virtus/attributes/string_spec.rb +20 -0
  45. data/spec/unit/virtus/attributes/time_spec.rb +71 -0
  46. data/spec/unit/virtus/class_methods/new_spec.rb +41 -0
  47. data/spec/unit/virtus/determine_type_spec.rb +20 -0
  48. data/spec/unit/virtus/instance_methods/attribute_get_spec.rb +22 -0
  49. data/spec/unit/virtus/instance_methods/attribute_set_spec.rb +30 -0
  50. data/spec/unit/virtus/instance_methods/attributes_spec.rb +37 -0
  51. data/virtus.gemspec +95 -0
  52. metadata +131 -0
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Virtus::Attributes::String do
4
+ it_should_behave_like 'Attribute' do
5
+ let(:attribute_name) { :email }
6
+ let(:attribute_value) { 'red john' }
7
+ let(:attribute_value_other) { :'red john' }
8
+ end
9
+
10
+ describe '#typecast' do
11
+ let(:model) { Class.new { include Virtus } }
12
+ let(:attribute) { model.attribute(:name, String) }
13
+ let(:value) { 1 }
14
+ let(:typecast_value) { '1' }
15
+
16
+ subject { attribute.typecast(value) }
17
+
18
+ it { should == typecast_value }
19
+ end
20
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe Virtus::Attributes::Time do
4
+ it_should_behave_like 'Attribute' do
5
+ let(:attribute_name) { :birthday }
6
+ let(:attribute_value) { Time.now }
7
+ let(:attribute_value_other) { Time.now.to_s }
8
+ end
9
+
10
+ describe '#typecast' do
11
+ let(:model) { Class.new { include Virtus } }
12
+ let(:attribute) { model.attribute(:birthday, Virtus::Attributes::Time) }
13
+
14
+ let(:year) { 1983 }
15
+ let(:month) { 11 }
16
+ let(:day) { 18 }
17
+ let(:hour) { 8 }
18
+ let(:min) { 16 }
19
+ let(:sec) { 32 }
20
+
21
+ subject { attribute.typecast(value) }
22
+
23
+ shared_examples_for "a correct time" do
24
+ it { should be_kind_of(Time) }
25
+ its(:year) { should == year }
26
+ its(:month) { should == month }
27
+ its(:day) { should == day }
28
+ its(:hour) { should == hour }
29
+ its(:min) { should == min }
30
+ its(:sec) { should == sec }
31
+ end
32
+
33
+ context 'with a date time' do
34
+ it_should_behave_like "a correct time" do
35
+ let(:value) { DateTime.new(year, month, day, hour, min, sec) }
36
+ end
37
+ end
38
+
39
+ context 'with a hash' do
40
+ it_should_behave_like "a correct time" do
41
+ let(:value) do
42
+ { :year => year, :month => month, :day => day,
43
+ :hour => hour, :min => min, :sec => sec }
44
+ end
45
+ end
46
+ end
47
+
48
+ context 'with a string' do
49
+ context "without hour, min and sec" do
50
+ let(:hour) { 0 }
51
+ let(:min) { 0 }
52
+ let(:sec) { 0 }
53
+
54
+ it_should_behave_like "a correct time" do
55
+ let(:value) { "November #{day}th, #{year}" }
56
+ end
57
+ end
58
+
59
+ context "with hour, min and sec" do
60
+ it_should_behave_like "a correct time" do
61
+ let(:value) { "November #{day}th, #{year}, #{hour}:#{min}:#{sec}" }
62
+ end
63
+ end
64
+ end
65
+
66
+ context 'with a non-date value' do
67
+ let(:value) { '2999' }
68
+ it { should == value }
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Virtus::Attributes, '.new' do
4
+ let(:model) do
5
+ Class.new do
6
+ include Virtus
7
+
8
+ attribute :name, String
9
+
10
+ attr_reader :email
11
+ attr_accessor :age
12
+
13
+ def self.new(attributes)
14
+ model = super
15
+ model.age = 28
16
+ model
17
+ end
18
+
19
+ def initialize(attributes = {})
20
+ super
21
+ @email = "john@domain.com"
22
+ end
23
+ end
24
+ end
25
+
26
+ let(:object) do
27
+ model.new(:name => "john")
28
+ end
29
+
30
+ it "sets the attributes" do
31
+ object.name.should == "john"
32
+ end
33
+
34
+ it "calls custom #initialize" do
35
+ object.email.should == "john@domain.com"
36
+ end
37
+
38
+ it "calls custom .new" do
39
+ object.age.should == 28
40
+ end
41
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Virtus, '.determine_type' do
4
+
5
+ (Virtus::Attributes::Object.descendants - [
6
+ Virtus::Attributes::Boolean,
7
+ Virtus::Attributes::Object,
8
+ Virtus::Attributes::Numeric ]).each do |attribute_class|
9
+
10
+ context "with #{attribute_primitive = attribute_class.primitive}" do
11
+ subject { described_class.determine_type(attribute_primitive) }
12
+
13
+ it "returns the corresponding attribute class" do
14
+ subject.should be(attribute_class)
15
+ end
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Virtus::InstanceMethods, '#attribute_get' do
4
+ let(:described_class) do
5
+ Class.new do
6
+ include Virtus
7
+ attribute :name, String
8
+ end
9
+ end
10
+
11
+ let(:object) do
12
+ described_class.new(:name => value)
13
+ end
14
+
15
+ let(:value) do
16
+ 'john'
17
+ end
18
+
19
+ it "returns the value of an attribute" do
20
+ object.attribute_get(:name).should == value
21
+ end
22
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Virtus::InstanceMethods, '#attribute_set' do
4
+ let(:described_class) do
5
+ Class.new do
6
+ include Virtus
7
+ attribute :name, String
8
+ end
9
+ end
10
+
11
+ let(:object) do
12
+ described_class.new
13
+ end
14
+
15
+ let(:value) do
16
+ 'john'
17
+ end
18
+
19
+ before do
20
+ object.attribute_set(:name, value)
21
+ end
22
+
23
+ it "returns the value" do
24
+ object.attribute_set(:name, value).should == value
25
+ end
26
+
27
+ it "sets value of an attribute" do
28
+ object.name.should == value
29
+ end
30
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Virtus do
4
+ let(:model) do
5
+ Class.new do
6
+ include Virtus
7
+
8
+ attribute :name, String
9
+ attribute :age, Integer
10
+ attribute :email, String, :accessor => :private
11
+ end
12
+ end
13
+
14
+ let(:object) do
15
+ model.new(attributes)
16
+ end
17
+
18
+ let(:attributes) do
19
+ { :name => 'john', :age => 28 }
20
+ end
21
+
22
+ describe '#attributes' do
23
+ it "returns a hash of attributes" do
24
+ object.attributes.should == attributes
25
+ end
26
+ end
27
+
28
+ describe "#attributes=" do
29
+ before do
30
+ object.attributes = attributes
31
+ end
32
+
33
+ it "sets attribute values for publicly accessible attributes" do
34
+ object.attributes.should == attributes
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,95 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{virtus}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = [%q{Piotr Solnica}]
12
+ s.date = %q{2011-06-04}
13
+ s.description = %q{Attributes for your plain ruby objects}
14
+ s.email = [%q{piotr@rubyverse.com}]
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ ".rvmrc",
22
+ ".travis.yml",
23
+ "Gemfile",
24
+ "LICENSE",
25
+ "README.markdown",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/virtus.rb",
29
+ "lib/virtus/attributes/array.rb",
30
+ "lib/virtus/attributes/attribute.rb",
31
+ "lib/virtus/attributes/boolean.rb",
32
+ "lib/virtus/attributes/date.rb",
33
+ "lib/virtus/attributes/date_time.rb",
34
+ "lib/virtus/attributes/decimal.rb",
35
+ "lib/virtus/attributes/float.rb",
36
+ "lib/virtus/attributes/hash.rb",
37
+ "lib/virtus/attributes/integer.rb",
38
+ "lib/virtus/attributes/numeric.rb",
39
+ "lib/virtus/attributes/object.rb",
40
+ "lib/virtus/attributes/string.rb",
41
+ "lib/virtus/attributes/time.rb",
42
+ "lib/virtus/attributes/typecast/numeric.rb",
43
+ "lib/virtus/attributes/typecast/time.rb",
44
+ "lib/virtus/class_methods.rb",
45
+ "lib/virtus/instance_methods.rb",
46
+ "lib/virtus/support/chainable.rb",
47
+ "spec/integration/virtus/class_methods/attribute_spec.rb",
48
+ "spec/integration/virtus/class_methods/attributes_spec.rb",
49
+ "spec/integration/virtus/class_methods/const_missing_spec.rb",
50
+ "spec/spec_helper.rb",
51
+ "spec/unit/shared/attribute.rb",
52
+ "spec/unit/virtus/attributes/array_spec.rb",
53
+ "spec/unit/virtus/attributes/attribute_spec.rb",
54
+ "spec/unit/virtus/attributes/boolean_spec.rb",
55
+ "spec/unit/virtus/attributes/date_spec.rb",
56
+ "spec/unit/virtus/attributes/date_time_spec.rb",
57
+ "spec/unit/virtus/attributes/decimal_spec.rb",
58
+ "spec/unit/virtus/attributes/float_spec.rb",
59
+ "spec/unit/virtus/attributes/hash_spec.rb",
60
+ "spec/unit/virtus/attributes/integer_spec.rb",
61
+ "spec/unit/virtus/attributes/numeric/class_methods/descendants_spec.rb",
62
+ "spec/unit/virtus/attributes/object/class_methods/descendants_spec.rb",
63
+ "spec/unit/virtus/attributes/string_spec.rb",
64
+ "spec/unit/virtus/attributes/time_spec.rb",
65
+ "spec/unit/virtus/class_methods/new_spec.rb",
66
+ "spec/unit/virtus/determine_type_spec.rb",
67
+ "spec/unit/virtus/instance_methods/attribute_get_spec.rb",
68
+ "spec/unit/virtus/instance_methods/attribute_set_spec.rb",
69
+ "spec/unit/virtus/instance_methods/attributes_spec.rb",
70
+ "virtus.gemspec"
71
+ ]
72
+ s.homepage = %q{https://github.com/solnic/virtus}
73
+ s.require_paths = [%q{lib}]
74
+ s.rubygems_version = %q{1.8.5}
75
+ s.summary = %q{Attributes for your plain ruby objects}
76
+
77
+ if s.respond_to? :specification_version then
78
+ s.specification_version = 3
79
+
80
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
81
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
82
+ s.add_development_dependency(%q<rspec>, ["~> 2.6.0"])
83
+ s.add_development_dependency(%q<simplecov>, ["~> 0.4.2"])
84
+ else
85
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
86
+ s.add_dependency(%q<rspec>, ["~> 2.6.0"])
87
+ s.add_dependency(%q<simplecov>, ["~> 0.4.2"])
88
+ end
89
+ else
90
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
91
+ s.add_dependency(%q<rspec>, ["~> 2.6.0"])
92
+ s.add_dependency(%q<simplecov>, ["~> 0.4.2"])
93
+ end
94
+ end
95
+
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: virtus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Piotr Solnica
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-06-04 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: jeweler
16
+ requirement: &76569310 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.5.2
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *76569310
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &76569070 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.6.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *76569070
36
+ - !ruby/object:Gem::Dependency
37
+ name: simplecov
38
+ requirement: &76568830 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.4.2
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *76568830
47
+ description: Attributes for your plain ruby objects
48
+ email:
49
+ - piotr@rubyverse.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files:
53
+ - LICENSE
54
+ - README.markdown
55
+ files:
56
+ - .gitignore
57
+ - .rvmrc
58
+ - .travis.yml
59
+ - Gemfile
60
+ - LICENSE
61
+ - README.markdown
62
+ - Rakefile
63
+ - VERSION
64
+ - lib/virtus.rb
65
+ - lib/virtus/attributes/array.rb
66
+ - lib/virtus/attributes/attribute.rb
67
+ - lib/virtus/attributes/boolean.rb
68
+ - lib/virtus/attributes/date.rb
69
+ - lib/virtus/attributes/date_time.rb
70
+ - lib/virtus/attributes/decimal.rb
71
+ - lib/virtus/attributes/float.rb
72
+ - lib/virtus/attributes/hash.rb
73
+ - lib/virtus/attributes/integer.rb
74
+ - lib/virtus/attributes/numeric.rb
75
+ - lib/virtus/attributes/object.rb
76
+ - lib/virtus/attributes/string.rb
77
+ - lib/virtus/attributes/time.rb
78
+ - lib/virtus/attributes/typecast/numeric.rb
79
+ - lib/virtus/attributes/typecast/time.rb
80
+ - lib/virtus/class_methods.rb
81
+ - lib/virtus/instance_methods.rb
82
+ - lib/virtus/support/chainable.rb
83
+ - spec/integration/virtus/class_methods/attribute_spec.rb
84
+ - spec/integration/virtus/class_methods/attributes_spec.rb
85
+ - spec/integration/virtus/class_methods/const_missing_spec.rb
86
+ - spec/spec_helper.rb
87
+ - spec/unit/shared/attribute.rb
88
+ - spec/unit/virtus/attributes/array_spec.rb
89
+ - spec/unit/virtus/attributes/attribute_spec.rb
90
+ - spec/unit/virtus/attributes/boolean_spec.rb
91
+ - spec/unit/virtus/attributes/date_spec.rb
92
+ - spec/unit/virtus/attributes/date_time_spec.rb
93
+ - spec/unit/virtus/attributes/decimal_spec.rb
94
+ - spec/unit/virtus/attributes/float_spec.rb
95
+ - spec/unit/virtus/attributes/hash_spec.rb
96
+ - spec/unit/virtus/attributes/integer_spec.rb
97
+ - spec/unit/virtus/attributes/numeric/class_methods/descendants_spec.rb
98
+ - spec/unit/virtus/attributes/object/class_methods/descendants_spec.rb
99
+ - spec/unit/virtus/attributes/string_spec.rb
100
+ - spec/unit/virtus/attributes/time_spec.rb
101
+ - spec/unit/virtus/class_methods/new_spec.rb
102
+ - spec/unit/virtus/determine_type_spec.rb
103
+ - spec/unit/virtus/instance_methods/attribute_get_spec.rb
104
+ - spec/unit/virtus/instance_methods/attribute_set_spec.rb
105
+ - spec/unit/virtus/instance_methods/attributes_spec.rb
106
+ - virtus.gemspec
107
+ homepage: https://github.com/solnic/virtus
108
+ licenses: []
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 1.8.5
128
+ signing_key:
129
+ specification_version: 3
130
+ summary: Attributes for your plain ruby objects
131
+ test_files: []