genderize 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 130eb88d15b63e6b884dbc13af79ca7378bcbe92
4
- data.tar.gz: 06d1941d8108fa31d1f698a3a73e591ea3eaa5a9
3
+ metadata.gz: f590ec5a9831dd9016d6b09cca52d9b7b3f61953
4
+ data.tar.gz: f5173c96546200eb7eb22277c19afc4d43878cdd
5
5
  SHA512:
6
- metadata.gz: 474518f5236848d1f2833308f59c64a1b6be51ec85fa70e7d6dff8c40e3a5137a23ec05c68da30440098df08b8249cfba2a2575cb07d712b8936547a0c0a605c
7
- data.tar.gz: cef4b252424262c974f33575c920e8ad37b28a33f9b649ce4bf71bcc37e4f915b462013bf1afab20a3c8d333e8aeecc8803c90a1781468642153bc7df34ddbce
6
+ metadata.gz: da8cf63be0f3e3c1e756a8e02e8144f6c7806a4b05ec6c60436d43e71a6262129aee4f7cda4d155181d6abdf4ab0b5b9cac134304e0578d1171cbe9ab1b37c53
7
+ data.tar.gz: 5b9ae766aa1e2fae6679da160a112a0e655a2c57e38982c9068d3c62e827dca04a6e7b957d908f1b297cfab2c30e46f1d3e8db1a05e22b30408153d391dea8dd
@@ -2,48 +2,63 @@ module Genderize
2
2
  class Gender
3
3
 
4
4
  include I18n
5
-
6
- attr_reader :name, :abbr, :subject, :object, :possessive, :casual
7
-
8
- def initialize(abbr)
9
- raise "Invalid abbreviation" unless abbr.to_s =~ /\A(f|m)\Z/i
10
- @abbr = abbr.downcase
11
- @male = self.abbr == 'm'
12
- @name = male? ? t("name") : t("name", "feminine")
13
5
 
14
- # pronouns
15
- @subject = male? ? t("subject") : t("subject", "feminine")
16
- @object = male? ? t("object") : t("object", "feminine")
17
- @possessive = male? ? t("possessive") : t("possessive", "feminine")
6
+ attr_reader :abbr
7
+
8
+ def initialize(abbr)
9
+ raise "Invalid abbreviation: '#{abbr}'" unless abbr.blank? or abbr.to_s =~ /\A(f|m)\Z/i
10
+ @abbr = abbr.blank? ? nil : abbr.to_s.downcase
11
+ end
18
12
 
19
- # Other forms
20
- @casual = male? ? t("casual") : t("casual", "feminine")
13
+ def name
14
+ @name ||= translation_for("name")
21
15
  end
22
16
 
17
+ def subject
18
+ @subject ||= translation_for("subject")
19
+ end
20
+
21
+ def object
22
+ @object ||= translation_for("object")
23
+ end
24
+
25
+ def possessive
26
+ @possessive ||= translation_for("possessive")
27
+ end
28
+
29
+ def casual
30
+ @casual ||= translation_for("casual")
31
+ end
32
+
23
33
  def capital_abbr
24
34
  abbr.capitalize
25
35
  end
26
36
 
27
37
  def male?
28
- @male
38
+ abbr == 'm'
29
39
  end
30
40
 
31
41
  def female?
32
- !male?
42
+ abbr == 'f'
33
43
  end
34
44
 
35
45
  def to_s
36
- @abbr
46
+ abbr
37
47
  end
38
48
 
39
49
  def ==(val)
40
- to_s == val.to_s
50
+ abbr.to_s == val.to_s
41
51
  end
42
52
 
43
53
  private
44
-
45
- def t(attribute, gender = "masculine")
46
- I18n.t("genderize.#{attribute}.#{gender}")
54
+
55
+ def translation_for(key)
56
+ case
57
+ when male? then I18n.t("genderize.#{key}.masculine")
58
+ when female? then I18n.t("genderize.#{key}.feminine")
59
+ else
60
+ nil
61
+ end
47
62
  end
48
63
 
49
64
  end
@@ -1,5 +1,3 @@
1
- require "genderize/gender"
2
-
3
1
  module Genderize::Genderize
4
2
 
5
3
  def self.included(base)
@@ -7,9 +5,9 @@ module Genderize::Genderize
7
5
  end
8
6
 
9
7
  module ClassMethods
8
+ require "genderize/gender"
10
9
 
11
10
  def genderize(col_name = "gender")
12
-
13
11
  # Reads the DB column value for gender attribute and creates a new Gender
14
12
  # object with it's value
15
13
  #
@@ -19,8 +17,12 @@ module Genderize::Genderize
19
17
  define_method col_name do
20
18
  if value = instance_variable_get("@#{col_name}")
21
19
  return value
20
+ end
21
+ read_value = read_attribute(col_name)
22
+ if read_value.blank?
23
+ return read_value
22
24
  else
23
- instance_variable_set("@#{col_name}", Gender.new(read_attribute(col_name)))
25
+ instance_variable_set("@#{col_name}", Genderize::Gender.new(read_attribute(col_name)))
24
26
  end
25
27
  end
26
28
 
@@ -31,11 +33,16 @@ module Genderize::Genderize
31
33
  #
32
34
  # Raises ArgumentError if gender is not a single alphanumeric character "m" or "f"
33
35
  define_method "#{col_name}=" do |string|
34
- unless string.to_s =~ /\A(m|f)\Z/i
36
+ unless string.blank? or string.to_s =~ /\A(m|f)\Z/i
35
37
  raise ArgumentError, "Gender must be a single alphanumeric character"
36
38
  end
37
39
  write_attribute(col_name, string)
38
- instance_variable_set("@#{col_name}", Gender.new(read_attribute(col_name)))
40
+
41
+ if string.blank?
42
+ instance_variable_set("@#{col_name}", string)
43
+ else
44
+ instance_variable_set("@#{col_name}", Genderize::Gender.new(read_attribute(col_name)))
45
+ end
39
46
  end
40
47
 
41
48
  end
@@ -1,3 +1,3 @@
1
1
  module Genderize
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
Binary file
@@ -35,3 +35,58 @@ Connecting to database specified by database.yml
35
35
  Connecting to database specified by database.yml
36
36
  Connecting to database specified by database.yml
37
37
  Connecting to database specified by database.yml
38
+ Connecting to database specified by database.yml
39
+  (0.1ms) begin transaction
40
+ SQL (35.2ms) INSERT INTO "users" ("created_at", "gender", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Mon, 06 May 2013 14:54:57 UTC +00:00], ["gender", "f"], ["name", "f"], ["updated_at", Mon, 06 May 2013 14:54:57 UTC +00:00]]
41
+  (4.5ms) commit transaction
42
+ User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
43
+ Connecting to database specified by database.yml
44
+  (0.1ms) begin transaction
45
+ SQL (3.0ms) INSERT INTO "users" ("created_at", "gender", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Mon, 06 May 2013 14:55:10 UTC +00:00], ["gender", "f"], ["name", "f"], ["updated_at", Mon, 06 May 2013 14:55:10 UTC +00:00]]
46
+  (53.6ms) commit transaction
47
+ User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
48
+ Connecting to database specified by database.yml
49
+  (0.1ms) begin transaction
50
+ SQL (3.0ms) INSERT INTO "users" ("created_at", "gender", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Mon, 06 May 2013 14:55:52 UTC +00:00], ["gender", "f"], ["name", "f"], ["updated_at", Mon, 06 May 2013 14:55:52 UTC +00:00]]
51
+  (58.9ms) commit transaction
52
+ User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
53
+ Connecting to database specified by database.yml
54
+  (0.1ms) begin transaction
55
+ SQL (3.1ms) INSERT INTO "users" ("created_at", "gender", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Mon, 06 May 2013 14:57:23 UTC +00:00], ["gender", "f"], ["name", "f"], ["updated_at", Mon, 06 May 2013 14:57:23 UTC +00:00]]
56
+  (56.5ms) commit transaction
57
+ User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
58
+ Connecting to database specified by database.yml
59
+  (0.1ms) begin transaction
60
+ SQL (3.0ms) INSERT INTO "users" ("created_at", "gender", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Mon, 06 May 2013 14:57:33 UTC +00:00], ["gender", "f"], ["name", "f"], ["updated_at", Mon, 06 May 2013 14:57:33 UTC +00:00]]
61
+  (54.0ms) commit transaction
62
+ User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
63
+ Connecting to database specified by database.yml
64
+  (0.1ms) begin transaction
65
+ SQL (3.1ms) INSERT INTO "users" ("created_at", "gender", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Mon, 06 May 2013 14:58:24 UTC +00:00], ["gender", "f"], ["name", "f"], ["updated_at", Mon, 06 May 2013 14:58:24 UTC +00:00]]
66
+  (4.0ms) commit transaction
67
+ User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
68
+ Connecting to database specified by database.yml
69
+  (0.1ms) begin transaction
70
+ SQL (3.2ms) INSERT INTO "users" ("created_at", "gender", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Mon, 06 May 2013 14:58:57 UTC +00:00], ["gender", "f"], ["name", "f"], ["updated_at", Mon, 06 May 2013 14:58:57 UTC +00:00]]
71
+  (59.5ms) commit transaction
72
+ User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
73
+ Connecting to database specified by database.yml
74
+  (0.1ms) begin transaction
75
+ SQL (3.2ms) INSERT INTO "users" ("created_at", "gender", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Mon, 06 May 2013 14:59:21 UTC +00:00], ["gender", "f"], ["name", "f"], ["updated_at", Mon, 06 May 2013 14:59:21 UTC +00:00]]
76
+  (57.2ms) commit transaction
77
+ User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
78
+ Connecting to database specified by database.yml
79
+  (0.1ms) begin transaction
80
+ SQL (3.1ms) INSERT INTO "users" ("created_at", "gender", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Mon, 06 May 2013 15:00:22 UTC +00:00], ["gender", "f"], ["name", "f"], ["updated_at", Mon, 06 May 2013 15:00:22 UTC +00:00]]
81
+  (55.7ms) commit transaction
82
+ User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
83
+ Connecting to database specified by database.yml
84
+  (0.1ms) begin transaction
85
+ SQL (3.3ms) INSERT INTO "users" ("created_at", "gender", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Mon, 06 May 2013 15:00:58 UTC +00:00], ["gender", "f"], ["name", "f"], ["updated_at", Mon, 06 May 2013 15:00:58 UTC +00:00]]
86
+  (131.4ms) commit transaction
87
+ User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
88
+ Connecting to database specified by database.yml
89
+  (0.1ms) begin transaction
90
+ SQL (3.1ms) INSERT INTO "users" ("created_at", "gender", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Mon, 06 May 2013 15:01:20 UTC +00:00], ["gender", "f"], ["name", "f"], ["updated_at", Mon, 06 May 2013 15:01:20 UTC +00:00]]
91
+  (57.0ms) commit transaction
92
+ User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
@@ -4,6 +4,7 @@ describe Genderize::Gender do
4
4
 
5
5
  let(:female) { Gender.new("f") }
6
6
  let(:male) { Gender.new("M") }
7
+ let(:blank) { Gender.new('') }
7
8
 
8
9
  describe :name do
9
10
 
@@ -23,6 +24,15 @@ describe Genderize::Gender do
23
24
 
24
25
  end
25
26
 
27
+ context "when blank" do
28
+
29
+ it "should be nil" do
30
+ blank.name.should be_nil
31
+ end
32
+
33
+ end
34
+
35
+
26
36
  end
27
37
 
28
38
  describe :abbr do
@@ -42,6 +52,15 @@ describe Genderize::Gender do
42
52
  end
43
53
 
44
54
  end
55
+
56
+ context "when blank" do
57
+
58
+ it "should be nil" do
59
+ blank.abbr.should be_nil
60
+ end
61
+
62
+ end
63
+
45
64
 
46
65
  end
47
66
 
@@ -62,6 +81,15 @@ describe Genderize::Gender do
62
81
  end
63
82
 
64
83
  end
84
+
85
+ context "when blank" do
86
+
87
+ it "should be nil" do
88
+ blank.subject.should be_nil
89
+ end
90
+
91
+ end
92
+
65
93
  end
66
94
 
67
95
  describe :object do
@@ -82,6 +110,14 @@ describe Genderize::Gender do
82
110
 
83
111
  end
84
112
 
113
+ context "when blank" do
114
+
115
+ it "should be nil" do
116
+ blank.object.should be_nil
117
+ end
118
+
119
+ end
120
+
85
121
 
86
122
  end
87
123
 
@@ -103,6 +139,15 @@ describe Genderize::Gender do
103
139
 
104
140
  end
105
141
 
142
+ context "when blank" do
143
+
144
+ it "should be nil" do
145
+ blank.possessive.should be_nil
146
+ end
147
+
148
+ end
149
+
150
+
106
151
 
107
152
  end
108
153
 
@@ -124,6 +169,15 @@ describe Genderize::Gender do
124
169
 
125
170
  end
126
171
 
172
+ context "when blank" do
173
+
174
+ it "should be nil" do
175
+ blank.casual.should be_nil
176
+ end
177
+
178
+ end
179
+
180
+
127
181
  end
128
182
 
129
183
  describe :to_s do
@@ -153,12 +207,14 @@ describe Genderize::Gender do
153
207
 
154
208
  it "should return true if passed abbr value" do
155
209
  (male == "m").should be_true
156
- (female == "f").should be_true
210
+ (female == "f").should be_true
211
+ (blank == nil).should be_true
157
212
  end
158
213
 
159
214
  it "should return false if not passed abbr value" do
160
215
  (male == "f").should be_false
161
216
  (female == 1).should be_false
217
+ (blank == "$").should be_false
162
218
  end
163
219
 
164
220
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: genderize
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
  - Bodacious