attr_hash_accessor 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ ## 0.0.2 (Oct 04, 2012)
2
+
3
+ * add `attr_hash_accessor`, defines both setter and getter *hibariya*
4
+
5
+ ## 0.0.1 (Sep 25, 2012)
6
+
7
+ * initial release
8
+
data/README.md CHANGED
@@ -26,13 +26,18 @@ class MyClass
26
26
 
27
27
  attr_hash_writer :bar
28
28
  attr_hash_reader :foo, :bar
29
+ attr_hash_accessor :baz
29
30
  end
30
31
 
31
- obj = MyClass.new(foo: 'FOO', bar: 'BAR')
32
- obj.foo #=> 'FOO'
32
+ obj = MyClass.new(foo: 'FOO', bar: 'BAR', baz: 'BAZ')
33
+ obj.foo # => 'FOO'
33
34
 
34
35
  obj.bar = 'BARBAR'
35
36
  obj.bar # => 'BARBAR'
37
+
38
+ obj.baz # => 'BAZ'
39
+ obj.baz = 'BAZBAZ'
40
+ obj.baz # => 'BAZBAZ'
36
41
  ```
37
42
 
38
43
  ### Getter with filter
@@ -41,15 +46,40 @@ obj.bar # => 'BARBAR'
41
46
  class MyClass
42
47
  include AttrHashAccessor
43
48
 
44
- attr_hash_writer :vars
45
- attr_hash_reader :vars, ->(val) { Array(val) }
49
+ attr_hash_writer :foo
50
+ attr_hash_reader :foo, &->(val) { Array(val) }
51
+ attr_hash_accessor :bar, &->(val) { val || {} }
52
+ end
53
+
54
+ obj = MyClass.new
55
+ obj.foo # => []
56
+ obj.bar # => {}
57
+
58
+ obj.foo = [1, 2, 3]
59
+ obj.foo # => [1, 2, 3]
60
+
61
+ obj.bar = {lorem: 'ipsum'}
62
+ obj.bar # => {:lorem=>"ipsum"}
63
+ ```
64
+
65
+ ### Define default
66
+
67
+ ```
68
+ class MyClass
69
+ include AttrHashAccessor
70
+
71
+ attr_hash_reader :bar
72
+
73
+ def self.default
74
+ {bar: 'BAR'}
75
+ end
46
76
  end
47
77
 
48
78
  obj = MyClass.new
49
- obj.vars # => []
79
+ obj.bar # => 'BAR'
50
80
 
51
- obj.vars = [1, 2, 3]
52
- obj.vars #=> [1, 2, 3]
81
+ obj = MyClass.new(bar: 'baz')
82
+ obj.bar # => 'baz'
53
83
  ```
54
84
 
55
85
  ## Contributing
@@ -1,3 +1,3 @@
1
1
  module AttrHashAccessor
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -13,7 +13,8 @@ module AttrHashAccessor
13
13
  base.extend ClassMethods
14
14
  end
15
15
 
16
- def initialize(val = {})
16
+ def initialize(val = nil)
17
+ val = {} unless val.present?
17
18
  @attributes = self.class.default.stringify_keys.merge(val.stringify_keys)
18
19
  end
19
20
 
@@ -32,11 +33,16 @@ module AttrHashAccessor
32
33
  cols.each do |attr|
33
34
  define_method(attr) do
34
35
  val = @attributes[attr.to_s]
35
- (val.present? && filter) ? filter.call(val) : val
36
+ filter ? filter.call(val) : val
36
37
  end
37
38
  end
38
39
  end
39
40
 
41
+ def attr_hash_accessor(*cols, &filter)
42
+ attr_hash_writer *cols
43
+ attr_hash_reader *cols, &filter
44
+ end
45
+
40
46
  def default
41
47
  {}
42
48
  end
@@ -2,25 +2,77 @@ require 'spec_helper'
2
2
  require 'attr_hash_accessor'
3
3
 
4
4
  describe AttrHashAccessor do
5
- describe 'Basic usage' do
6
- let(:my_klass) do
7
- Class.new do
8
- include AttrHashAccessor
5
+ let(:my_klass) do
6
+ Class.new do
7
+ include AttrHashAccessor
9
8
 
10
- attr_hash_reader :foo
11
- attr_hash_writer :foo
12
- end
9
+ attr_hash_reader :foo
10
+ attr_hash_writer :foo
11
+ attr_hash_accessor :bar
13
12
  end
13
+ end
14
14
 
15
- subject(:object) { my_klass.new(foo: 'bar') }
15
+ describe 'Basic usage' do
16
+ subject(:object) { my_klass.new(foo: 'bar', bar: 'baz') }
16
17
 
17
18
  its(:foo) { should == 'bar' }
19
+ its(:bar) { should == 'baz' }
18
20
 
19
- context 'writer' do
21
+ context 'writer / reader' do
20
22
  before do
21
23
  object.foo = 'FOOO'
22
24
  end
25
+
23
26
  its(:foo) { should == 'FOOO' }
24
27
  end
28
+
29
+ context 'accessor' do
30
+ before do
31
+ object.bar = 'BARRR'
32
+ end
33
+
34
+ its(:bar) { should == 'BARRR' }
35
+ end
36
+ end
37
+
38
+ describe 'empty initialize' do
39
+ specify do
40
+ expect { my_klass.new }.not_to raise_exception
41
+ end
42
+
43
+ specify do
44
+ expect { my_klass.new(nil) }.not_to raise_exception
45
+ end
46
+ end
47
+
48
+ describe 'filter attribute' do
49
+ let(:my_klass2) do
50
+ Class.new do
51
+ include AttrHashAccessor
52
+ attr_hash_reader :arr, &->(v) { v.presence || [1] }
53
+ end
54
+ end
55
+ subject(:object) { my_klass2.new(nil) }
56
+ its(:arr) { should == [1] }
57
+ end
58
+
59
+ describe 'setting default' do
60
+ let(:my_klass) do
61
+ Class.new do
62
+ include AttrHashAccessor
63
+ attr_hash_accessor :bar
64
+
65
+ def self.default
66
+ {bar: 'BAZ'}
67
+ end
68
+ end
69
+ end
70
+ specify do
71
+ expect(my_klass.new.bar).to eq 'BAZ'
72
+ end
73
+
74
+ specify do
75
+ expect(my_klass.new(bar: 'val').bar).to eq 'val'
76
+ end
25
77
  end
26
78
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attr_hash_accessor
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: 2012-09-24 00:00:00.000000000 Z
12
+ date: 2012-10-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -83,6 +83,7 @@ extensions: []
83
83
  extra_rdoc_files: []
84
84
  files:
85
85
  - .gitignore
86
+ - CHANGELOG.md
86
87
  - Gemfile
87
88
  - LICENSE.txt
88
89
  - README.md
@@ -106,7 +107,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
106
107
  version: '0'
107
108
  segments:
108
109
  - 0
109
- hash: -3852618035639917224
110
+ hash: 606188242904257753
110
111
  required_rubygems_version: !ruby/object:Gem::Requirement
111
112
  none: false
112
113
  requirements:
@@ -115,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
116
  version: '0'
116
117
  segments:
117
118
  - 0
118
- hash: -3852618035639917224
119
+ hash: 606188242904257753
119
120
  requirements: []
120
121
  rubyforge_project:
121
122
  rubygems_version: 1.8.24