Skip to content
This repository has been archived by the owner on Sep 18, 2019. It is now read-only.

Commit

Permalink
Added documentation about default values
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-alexandrov committed May 27, 2013
1 parent 659ae2f commit 3fd4a34
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
49 changes: 35 additions & 14 deletions README.md
Expand Up @@ -78,31 +78,52 @@ user.api_attributes # => {...}

### Default values

You can easily setup default values for your attributes by passing `:default` option to `attr` method.
Attrio supports all the ways to setup default values that Virtus has.

```ruby
class User
class Page
include Attrio

define_attributes do
attr :name, String, :default => 'John Doe'
attr :age, Integer, :default => 18
attr :birthday, DateTime
attr :title, String

# default from a singleton value (integer in this case)
attr :views, Integer, :default => 0

# default from a singleton value (boolean in this case)
attr :published, Boolean, :default => false

# default from a callable object (proc in this case)
attr :slug, String, :default => lambda { |page, attribute| page.title.present? ? page.title.downcase.gsub(' ', '-') : nil }

# default from a method name as symbol
attr :editor_title, String, :default => :default_editor_title
end

def initialize(attributes = {})
self.attributes = attributes
end

def attributes=(attributes = {})
attributes.each do |attr,value|
self.send("#{attr}=", value) if self.respond_to?("#{attr}=")
end
end

def default_editor_title
if self.published?
title
else
title.present? ? "UNPUBLISHED: #{title}" : "UNPUBLISHED"
end
end
end
```

```ruby
user = User.new
user.age
# => 18
user.name
# => 'John Doe'
```

### Methods visibility

Don't want your accessors to be public? This behaviour can be overridden easily.
Don't want your accessors to be public? Visibility can be overridden easily.

```ruby
class User
Expand Down
2 changes: 1 addition & 1 deletion lib/attrio/attribute.rb
Expand Up @@ -30,7 +30,7 @@ def instance_variable_name

def default_value
if !defined?(@default_value)
@default_value = Attrio::DefaultValue.new(self.klass, self.name, (self.options[:default] || self.options[:default_value]))
@default_value = Attrio::DefaultValue.new(self.klass, self.name, self.options[:default])
end
@default_value
end
Expand Down
4 changes: 2 additions & 2 deletions lib/attrio/reset.rb
Expand Up @@ -14,12 +14,12 @@ def define_attrio_reset(as)
end

define_method "reset_#{as.to_s}_defaults" do |attributes = []|
self.send(as.to_s, attributes).values.select(&:default_value).each { |attribute| self.send(attribute.writer_method_name, nil) }
self.send(as.to_s, attributes).values.select{ |attribute| !attribute.default_value.nil? }.each { |attribute| self.send(attribute.writer_method_name, nil) }
self.send("set_#{as.to_s}_defaults", attributes)
end

define_method "set_#{as.to_s}_defaults" do |attributes = []|
self.send(as.to_s, attributes).values.select(&:default_value).each do |attribute|
self.send(as.to_s, attributes).values.select{ |attribute| !attribute.default_value.nil? }.each do |attribute|
next if self.send(attribute.reader_method_name).present?

default_value = attribute.default_value.is_a?(Attrio::DefaultValue::Base) ? attribute.default_value.call(self) : attribute.default_value
Expand Down

0 comments on commit 3fd4a34

Please sign in to comment.