has_attributes 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YzJlODZkMGM5MTI5NWFiYzM5YTE3NDYxNzgxODg2MjA3MGJlZTIyZg==
5
+ data.tar.gz: !binary |-
6
+ MjNmNTRkYTMwNTVkMzg4YTBkNzVmOGU3ODA4ODRiNjNhNmIwMjhjMQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZjVlNmUyNTlkMWViMzEzZDE1YzI3NzkwN2Q1YmU5ZDJmYjFiYmE4ZWQyMDUy
10
+ ZDJmNzMzNzc1YzVmNTk1MzQwYmMwMDcwZmIzYzgyNDQ2NTUwMzBhMzI0Zjhj
11
+ NWZlMzNkZmQ4OTU5ZWViN2YzMTQwNDQyNDZiMDViZDFhMDg4ZGE=
12
+ data.tar.gz: !binary |-
13
+ NmQxZGFjMWY1YzQ1M2VkZTMwYTcwNjdjNGY0MmZkMTNhNGY4ODVhNWI0OTk1
14
+ YmFhNmEwNzE3NTQwMjEyMTk2NDI2NWU5OGQzZjc3MGNjM2M2OTVhNTY4OGM3
15
+ OWJmOTA4YzA2NGE2NmUwNTQzNDEwNDBjZDIzODAzYTNhNDlmMGQ=
data/History.md CHANGED
@@ -1,4 +1,12 @@
1
- 0.0.2 / 2014-02-03
1
+ 0.0.3 / 2014-02-08
2
+ ==================
3
+
4
+ * Set model_attributes to an empty set in included hook
5
+ * Filter out nils from #attributes calls
6
+ * Update readme
7
+
8
+
9
+ 0.0.2 / 2014-02-07
2
10
  ==================
3
11
 
4
12
  * Use public_send for public instance methods
data/README.md CHANGED
@@ -12,15 +12,15 @@ has_attributes delivers the following class and instance methods:
12
12
 
13
13
  #### .model_attributes() → Set
14
14
 
15
- An attribute accessor on the class that contains a set of attributes specified with `has_attributes`
15
+ An attribute accessor on the class that contains a set of attributes specified with `has_attributes`. It is added to the class via the included hook when `include`ing the module. If there is no call to `has_attributes`, then calling `model_attributes` on the class will be `nil`.
16
16
 
17
17
  #### .has_attributes(*args) → nil
18
18
 
19
- Takes any number of symbols or strings and defines attribute accessors for those attributes. It also defines a `model_attributes` method on the class which will return the set of attributes. `has_attributes` will attempt to copy any existing attributes from the parent class unless the last argument is a `Hash` and contains an `inherit` key set to `false`.
19
+ Takes any number of symbols or strings and defines attribute accessors for those attributes. `has_attributes` will attempt to copy any existing attributes from the parent class unless the last argument is a `Hash` and contains an `inherit` key set to `false`.
20
20
 
21
21
  #### #attributes() → Hash
22
22
 
23
- Returns a hash whose keys are all the attributes declared `has_attributes` and the values are their corresponding values. If the values have not been set, the key will still be present in the hash but with a value of `nil`.
23
+ Returns a hash whose keys are the attributes set with `has_attributes`. Only the keys whose values are non `nil` will be returned.
24
24
 
25
25
  #### #attributes=(attrs) → Hash
26
26
 
@@ -19,6 +19,6 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rake"
23
- spec.add_development_dependency "rspec"
22
+ spec.add_development_dependency "rake", "~> 10.1"
23
+ spec.add_development_dependency "rspec", "~> 2.14"
24
24
  end
@@ -1,3 +1,3 @@
1
1
  module HasAttributes
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,9 +1,10 @@
1
- require "has_attributes/version"
1
+ require 'has_attributes/version'
2
2
  require "set"
3
3
 
4
4
  module HasAttributes
5
5
  def self.included(base)
6
6
  base.class.send(:attr_accessor, :model_attributes)
7
+ base.send(:model_attributes=, Set.new)
7
8
  base.extend(ClassMethods)
8
9
  end
9
10
 
@@ -18,7 +19,7 @@ module HasAttributes
18
19
  Set.new
19
20
  end
20
21
 
21
- attrs = (model_attributes || Set.new).merge(attrs.map(&:to_sym))
22
+ attrs = model_attributes.merge(attrs.map(&:to_sym))
22
23
 
23
24
  if inherit_parent_attributes
24
25
  attrs = parent_attributes.merge(attrs)
@@ -38,13 +39,17 @@ module HasAttributes
38
39
  end
39
40
 
40
41
  def attributes=(attrs)
41
- self.class.model_attributes.each {|attr| public_send((attr.to_s << "=").to_sym, attrs[attr])}
42
- attributes
42
+ self.class.model_attributes.each do |attr|
43
+ public_send((attr.to_s << "=").to_sym, attrs[attr])
44
+ end
43
45
  end
44
46
 
45
47
  def attributes
46
48
  self.class.model_attributes.reduce({}) do |memo, attr|
47
- memo.merge(attr => public_send(attr))
49
+ unless (value = public_send(attr)).nil?
50
+ memo[attr] = value
51
+ end
52
+ memo
48
53
  end
49
54
  end
50
55
  end
@@ -78,6 +78,13 @@ describe HasAttributes do
78
78
  describe "#attributes" do
79
79
  let(:klass) { create_model :attr1, :attr2 }
80
80
 
81
+ it "returns an empty hash when no attributes were defined" do
82
+ instance = Class.new { include HasAttributes }.new
83
+
84
+ instance.attributes = {a: true, b: false}
85
+ expect(instance.attributes).to eq({})
86
+ end
87
+
81
88
  it "sets a hash of attributes on the instance" do
82
89
  instance = klass.new
83
90
 
@@ -98,7 +105,8 @@ describe HasAttributes do
98
105
  expect(instance.attr1).to be_nil
99
106
  expect(instance.attr2).to be_false
100
107
 
101
- expect(instance.attributes).to eq(attr1: nil, attr2: false)
108
+ expect(instance.attributes).not_to include(:attr1)
109
+ expect(instance.attributes).to include(attr2: false)
102
110
  end
103
111
 
104
112
  it "only sets attributes that were declared with .has_attributes" do
@@ -109,7 +117,9 @@ describe HasAttributes do
109
117
  expect(instance.attr1).to be_true
110
118
  expect(instance.attr2).to be_nil
111
119
 
112
- expect(instance.attributes).to eq(attr1: true, attr2: nil)
120
+ expect(instance.attributes).to include(attr1: true)
121
+ expect(instance.attributes).not_to include(:attr2)
122
+ expect(instance.attributes).not_to include(:another_attr)
113
123
  end
114
124
  end
115
125
  end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.0.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - Ben Reinhart
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-02-07 00:00:00.000000000 Z
11
+ date: 2014-02-09 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
@@ -30,35 +27,31 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ~>
36
32
  - !ruby/object:Gem::Version
37
- version: '0'
33
+ version: '10.1'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ~>
44
39
  - !ruby/object:Gem::Version
45
- version: '0'
40
+ version: '10.1'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ~>
52
46
  - !ruby/object:Gem::Version
53
- version: '0'
47
+ version: '2.14'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - ~>
60
53
  - !ruby/object:Gem::Version
61
- version: '0'
54
+ version: '2.14'
62
55
  description: Extend your classes with simple methods for creating elegant plain-ruby
63
56
  models
64
57
  email:
@@ -82,27 +75,26 @@ files:
82
75
  homepage: https://github.com/benjreinhart/has_attributes
83
76
  licenses:
84
77
  - MIT
78
+ metadata: {}
85
79
  post_install_message:
86
80
  rdoc_options: []
87
81
  require_paths:
88
82
  - lib
89
83
  required_ruby_version: !ruby/object:Gem::Requirement
90
- none: false
91
84
  requirements:
92
85
  - - ! '>='
93
86
  - !ruby/object:Gem::Version
94
87
  version: '0'
95
88
  required_rubygems_version: !ruby/object:Gem::Requirement
96
- none: false
97
89
  requirements:
98
90
  - - ! '>='
99
91
  - !ruby/object:Gem::Version
100
92
  version: '0'
101
93
  requirements: []
102
94
  rubyforge_project:
103
- rubygems_version: 1.8.23
95
+ rubygems_version: 2.2.2
104
96
  signing_key:
105
- specification_version: 3
97
+ specification_version: 4
106
98
  summary: Better plain-ruby models
107
99
  test_files:
108
100
  - spec/has_attributes_spec.rb