pethau 0.0.1 → 0.0.2

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.
data/README.md CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  A bunch of code that we find useful to share amongst our projects.
4
4
 
5
- This is the place that code lives when it doesn't have a larger purpose; in
6
- less awesome teams this would be the "utility" package.
5
+ This is the place that code lives when it doesn't have a larger purpose;
6
+ in less awesome teams this would be the "utility" package.
7
7
 
8
8
  Pethau is Welsh for "things," which I think captures the general purpose
9
9
  nature of this gem nicely.
@@ -22,18 +22,78 @@ A lot of my classes use the following initializer pattern:
22
22
  private :baz=, :baz
23
23
 
24
24
  def initialize bar, baz
25
- self.bar = bar
26
- self.baz = baz
25
+ self.bar = bar
26
+ self.baz = baz
27
27
  end
28
28
  end
29
29
 
30
- I've been doing this for a while now, and I believe it's established itself as
31
- enough of a pattern that I want to pull out the implementation and DRY it up:
30
+ I've been doing this for a while now, and I believe it's established
31
+ itself as enough of a pattern that I want to pull out the implementation
32
+ and DRY it up:
32
33
 
33
34
  class Foo
34
35
  include Pethau::InitializeWith
35
36
  initialize_with :foo, :bar
36
37
  end
37
38
 
38
- ince this is used in a lot of places in my projects I tend to include it in
39
- `Object` but I didn't want to make that decision for you.
39
+ Since this is used in a lot of places in my projects I tend to include
40
+ it in `Object` but I didn't want to make that decision for you.
41
+
42
+
43
+ ### default\_value\_of
44
+
45
+ It's useful to provide default values for attribute when they're not defined, but it's annoying to type code like this all the time:
46
+
47
+ class Quux
48
+ attr_writer :corge, :foo
49
+
50
+ def corge
51
+ @corge || "Default Value"
52
+ end
53
+
54
+ def foo
55
+ @foo || FooBuilder.new.execute
56
+ end
57
+ end
58
+
59
+ Instead, I prefer this:
60
+
61
+ class Quux
62
+ attr_accessor :corge, :foo
63
+ default_value_of :corge, "Default Value"
64
+ default_value_of :foo do
65
+ FooBuilder.new.execute
66
+ end
67
+ end
68
+
69
+ Combining it with InitializeWith yields a rather nice, readable, concise
70
+ class definition:
71
+
72
+ class Grault
73
+ initialize_with :garply, :waldo
74
+ default_value_of :waldo do
75
+ plugh = Plugh.new
76
+ plugh.execute
77
+ end
78
+ end
79
+
80
+
81
+ ### private\_attr\_accessor
82
+
83
+ I tend to avoid using instance variables where possible, my opinion
84
+ being that they couple you to the implementation of the class where
85
+ using accessors tie you only to the internal class interface - I've
86
+ found that the later makes it faster and easier to refactor my code.
87
+
88
+ Instead of typing this all the time:
89
+
90
+ class Boggle
91
+ attr_accessor :orf, :gin
92
+ private :orf=, :orf, :gin=, :gin
93
+ end
94
+
95
+ I can type this:
96
+
97
+ class Boggle
98
+ private_attr_accessor :orf, :gin
99
+ end
@@ -0,0 +1,28 @@
1
+ module Pethau
2
+ module DefaultValueOf
3
+ def self.included into
4
+ into.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def default_value_of attribute_name, default_value = nil, &builder
9
+ if default_value && block_given?
10
+ raise "Only provide one of default value or builder"
11
+ end
12
+
13
+ getter_with_default = "#{attribute_name}_with_default"
14
+ getter_without_default = "#{attribute_name}_without_default"
15
+ alias_method getter_without_default, attribute_name
16
+
17
+ define_method getter_with_default do
18
+ original_value = send getter_without_default
19
+ return original_value unless original_value.nil?
20
+ return builder.call if block_given?
21
+ default_value
22
+ end
23
+ alias_method attribute_name, getter_with_default
24
+ end
25
+ private :default_value_of
26
+ end
27
+ end
28
+ end
@@ -6,16 +6,17 @@ module Pethau
6
6
 
7
7
  module ClassMethods
8
8
  def initialize_with *args
9
- args.each do |attribute|
10
- attr_accessor attribute
11
- private attribute, "#{attribute}="
12
- end
9
+ args.each do |attribute|
10
+ attr_accessor attribute
11
+ private attribute, "#{attribute}="
12
+ end
13
13
 
14
- define_method :initialize do |*initial_args|
15
- args.each do |arg|
16
- send "#{arg}=", initial_args.shift
17
- end
18
- end
14
+ define_method :initialize do |*initial_args|
15
+ args.each do |arg|
16
+ break if initial_args.empty?
17
+ send "#{arg}=", initial_args.shift
18
+ end
19
+ end
19
20
  end
20
21
  private :initialize_with
21
22
  end
@@ -0,0 +1,17 @@
1
+ module Pethau
2
+ module PrivateAttrAccessor
3
+ def self.included into
4
+ into.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def private_attr_accessor *attribute_names
9
+ attr_accessor *attribute_names
10
+ attribute_names.each do |attribute_name|
11
+ private attribute_name, "#{attribute_name}="
12
+ end
13
+ end
14
+ private :private_attr_accessor
15
+ end
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module Pethau
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
data/lib/pethau.rb CHANGED
@@ -1,2 +1,4 @@
1
1
  require 'pethau/version'
2
2
  require 'pethau/initialize_with'
3
+ require 'pethau/default_value_of'
4
+ require 'pethau/private_attr_accessor'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pethau
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-06 00:00:00.000000000Z
12
+ date: 2011-10-25 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: Extracting a bunch of code that we use across our projects. In less awesome
15
15
  teams this would be the utility package.
@@ -19,7 +19,9 @@ executables: []
19
19
  extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
+ - lib/pethau/default_value_of.rb
22
23
  - lib/pethau/initialize_with.rb
24
+ - lib/pethau/private_attr_accessor.rb
23
25
  - lib/pethau/version.rb
24
26
  - lib/pethau.rb
25
27
  - README.md