Sha256: 3b7a1808e26c828a55f02f567c9d6869b762c2148f060b952b10999c175b8d79

Contents?: true

Size: 1.29 KB

Versions: 14

Compression:

Stored size: 1.29 KB

Contents

require "disposable/twin/struct"

class Disposable::Twin
  module Property
    # trailblazer.to/gems/disposable/api.html#hash
    module Hash
      def self.included(includer)
        # hash: true top-level properties need :default support.
        includer.feature Default

        # Recursively include Struct in :hash and nested properties.
        # defaults is applied to all ::property calls.
        includer.defaults do |name, options|
          if options[:field] == :hash
            hash_options
          else
            {}
          end
        end
      end

    private
      # Note that :_features `include`s modules in this order, first to last.
      def self.hash_options
        { _features: [NestedDefaults, Property::Struct, Hash::Sync], default: ->(*) { ::Hash.new } }
      end

      # NestedDefaults for properties nested in the top :hash column.
      module NestedDefaults
        def self.included(includer)
          includer.defaults do |name, options|
            if options[:_nested_builder] # DISCUSS: any other way to figure out we're nested?
              Hash.hash_options
            else
              { }
            end
          end
        end
      end

      module Sync
        def sync!(options={})
          @model.merge(super)
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
disposable-0.6.3 lib/disposable/twin/property/hash.rb
disposable-0.6.2 lib/disposable/twin/property/hash.rb
disposable-0.6.1 lib/disposable/twin/property/hash.rb
disposable-0.6.0 lib/disposable/twin/property/hash.rb
disposable-0.5.0 lib/disposable/twin/property/hash.rb
disposable-0.4.7 lib/disposable/twin/property/hash.rb
disposable-0.4.6 lib/disposable/twin/property/hash.rb
disposable-0.4.5 lib/disposable/twin/property/hash.rb
disposable-0.4.4 lib/disposable/twin/property/hash.rb
disposable-0.4.3 lib/disposable/twin/property/hash.rb
disposable-0.4.2 lib/disposable/twin/property/hash.rb
disposable-0.4.1 lib/disposable/twin/property/hash.rb
disposable-0.4.0 lib/disposable/twin/property/hash.rb
disposable-0.3.2 lib/disposable/twin/property/hash.rb