Module: LazyLoadAttributes::ClassMethods
- Defined in:
- lib/lazy_load_attributes.rb
Instance Method Summary collapse
- #all_lazy_attributes ⇒ Object
- #lazy_attr_accessor(attribute, &initializer) ⇒ Object
-
#lazy_attr_reader(attribute, &initializer) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #lazy_attributes ⇒ Object
Instance Method Details
#all_lazy_attributes ⇒ Object
20 21 22 23 24 |
# File 'lib/lazy_load_attributes.rb', line 20 def all_lazy_attributes ancestors.select { |ancestor| ancestor.singleton_class.include?(::LazyLoadAttributes) } .map(&:lazy_attributes) .reduce(&:|) end |
#lazy_attr_accessor(attribute, &initializer) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/lazy_load_attributes.rb', line 26 def lazy_attr_accessor(attribute, &initializer) accessor = [ *lazy_attr_reader(attribute, &initializer), *attr_writer(attribute) ] return nil if Gem.ruby_version < RUBY_3 accessor end |
#lazy_attr_reader(attribute, &initializer) ⇒ Object
rubocop:disable Metrics/MethodLength
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/lazy_load_attributes.rb', line 37 def lazy_attr_reader(attribute, &initializer) # rubocop:disable Metrics/MethodLength raise NameError, "bad attribute name '#{attribute}' (use a-z, 0-9, _)" unless attribute.match? ATTR_NAME_REGEX raise ArgumentError, "no initializer block given in lazy-loaded attribute definition" if initializer.nil? lazy_attributes.add(attribute) define_method(attribute) do instance_variable = :"@#{attribute}" if instance_variable_defined?(instance_variable) instance_variable_get(instance_variable) else instance_variable_set(instance_variable, instance_eval(&initializer)) end end return nil if Gem.ruby_version < RUBY_3 [attribute] end |
#lazy_attributes ⇒ Object
16 17 18 |
# File 'lib/lazy_load_attributes.rb', line 16 def lazy_attributes @lazy_attributes ||= Set.new end |