Class: Lean::Attributes::Attribute Private
- Inherits:
-
Object
- Object
- Lean::Attributes::Attribute
- Defined in:
- lib/lean-attributes/attributes/attribute.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Represents an attribute defined by ClassMethods#attribute
Instance Attribute Summary collapse
-
#name ⇒ Symbol
readonly
private
Name of the Attribute.
Instance Method Summary collapse
-
#coercion_method ⇒ String
private
Generates a method definition as a String with the name ‘coerce_<attribute>`.
-
#coercion_method_name ⇒ String
private
Generates a method with a name ‘coerce_<attribute>`.
-
#default ⇒ Object
private
If the configured default is a Symbol but the intended type for this attribute is anything but, interpret the default as a method name to which we will ‘send`.
-
#getter_method ⇒ String
private
Generates a getter method definition if the Attribute has a default, or we just use the straightforward ‘attr_reader :attribute_name`.
-
#getter_method_with_default ⇒ String
private
Generates a getter method definition that lazily sets a default.
-
#initialize(options = {}) ⇒ Attribute
constructor
private
Description of method.
-
#setter_method ⇒ String
private
Generates a setter method definition that coerces values to the configured type if necessary.
Constructor Details
#initialize(options = {}) ⇒ Attribute
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Description of method
25 26 27 28 29 |
# File 'lib/lean-attributes/attributes/attribute.rb', line 25 def initialize( = {}) @default = [:default] @name = [:name].to_sym @type = [:type].to_s.to_sym end |
Instance Attribute Details
#name ⇒ Symbol (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns name of the Attribute.
12 13 14 |
# File 'lib/lean-attributes/attributes/attribute.rb', line 12 def name @name end |
Instance Method Details
#coercion_method ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generates a method definition as a String with the name ‘coerce_<attribute>`.
55 56 57 58 59 60 61 |
# File 'lib/lean-attributes/attributes/attribute.rb', line 55 def coercion_method <<-EOS def coerce_#{name}(value) #{CoercionHelpers.method_body_for_type(@type)} unless value.nil? end EOS end |
#coercion_method_name ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generates a method with a name ‘coerce_<attribute>`.
68 69 70 |
# File 'lib/lean-attributes/attributes/attribute.rb', line 68 def coercion_method_name "coerce_#{name}" end |
#default ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
If the configured default is a Symbol but the intended type for this attribute is anything but, interpret the default as a method name to which we will ‘send`. Otherwise, just use the configured default as a String.
82 83 84 85 86 87 88 |
# File 'lib/lean-attributes/attributes/attribute.rb', line 82 def default if @default.is_a?(Symbol) && @type != :Symbol return "send(:#{@default})" end @default.inspect end |
#getter_method ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generates a getter method definition if the Attribute has a default, or we just use the straightforward ‘attr_reader :attribute_name`.
112 113 114 115 116 |
# File 'lib/lean-attributes/attributes/attribute.rb', line 112 def getter_method return getter_method_with_default unless @default.nil? "attr_reader :#{@name}" end |
#getter_method_with_default ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generates a getter method definition that lazily sets a default.
141 142 143 144 145 146 147 |
# File 'lib/lean-attributes/attributes/attribute.rb', line 141 def getter_method_with_default <<-EOS def #{name} @#{name} ||= #{default} end EOS end |
#setter_method ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generates a setter method definition that coerces values to the configured type if necessary.
166 167 168 169 170 171 172 173 |
# File 'lib/lean-attributes/attributes/attribute.rb', line 166 def setter_method <<-EOS def #{name}=(value) value = #{coercion_method_name}(value) @#{name} = value end EOS end |