Class: HasHelpers::Constant::Definition
- Inherits:
-
Object
- Object
- HasHelpers::Constant::Definition
- Defined in:
- lib/has_helpers/constant.rb
Overview
Container class for a constant definition. Attributes are accessible via reader methods.
Examples
definition = Definition.new(name: "Kansas", abbreviation: "KS") definition.name # => "Kansas" definition.abbreviation # => "KS" definition.attributes # => { name: "Kansas", abbreviation: "KS" }
# Lazy block -- this is useful for creating groups or other constants which
# we do not want to load immediately.
Definition.new(name: "Annuity") { [ADDITIONAL, INITIAL, RENEWAL, TRIAL, BONUS] }
Constant Summary collapse
- ERROR_MSG_MISSING_KEY =
"At least one option from :name and :key is required"
Instance Attribute Summary collapse
-
#attrs(force_eval: false) ⇒ Object
Returns a hash of attributes which define the constant.
-
#block ⇒ Object
Returns the value of attribute block.
-
#group_foreign_key ⇒ Object
Returns the value of attribute group_foreign_key.
-
#include_parent_group_id ⇒ Object
Returns the value of attribute include_parent_group_id.
-
#key ⇒ Object
Note that this may hide the original key value specified in the attributes.
-
#persisted_parent ⇒ Object
Returns the value of attribute persisted_parent.
Instance Method Summary collapse
- #evaluate ⇒ Object
-
#initialize(key: nil, name: nil, group_foreign_key: nil, **attrs, &block) ⇒ Definition
constructor
Options
__attrs__: Data attributes. - #lazy? ⇒ Boolean
- #load(klass = nil) ⇒ Object
- #meta ⇒ Object
- #name ⇒ Object
- #persisted_parent? ⇒ Boolean
- #seed!(klass) ⇒ Object
- #to_constant ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(key: nil, name: nil, group_foreign_key: nil, **attrs, &block) ⇒ Definition
Options
__attrs__: Data attributes. These will be used to look up the constant.
137 138 139 140 141 142 143 144 145 |
# File 'lib/has_helpers/constant.rb', line 137 def initialize(key: nil, name: nil, group_foreign_key: nil, **attrs, &block) raise ArgumentError, ERROR_MSG_MISSING_KEY unless name || key self.attrs = attrs self.key = key self.group_foreign_key = group_foreign_key self.persisted_parent = !!group_foreign_key attrs[:name] = name if name self.block = block end |
Instance Attribute Details
#attrs(force_eval: false) ⇒ Object
Returns a hash of attributes which define the constant. Some attributes
may be "lazy", meaning they are wrapped inside a Proc. To get the attributes
with their final evaluated values pass use the force_eval option, e.g.
attr(force_eval: true)
151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/has_helpers/constant.rb', line 151 def attrs(force_eval: false) if force_eval attrs.each do |k, v| # Evaluate each lazy attribute by calling its proc and storing the result if v.respond_to?(:call) attrs[k] = v.call end end attrs else @attrs ||= {} end end |
#block ⇒ Object
Returns the value of attribute block.
133 134 135 |
# File 'lib/has_helpers/constant.rb', line 133 def block @block end |
#group_foreign_key ⇒ Object
Returns the value of attribute group_foreign_key.
133 134 135 |
# File 'lib/has_helpers/constant.rb', line 133 def group_foreign_key @group_foreign_key end |
#include_parent_group_id ⇒ Object
Returns the value of attribute include_parent_group_id.
133 134 135 |
# File 'lib/has_helpers/constant.rb', line 133 def include_parent_group_id @include_parent_group_id end |
#key ⇒ Object
Note that this may hide the original key value specified in the attributes.
165 166 167 |
# File 'lib/has_helpers/constant.rb', line 165 def key # Note that this may hide the original key value specified in the attributes. (@key || name.to_s.constant_case).to_sym # Return a symbol for easy comparison with the argument to const_missing, which is a symbol end |
#persisted_parent ⇒ Object
Returns the value of attribute persisted_parent.
133 134 135 |
# File 'lib/has_helpers/constant.rb', line 133 def persisted_parent @persisted_parent end |
Instance Method Details
#evaluate ⇒ Object
177 178 179 |
# File 'lib/has_helpers/constant.rb', line 177 def evaluate @block.call end |
#lazy? ⇒ Boolean
173 174 175 |
# File 'lib/has_helpers/constant.rb', line 173 def lazy? @block.present? end |
#load(klass = nil) ⇒ Object
193 194 195 |
# File 'lib/has_helpers/constant.rb', line 193 def load(klass = nil) lazy? && !persisted_parent? ? evaluate : load_record(klass) end |
#meta ⇒ Object
210 211 212 |
# File 'lib/has_helpers/constant.rb', line 210 def @meta ||= OpenStruct.new end |
#name ⇒ Object
169 170 171 |
# File 'lib/has_helpers/constant.rb', line 169 def name @attrs[:name] end |
#persisted_parent? ⇒ Boolean
181 182 183 |
# File 'lib/has_helpers/constant.rb', line 181 def persisted_parent? @persisted_parent end |
#seed!(klass) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/has_helpers/constant.rb', line 197 def seed!(klass) find_by_attrs = attrs(force_eval: true) find_by_attrs[:id] = default_id(klass) if klass.simple find_by_attrs[:option_type] = default_option_type(klass) if klass.has_attribute? :option_type # Leaving out the group_foreign_key attr from the query for the cases where we're seeding after modifying it. # Including it would cause those queries to fail and it would try to initialize a new constant without it being necessary. returned = klass.where(find_by_attrs.with_indifferent_access.except(self.group_foreign_key)).first_or_initialize.tap do |constant| constant.assign_attributes(find_by_attrs.with_indifferent_access.slice(self.group_foreign_key)) if find_by_attrs.with_indifferent_access.key?(self.group_foreign_key) end returned.save! returned end |
#to_constant ⇒ Object
189 190 191 |
# File 'lib/has_helpers/constant.rb', line 189 def to_constant self end |
#to_s ⇒ Object
185 186 187 |
# File 'lib/has_helpers/constant.rb', line 185 def to_s name end |