README.md in u-attributes-0.3.0 vs README.md in u-attributes-0.4.0
- old
+ new
@@ -56,10 +56,32 @@
# By design, the attributes expose only reader methods (getters).
# If you try to call a setter, you will see a NoMethodError.
#
# person.name = 'Rodrigo'
# NoMethodError (undefined method `name=' for #<Person:0x0000... @name="John Doe", @age=21>)
+
+####################
+# self.attributes= #
+####################
+
+# This protected method is added to make easier the assignment in a constructor.
+
+class Person
+ include Micro::Attributes
+
+ attribute :name
+ attribute :age
+
+ def initialize(options)
+ self.attributes = options
+ end
+end
+
+person = Person.new('name' => 'John', age: 20)
+
+puts person.name # John
+puts person.age # 20
```
### How to define multiple attributes?
```ruby
@@ -69,12 +91,12 @@
class Person
include Micro::Attributes
attributes :name, :age
- def initialize(name, age)
- @name, @age = name, age
+ def initialize(options)
+ self.attributes = options
end
end
person = Person.new('Serradura', 32)
@@ -123,9 +145,10 @@
# ArgumentError (argument must be a Hash)
#########################################################
# Inheritance will preserve the parent class attributes #
#########################################################
+
class Subclass < Person
attribute :foo
end
instance = Subclass.new({})