attributable 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: 0ae2deaa3e88ed46bd80f2f96e721c71093f06ca
4
- data.tar.gz: b0253f219bc607ccb07ac8b9bdedc76a7af16b97
3
+ metadata.gz: 167cb3bfb8d4237095209222dd5e925e909b3e6c
4
+ data.tar.gz: 99d532b61ef03948d2e43a3c00412f54323a7978
5
5
  SHA512:
6
- metadata.gz: 133f45dda1e6cf016f88811a8d67813743c58e4b4b2873c2aac7c2fedc562576f47be47faeab67860088081847135e709750559d377c65cae9468154fa663ecd
7
- data.tar.gz: 3b954192277be8bb214c55a953d6fac6475f395295b4eb0bea3b152727cd36773d002cb3af36e5d649d9d49f8e6fe7ca9dd209b14f2a81c2e0aed90a5e964285
6
+ metadata.gz: 3b6b9812417f37be3717ce38c74dd04c4e13ba0ddda2278aa7684aeca11f3a0fb234a1bcb3b3359c52c98ad8cdbf1e432473b3f2bdb71a0edff1b0cdbcb33400
7
+ data.tar.gz: dd81b2cc7552dfe36f6d2f9ca1b69b8197055439f6e728615695493741481816674038169dd33d22053a6773424becb84ebe6cfa36a63a3cd2a8c722a031597c
data/README.md CHANGED
@@ -76,8 +76,6 @@ Attributable adds an `inspect` method to your class which display attribute valu
76
76
 
77
77
  Attributable provides the `initialize_attributes` method which can be used if you need to specify your own `initialize` method. For example:
78
78
 
79
- require "attributable"
80
-
81
79
  class UserWithDerivedAttribute
82
80
  extend Attributable
83
81
  attributes :forename, :surname
@@ -126,6 +124,22 @@ Specialising classes can override the defaults set in specialised classes.
126
124
  Ronson.new(forename: "Jon").inspect # <Ronson forename="Jon", surname="Ronson">
127
125
  Ronson.new(forename: "Mark").inspect # <Ronson forename="Mark", surname="Ronson">
128
126
 
127
+ ### Automatic specialisation for superclasses
128
+
129
+ `specialise` is automatically called when subclassing a type that already mixes-in Attributable. In other words, the following code:
130
+
131
+ class AuthorWithDerivedAttribute < UserWithDerivedAttribute
132
+ attributes blogs: []
133
+ end
134
+
135
+ is equivalent to the slightly more verbose:
136
+
137
+ class AuthorWithDerivedAttribute < UserWithDerivedAttribute
138
+ specialises UserWithDerivedAttribute
139
+ attributes blogs: []
140
+ end
141
+
142
+
129
143
  ## Installation
130
144
 
131
145
  Add this line to your application's Gemfile:
@@ -1,3 +1,3 @@
1
1
  module Attributable
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/attributable.rb CHANGED
@@ -16,6 +16,7 @@ module Attributable
16
16
  @predefined_attributes ||= {}
17
17
  @predefined_attributes = super_attributes.merge(@predefined_attributes)
18
18
  add_instance_methods(@predefined_attributes)
19
+ @predefined_attributes
19
20
  end
20
21
 
21
22
  private
@@ -42,6 +43,9 @@ module Attributable
42
43
  end
43
44
 
44
45
  define_method "initialize_attributes" do |attributes = {}|
46
+ if self.class.superclass.kind_of? Attributable
47
+ predefined_attributes = self.class.specialises(self.class.superclass)
48
+ end
45
49
  @attributes = predefined_attributes.merge(attributes)
46
50
  end
47
51
  end
@@ -79,4 +79,33 @@ describe Attributable do
79
79
  )
80
80
  end
81
81
  end
82
+
83
+ describe "automatic specialisation" do
84
+ it "should automatically specialise if superclass is an instance of Attributable" do
85
+ class SuperUser6 < User
86
+ attributes :password, active: true
87
+ end
88
+
89
+ s = SuperUser6.new(id: 1, forename: "Bob", password: "secret", active: false)
90
+
91
+ expect(s.id).to eq(1)
92
+ expect(s.forename).to eq("Bob")
93
+ expect(s.surname).to eq("Bloggs")
94
+ expect(s.password).to eq("secret")
95
+ expect(s.active).to be_false
96
+ end
97
+
98
+ it "shouldn't automatically specialise unless superclass is an instance of Attributable" do
99
+ class PORO
100
+ attr_accessor :name
101
+ end
102
+
103
+ class SuperUser7 < PORO
104
+ extend Attributable
105
+ attributes :forename, :surname
106
+ end
107
+
108
+ expect { SuperUser7.new }.to_not raise_error
109
+ end
110
+ end
82
111
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attributable
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
  - Louis Rose