Module: ZohoHub::WithAttributes
- Included in:
- BaseRecord, Settings::Field, Settings::Module
- Defined in:
- lib/zoho_hub/with_attributes.rb
Overview
Allows adding attributes to a class, as attr_accessors
that can then be listed from the class or an instance of that class.
Example
class User
include ZohoHub::WithAttributes
attributes :first_name, :last_name, :email
end
User.attributes # => [:first_name, :last_name, :email]
User.new.attributes # => [:first_name, :last_name, :email]
user = User.new
user.first_name = 'Ricardo'
user.last_name = 'Otero'
user.first_name # => "Ricardo"
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#assign_attributes(new_attributes) ⇒ Object
This method and the correponding private methods are inspired from Rails ActiveModel github.com/rails/rails/blob/master/activemodel/lib/active_model/attribute_assignment.rb.
-
#attributes ⇒ Object
Returns the list of attributes defined for the instance class.
Class Method Details
.included(base) ⇒ Object
23 24 25 |
# File 'lib/zoho_hub/with_attributes.rb', line 23 def self.included(base) base.extend ClassMethods end |
Instance Method Details
#assign_attributes(new_attributes) ⇒ Object
This method and the correponding private methods are inspired from Rails ActiveModel github.com/rails/rails/blob/master/activemodel/lib/active_model/attribute_assignment.rb
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/zoho_hub/with_attributes.rb', line 66 def assign_attributes(new_attributes) unless new_attributes.is_a?(Hash) raise ArgumentError, 'When assigning attributes, you must pass a hash as an argument' end return if new_attributes.empty? attributes = new_attributes.transform_keys(&:to_s) attributes.each do |k, v| assign_attribute(k, v) end end |
#attributes ⇒ Object
Returns the list of attributes defined for the instance class.
60 61 62 |
# File 'lib/zoho_hub/with_attributes.rb', line 60 def attributes self.class.attributes end |