attr_object 0.0.2 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 795639ebc0419308385132e91f80ab5cff0deb56
4
- data.tar.gz: 0bdae77cfe42a68ba399985381a4557511923597
3
+ metadata.gz: 6553f9b64e829966f46fe040a9f23e6723f8573d
4
+ data.tar.gz: d4e32fbf0fc83f86cbbc9d6912c68d46e78e7791
5
5
  SHA512:
6
- metadata.gz: ad8d0f825f7fdc047721babb390b84b7ef2cdb92f341fcfabc38a5447bc3bab235da4525d63011e08ce897a850ecec970396e1c6b15f7300be341bf67f675249
7
- data.tar.gz: 358cbeea5ab856bd39621f3b71d62c67579f0cfbbb38c0d81472fe3dbb57db3f75de1ae4f809abb7b0c5608652fb8f7fbd961998bfddb08d272adbe14ab35a53
6
+ metadata.gz: f6ab2ee78fbedd7ab76b602223b70bdc7057251526c08bec6643455ba82df517e06cd2ff33d7b6537dfc66a93bc004bcc19d7950e7aaa8038fba348e50eb1b00
7
+ data.tar.gz: c145f8f9c19872c1cc427472ea6dc2ef2660de7c522b0f5b61691427ef3eeee7bb79858128a9a5cf2af5cdea5aad38ba9be4cea0b3aad03ede88693be7a1579c
@@ -1,3 +1,3 @@
1
1
  module AttrObject
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,10 @@
1
+ Description:
2
+ Create an `attr_object` class (see https://github.com/dsimard/attr_object)
3
+
4
+ Example:
5
+ rails generate attr_object phone [class]
6
+
7
+ This will create:
8
+ app/attr_objects/phone_attr.rb
9
+
10
+ `class` must be a valid Ruby class
@@ -0,0 +1,22 @@
1
+ class AttrObjectGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def create_attr_object_file
5
+ #template "attr_object.rb", File.join('app/attr_objects/phone_attr.rb')
6
+ template "attr_object.rb", "app/attr_objects/#{lower_class_name}_attr.rb"
7
+ end
8
+
9
+ protected
10
+ def class_name
11
+ file_name.classify
12
+ end
13
+
14
+ def lower_class_name
15
+ class_name.underscore
16
+ end
17
+
18
+ def class_delegate
19
+ del = self.args.first
20
+ del.classify if del
21
+ end
22
+ end
@@ -0,0 +1,2 @@
1
+ class <%= class_name %>Attr<% if class_delegate %> < DelegateClass(<%= class_delegate %>)<% end %>
2
+ end
@@ -1,15 +1,10 @@
1
1
  require 'test_helper'
2
-
3
2
  require 'minitest/spec'
4
3
 
5
4
  describe AttrObject do
6
5
  DEFAULT_PHONE = "959-542-5256"
7
6
  DEFAULT_MOBILE = "878-858-5115"
8
7
 
9
- def userz(phone: DEFAULT_PHONE, mobile: DEFAULT_MOBILE, position: nil)
10
- User.new phone: phone, mobile: mobile, position: position
11
- end
12
-
13
8
  let(:user) do
14
9
  User.new phone: DEFAULT_PHONE, mobile: DEFAULT_MOBILE
15
10
  end
@@ -25,10 +20,10 @@ describe AttrObject do
25
20
  end
26
21
  end
27
22
 
28
- describe PhoneValue do
23
+ describe PhoneAttr do
29
24
  it "should return the right class on the attribute" do
30
- assert user.phone.is_a?(PhoneValue), "`phone` should be PhoneValue"
31
- assert user.mobile.is_a?(PhoneValue), "`mobile` should be PhoneValue"
25
+ assert user.phone.is_a?(PhoneAttr), "`phone` should be PhoneValue"
26
+ assert user.mobile.is_a?(PhoneAttr), "`mobile` should be PhoneValue"
32
27
  end
33
28
 
34
29
  it "should call a method from the value object" do
@@ -57,13 +52,13 @@ describe AttrObject do
57
52
  end
58
53
  end
59
54
 
60
- describe PositionValue do
55
+ describe PositionAttr do
61
56
  let(:user) do
62
57
  User.new position: 0
63
58
  end
64
59
 
65
60
  it "`position` should be a PositionValue" do
66
- _(user.position).must_be_instance_of PositionValue
61
+ _(user.position).must_be_instance_of PositionAttr
67
62
  end
68
63
 
69
64
  it "`position` should be updated as a `Fixnum`" do
@@ -1,4 +1,4 @@
1
- class PhoneValue < DelegateClass(String)
1
+ class PhoneAttr < DelegateClass(String)
2
2
  include Comparable
3
3
 
4
4
  # See https://en.wikipedia.org/wiki/North_American_Numbering_Plan
@@ -1,4 +1,4 @@
1
- class PositionValue
1
+ class PositionAttr
2
2
  attr_accessor :value
3
3
 
4
4
  def initialize(value)
@@ -1,4 +1,4 @@
1
1
  class User < ActiveRecord::Base
2
- attr_object :phone, :mobile, PhoneValue
3
- attr_object :position, PositionValue
2
+ attr_object :phone, :mobile, PhoneAttr
3
+ attr_object :position, PositionAttr
4
4
  end
Binary file
@@ -15018,3 +15018,31 @@ AttrObjectGeneratorTest: test_generator_runs_without_errors
15018
15018
  SQL (0.1ms) INSERT INTO "users" ("position", "created_at", "updated_at") VALUES (?, ?, ?) [["position", 1], ["created_at", "2016-01-12 11:44:25.913970"], ["updated_at", "2016-01-12 11:44:25.913970"]]
15019
15019
   (22.2ms) commit transaction
15020
15020
  User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 511]]
15021
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
15022
+  (0.1ms) begin transaction
15023
+ SQL (0.3ms) INSERT INTO "users" ("position", "created_at", "updated_at") VALUES (?, ?, ?) [["position", 99], ["created_at", "2016-01-12 11:49:21.780995"], ["updated_at", "2016-01-12 11:49:21.780995"]]
15024
+  (16.5ms) commit transaction
15025
+ User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 512]]
15026
+  (0.1ms) begin transaction
15027
+ SQL (0.1ms) INSERT INTO "users" ("position", "created_at", "updated_at") VALUES (?, ?, ?) [["position", 0], ["created_at", "2016-01-12 11:49:21.810000"], ["updated_at", "2016-01-12 11:49:21.810000"]]
15028
+  (24.7ms) commit transaction
15029
+ User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 513]]
15030
+  (0.1ms) begin transaction
15031
+ SQL (0.1ms) INSERT INTO "users" ("position", "created_at", "updated_at") VALUES (?, ?, ?) [["position", 1], ["created_at", "2016-01-12 11:49:21.837698"], ["updated_at", "2016-01-12 11:49:21.837698"]]
15032
+  (12.8ms) commit transaction
15033
+ User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 514]]
15034
+  (0.1ms) begin transaction
15035
+ ----------------------------------------------------------
15036
+ AttrObjectGeneratorTest: test_generator_with_invalid_class
15037
+ ----------------------------------------------------------
15038
+  (0.1ms) rollback transaction
15039
+  (0.0ms) begin transaction
15040
+ -----------------------------------------------------------
15041
+ AttrObjectGeneratorTest: test_generator_runs_without_errors
15042
+ -----------------------------------------------------------
15043
+  (0.1ms) rollback transaction
15044
+  (0.0ms) begin transaction
15045
+ --------------------------------------------------
15046
+ AttrObjectGeneratorTest: test_generator_with_class
15047
+ --------------------------------------------------
15048
+  (0.0ms) rollback transaction
@@ -0,0 +1,2 @@
1
+ class PositionAttr < DelegateClass(Fixnum)
2
+ end
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+ require 'generators/attr_object/attr_object_generator'
3
+
4
+ class AttrObjectGeneratorTest < Rails::Generators::TestCase
5
+ tests AttrObjectGenerator
6
+ destination Rails.root.join('tmp/generators')
7
+ setup :prepare_destination
8
+
9
+ test "generator runs without errors" do
10
+ assert_nothing_raised do
11
+ run_generator %w[phone]
12
+ end
13
+
14
+ assert_file "app/attr_objects/phone_attr.rb", /class PhoneAttr/
15
+ end
16
+
17
+ test "generator with class" do
18
+ assert_nothing_raised do
19
+ run_generator %w[position fixnum]
20
+ end
21
+
22
+ assert_file "app/attr_objects/position_attr.rb", /class PositionAttr < DelegateClass\(Fixnum\)/
23
+ end
24
+
25
+ test "generator with invalid class" do
26
+ assert_nothing_raised do
27
+ run_generator %w[phone invalid_ruby_class]
28
+ end
29
+
30
+ assert_file "app/attr_objects/phone_attr.rb", /class PhoneAttr < DelegateClass\(InvalidRubyClass\)/
31
+ end
32
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attr_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - dsimard
@@ -50,13 +50,17 @@ files:
50
50
  - Rakefile
51
51
  - lib/attr_object.rb
52
52
  - lib/attr_object/version.rb
53
+ - lib/generators/attr_object/USAGE
54
+ - lib/generators/attr_object/attr_object_generator.rb
55
+ - lib/generators/attr_object/templates/attr_object.rb
53
56
  - lib/tasks/attr_object_tasks.rake
57
+ - test/attr_object_test.rb
54
58
  - test/dummy/README.rdoc
55
59
  - test/dummy/Rakefile
56
60
  - test/dummy/app/assets/javascripts/application.js
57
61
  - test/dummy/app/assets/stylesheets/application.css
58
- - test/dummy/app/attr_objects/phone_value.rb
59
- - test/dummy/app/attr_objects/position_value.rb
62
+ - test/dummy/app/attr_objects/phone_attr.rb
63
+ - test/dummy/app/attr_objects/position_attr.rb
60
64
  - test/dummy/app/controllers/application_controller.rb
61
65
  - test/dummy/app/helpers/application_helper.rb
62
66
  - test/dummy/app/models/user.rb
@@ -98,9 +102,9 @@ files:
98
102
  - test/dummy/public/favicon.ico
99
103
  - test/dummy/test/fixtures/users.yml
100
104
  - test/dummy/test/models/user_test.rb
101
- - test/dummy/tmp/generators/app/attr_objects/phone_attr.rb
105
+ - test/dummy/tmp/generators/app/attr_objects/position_attr.rb
106
+ - test/lib/generators/attr_object_generator_test.rb
102
107
  - test/test_helper.rb
103
- - test/value_object_test.rb
104
108
  homepage: https://github.com/dsimard/attr_object
105
109
  licenses:
106
110
  - MIT
@@ -126,17 +130,16 @@ signing_key:
126
130
  specification_version: 4
127
131
  summary: Value Objects for Ruby on Rails
128
132
  test_files:
129
- - test/value_object_test.rb
130
133
  - test/dummy/Rakefile
131
134
  - test/dummy/app/controllers/application_controller.rb
132
135
  - test/dummy/app/helpers/application_helper.rb
133
136
  - test/dummy/app/models/user.rb
134
137
  - test/dummy/app/views/layouts/application.html.erb
135
- - test/dummy/app/attr_objects/phone_value.rb
136
- - test/dummy/app/attr_objects/position_value.rb
138
+ - test/dummy/app/attr_objects/phone_attr.rb
139
+ - test/dummy/app/attr_objects/position_attr.rb
137
140
  - test/dummy/app/assets/stylesheets/application.css
138
141
  - test/dummy/app/assets/javascripts/application.js
139
- - test/dummy/tmp/generators/app/attr_objects/phone_attr.rb
142
+ - test/dummy/tmp/generators/app/attr_objects/position_attr.rb
140
143
  - test/dummy/README.rdoc
141
144
  - test/dummy/public/500.html
142
145
  - test/dummy/public/422.html
@@ -175,5 +178,7 @@ test_files:
175
178
  - test/dummy/bin/setup
176
179
  - test/dummy/bin/rails
177
180
  - test/dummy/bin/rake
181
+ - test/attr_object_test.rb
178
182
  - test/test_helper.rb
183
+ - test/lib/generators/attr_object_generator_test.rb
179
184
  has_rdoc:
@@ -1,2 +0,0 @@
1
- class PhoneAttr
2
- end