README.md in u-attributes-0.13.0 vs README.md in u-attributes-0.14.0

- old
+ new

@@ -1,9 +1,31 @@ -# μ-attributes (Micro::Attributes) [![Build Status](https://travis-ci.com/serradura/u-attributes.svg?branch=master)](https://travis-ci.com/serradura/u-attributes) +[![Build Status](https://travis-ci.com/serradura/u-attributes.svg?branch=master)](https://travis-ci.com/serradura/u-attributes) [![Maintainability](https://api.codeclimate.com/v1/badges/b562e6b877a9edf4dbf6/maintainability)](https://codeclimate.com/github/serradura/u-attributes/maintainability) +μ-attributes (Micro::Attributes) +================================ + This gem allows defining read-only attributes, that is, your objects will have only getters to access their attributes data. +## Table of contents +- [μ-attributes (Micro::Attributes)](#%CE%BC-attributes-MicroAttributes) + - [Table of contents](#Table-of-contents) + - [Installation](#Installation) + - [Usage](#Usage) + - [How to require?](#How-to-require) + - [How to define attributes?](#How-to-define-attributes) + - [How to define multiple attributes?](#How-to-define-multiple-attributes) + - [How to define attributes with a constructor to assign them?](#How-to-define-attributes-with-a-constructor-to-assign-them) + - [How to query the attributes?](#How-to-query-the-attributes) + - [Built-in extensions](#Built-in-extensions) + - [ActiveModel::Validations extension](#ActiveModelValidations-extension) + - [Diff extension](#Diff-extension) + - [Initialize extension](#Initialize-extension) + - [Development](#Development) + - [Contributing](#Contributing) + - [License](#License) + - [Code of Conduct](#Code-of-Conduct) + ## Installation Add this line to your application's Gemfile: ```ruby @@ -266,21 +288,21 @@ ## Built-in extensions You can use the method `Micro::Attributes.features()` or `Micro::Attributes.with()` to combine and require only the features that better fit your needs. -### Usage +Note: If you desire require only one feature, use the `Micro::Attributes.feature()` method. ```ruby #----------------------------------# # Via Micro::Attributes.features() # #----------------------------------# # Loading specific features class Job - include Micro::Attributes.features(:diff) + include Micro::Attributes.feature(:diff) attribute :id attribute :state, 'sleeping' def initialize(options) @@ -329,12 +351,10 @@ ### ActiveModel::Validations extension If your application uses ActiveModel as a dependency (like a regular Rails app). You will be enabled to use the `actimodel_validations` extension. -#### Usage - ```ruby class Job # include Micro::Attributes.with(:initialize, :activemodel_validations) # include Micro::Attributes.features(:initialize, :activemodel_validations) include Micro::Attributes.to_initialize(activemodel_validations: true) @@ -353,12 +373,10 @@ ### Diff extension Provides a way to track changes in your object attributes. -#### Usage - ```ruby require 'securerandom' class Job # include Micro::Attributes.with(:initialize, :diff) @@ -399,9 +417,56 @@ #----------------# # #differences() # #----------------# p job_changes.differences # {"state"=> {"from" => "sleeping", "to" => "running"}} +``` + +### Initialize extension + +1. Creates a constructor to assign the attributes. +2. Adds methods to build new instances when some data was assigned. + +```ruby +class Job + # include Micro::Attributes.features(:initialize) + # include Micro::Attributes.with(:initialize) + # include Micro::Attributes.feature(:initialize) + include Micro::Attributes.to_initialize + + attributes :id, :state +end + +job = Job.new(id: 1, state: 'sleeping') + +p job.id # 1 +p job.state # "sleeping" + +############################################## +# Assigning new values to get a new instance # +############################################## + +#-------------------# +# #with_attribute() # +#-------------------# + +new_job = job.with_attribute(:state, 'running') + +puts new_job.id # 1 +puts new_job.state # running +puts new_job.equal?(job) # false + +#--------------------# +# #with_attributes() # +#--------------------# +# +# Use it to assign multiple attributes + +other_job = job.with_attributes(id: 2, state: 'killed') + +puts other_job.id # 2 +puts other_job.state # killed +puts other_job.equal?(job) # false ``` ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.