README.md in sinclair-1.1.0 vs README.md in sinclair-1.1.1
- old
+ new
@@ -31,47 +31,51 @@
---------------
The concern builder can actully be used in two ways, as an stand alone object capable of
adding methods to your class or by extending it for more complex logics
- Stand Alone usage:
-```ruby
- class Clazz
- end
- builder = Sinclair.new(Clazz)
+ ```ruby
- builder.add_method(:twenty, '10 + 10')
- builder.add_method(:eighty) { 4 * twenty }
- builder.build
+ class Clazz
+ end
- instance = Clazz.new
+ builder = Sinclair.new(Clazz)
- puts "Twenty => #{instance.twenty}"
- puts "Eighty => #{instance.eighty}"
-```
+ builder.add_method(:twenty, '10 + 10')
+ builder.add_method(:eighty) { 4 * twenty }
+ builder.build
-```string
- Twenty => 20
- Eighty => 80
-```
+ instance = Clazz.new
+ puts "Twenty => #{instance.twenty}"
+ puts "Eighty => #{instance.eighty}"
+ ```
+
+ ```string
+
+ Twenty => 20
+ Eighty => 80
+ ```
+
- Extending the builder
```ruby
+
class ValidationBuilder < Sinclair
delegate :expected, to: :options_object
- def initialize(clazz, options={})
+ def initialize(klass, options={})
super
end
def add_validation(field)
add_method("#{field}_valid?", "#{field}.is_a?#{expected}")
end
def add_accessors(fields)
- clazz.send(:attr_accessor, *fields)
+ klass.send(:attr_accessor, *fields)
end
end
module MyConcern
extend ActiveSupport::Concern
@@ -117,109 +121,102 @@
instance = MyClass.new
```
the instance will respond to the methods
-```name``` ```name=``` ```name_valid?```
-```surname``` ```surname=``` ```surname_valid?```
-```age``` ```age=``` ```age_valid?```
-```legs``` ```legs=``` ```legs_valid?```
-```valid?```
+ ```name``` ```name=``` ```name_valid?```
+ ```surname``` ```surname=``` ```surname_valid?```
+ ```age``` ```age=``` ```age_valid?```
+ ```legs``` ```legs=``` ```legs_valid?```
+ ```valid?```
```ruby
+
valid_object = MyClass.new(
name: :name,
surname: 'surname',
age: 20,
legs: 2
)
- valid_object.valid?
+ valid_object.valid? # returns true
```
- returns
-
- ```
- true
- ```
-
```ruby
+
invalid_object = MyClass.new(
name: 'name',
surname: 'surname',
age: 20,
legs: 2
)
- invalid_object.valid?
+ invalid_object.valid? # returns false
```
- returns
-
- ```
- false
- ```
-
RSspec matcher
---------------
You can use the provided matcher to check that your builder is adding a method correctly
-```ruby
-class DefaultValue
- delegate :build, to: :builder
- attr_reader :klass, :method, :value
+ ```ruby
- def initialize(klass, method, value)
- @klass = klass
- @method = method
- @value = value
- end
+ class DefaultValue
+ delegate :build, to: :builder
+ attr_reader :klass, :method, :value
- private
+ def initialize(klass, method, value)
+ @klass = klass
+ @method = method
+ @value = value
+ end
- def builder
- @builder ||= Sinclair.new(klass).tap do |b|
- b.add_method(method) { value }
+ private
+
+ def builder
+ @builder ||= Sinclair.new(klass).tap do |b|
+ b.add_method(method) { value }
+ end
end
end
-end
-RSpec.configure do |config|
- config.include Sinclair::Matchers
-end
+ RSpec.configure do |config|
+ config.include Sinclair::Matchers
+ end
-RSpec.describe DefaultValue do
- let(:klass) { Class.new }
- let(:method) { :the_method }
- let(:value) { Random.rand(100) }
- let(:builder) { described_class.new(klass, method, value) }
- let(:instance) { klass.new }
+ RSpec.describe DefaultValue do
+ let(:klass) { Class.new }
+ let(:method) { :the_method }
+ let(:value) { Random.rand(100) }
+ let(:builder) { described_class.new(klass, method, value) }
+ let(:instance) { klass.new }
- context 'when the builder runs' do
- it do
- expect do
- described_class.new(klass, method, value).build
- end.to add_method(method).to(instance)
+ context 'when the builder runs' do
+ it do
+ expect do
+ described_class.new(klass, method, value).build
+ end.to add_method(method).to(instance)
+ end
end
- end
- context 'when the builder runs' do
- it do
- expect do
- described_class.new(klass, method, value).build
- end.to add_method(method).to(klass)
+ context 'when the builder runs' do
+ it do
+ expect do
+ described_class.new(klass, method, value).build
+ end.to add_method(method).to(klass)
+ end
end
end
-end
-```
+ ```
-```bash
-> bundle exec rspec
-```
+ ```bash
-```string
-DefaultValue
- when the builder runs
- should add method 'the_method' to #<Class:0x0000000146c160> instances
- when the builder runs
- should add method 'the_method' to #<Class:0x0000000143a1b0> instances
+ > bundle exec rspec
+ ```
-```
+ ```string
+
+ DefaultValue
+ when the builder runs
+ should add method 'the_method' to #<Class:0x0000000146c160> instances
+ when the builder runs
+ should add method 'the_method' to #<Class:0x0000000143a1b0> instances
+
+ ```