module Hashie::Extensions::Dash::Lazy

Loads declared properties on demand from an initializer block. Include after other Dash extensions. Hash enumeration only sees loaded entries; to_h loads every declared property.

@example Load and memoize a property

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::Lazy
  property :name
end
calls = []
dash = klass.new { |key| calls << key; 'Ada' }
calls # => []
dash.name # => 'Ada'
dash.name # => 'Ada'
calls # => [:name]

Public Class Methods

new(attributes = {}, &loader) click to toggle source

@example Hash initialization without a loader retains normal behavior

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::Lazy
  property :name, required: true
end
klass.new(name: 'Ada').name # => 'Ada'
Calls superclass method
# File lib/hashie/extensions/dash/lazy.rb, line 28
def initialize(attributes = {}, &loader)
  @lazy_loader = loader
  @lazy_loading = []
  super(attributes, &nil)
end

Public Instance Methods

[](property, &block) click to toggle source

@example Read a property without loading its neighbors

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::Lazy
  property :name
  property :age
end
dash = klass.new { |key| { name: 'Ada', age: 37 }[key] }
dash[:name] # => 'Ada'
dash.keys # => [:name]
Calls superclass method
# File lib/hashie/extensions/dash/lazy.rb, line 43
def [](property, &block)
  return super unless @lazy_loader

  assert_property_exists!(property)
  canonical_property = lazy_property(property)
  load_property(canonical_property) unless key?(property)
  value = super
  assert_property_required!(canonical_property, value)
  value
end
dig(property, *path) click to toggle source

@example Load the first property before digging into its value

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::Lazy
  property :user
end
klass.new { |_key| { name: 'Ada' } }.dig(:user, :name) # => 'Ada'
Calls superclass method
# File lib/hashie/extensions/dash/lazy.rb, line 71
def dig(property, *path)
  self[property] if @lazy_loader && self.class.property?(property)
  super
end
fetch(property, *args, &block) click to toggle source

@example Fetch a declared value through its loader

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::Lazy
  property :name
end
klass.new { |_key| 'Ada' }.fetch(:name) # => 'Ada'
Calls superclass method
# File lib/hashie/extensions/dash/lazy.rb, line 60
def fetch(property, *args, &block)
  self[property] if @lazy_loader && self.class.property?(property)
  super
end
freeze() click to toggle source

@example Freezing resolves pending values before preventing mutation

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::Lazy
  property :name
end
klass.new { |_key| 'Ada' }.freeze.name # => 'Ada'
Calls superclass method
# File lib/hashie/extensions/dash/lazy.rb, line 101
def freeze
  to_h if @lazy_loader && !frozen?
  super
end
to_h() click to toggle source

@example Convert all declared properties to a plain hash

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::Lazy
  property :name
end
klass.new { |_key| 'Ada' }.to_h # => { name: 'Ada' }
Calls superclass method
# File lib/hashie/extensions/dash/lazy.rb, line 82
def to_h
  self.class.properties.each { |property| self[property] } if @lazy_loader
  super
end
Also aliased as: to_hash
to_hash()

@example Convert all declared properties for hash coercion

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::Lazy
  property :name
end
klass.new { |_key| 'Ada' }.to_hash # => { name: 'Ada' }
Alias for: to_h

Private Instance Methods

assert_required_attributes_set!() click to toggle source
Calls superclass method
# File lib/hashie/extensions/dash/lazy.rb, line 114
def assert_required_attributes_set!
  super unless @lazy_loader
end
initialize_attributes(attributes) click to toggle source
Calls superclass method
# File lib/hashie/extensions/dash/lazy.rb, line 108
def initialize_attributes(attributes)
  return super unless @lazy_loader

  @lazy_attributes = attributes || {}
end
initialize_copy(other) click to toggle source
Calls superclass method
# File lib/hashie/extensions/dash/lazy.rb, line 118
def initialize_copy(other)
  super
  @lazy_loading = []
end
lazy_attribute_key(property) click to toggle source
# File lib/hashie/extensions/dash/lazy.rb, line 129
def lazy_attribute_key(property)
  return property if @lazy_attributes.key?(property)
  return unless respond_to?(:convert_key)

  converted = convert_key(property)
  converted if @lazy_attributes.key?(converted)
end
lazy_property(property) click to toggle source
# File lib/hashie/extensions/dash/lazy.rb, line 123
def lazy_property(property)
  return property if self.class.properties.include?(property)

  self.class.properties.find { |key| key.to_s == property.to_s } || property
end
load_property(property) click to toggle source
# File lib/hashie/extensions/dash/lazy.rb, line 137
def load_property(property)
  raise ArgumentError, "circular lazy loading for property `#{property}`" if @lazy_loading.include?(property)

  @lazy_loading << property
  begin
    source = if self.class.respond_to?(:inverse_translations)
               self.class.inverse_translations.fetch(property, property)
             else
               property
             end
    attribute = lazy_attribute_key(property)
    if attribute
      source = property
      value = @lazy_attributes[attribute]
    else
      attribute = lazy_attribute_key(source)
      value = attribute ? @lazy_attributes[attribute] : @lazy_loader.call(source)
    end
    if value.nil?
      value = self.class.defaults[property].dup
      value = value.arity == 1 ? value.call(self) : value.call if value.is_a?(Proc)
    elsif source != property
      translator = self.class.translations_hash[source][property]
      value = translator.call(value) if translator.respond_to?(:call)
    end
    self[property] = value
  ensure
    @lazy_loading.pop
  end
end