Sha256: 818100ac0a8c68cd0301b6c4f51ac08445c1e7f6d3e990e504352710c8342a0f

Contents?: true

Size: 1.76 KB

Versions: 99

Compression:

Stored size: 1.76 KB

Contents

module SmartProperties
  class PropertyCollection
    include Enumerable

    attr_reader :parent

    def self.for(scope)
      parent = scope.ancestors[1..-1].find do |ancestor|
        ancestor.ancestors.include?(SmartProperties) &&
          ancestor != scope &&
          ancestor != SmartProperties
      end

      if parent.nil?
        new
      else
        parent.properties.register(collection = new)
        collection
      end
    end

    def initialize
      @collection = {}
      @collection_with_parent_collection = {}
      @children = []
    end

    def []=(name, value)
      name = name.to_s
      collection[name] = value
      collection_with_parent_collection[name] = value
      notify_children
      value
    end

    def [](name)
      collection_with_parent_collection[name.to_s]
    end

    def key?(name)
      collection_with_parent_collection.key?(name.to_s)
    end

    def keys
      collection_with_parent_collection.keys.map(&:to_sym)
    end

    def values
      collection_with_parent_collection.values
    end

    def each(&block)
      return to_enum(:each) if block.nil?
      collection_with_parent_collection.each { |name, value| block.call([name.to_sym, value]) }
    end

    def to_hash
      Hash[each.to_a]
    end

    def register(child)
      children.push(child)
      child.refresh(collection_with_parent_collection)
      nil
    end

    protected

    attr_accessor :children
    attr_accessor :collection
    attr_accessor :collection_with_parent_collection

    def notify_children
      @children.each { |child| child.refresh(collection_with_parent_collection) }
    end

    def refresh(parent_collection)
      @collection_with_parent_collection = parent_collection.merge(collection)
      notify_children
      nil
    end
  end
end

Version data entries

99 entries across 99 versions & 2 rubygems

Version Path
shopify-cli-2.19.0 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb
shopify-cli-2.18.1 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb
shopify-cli-2.18.0 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb
shopify-cli-2.17.0 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb
shopify-cli-2.16.1 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb
shopify-cli-2.16.0 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb
shopify-cli-2.15.6 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb
shopify-cli-2.15.5 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb
shopify-cli-2.15.4 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb
shopify-cli-2.15.3 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb
shopify-cli-2.15.2 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb
shopify-cli-2.15.1 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb
shopify-cli-2.15.0 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb
shopify-cli-2.14.0 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb
shopify-cli-2.13.0 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb
shopify-cli-2.12.0 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb
shopify-cli-2.11.2 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb
shopify-cli-2.11.1 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb
shopify-cli-2.11.0 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb
shopify-cli-2.10.2 vendor/deps/smart_properties/lib/smart_properties/property_collection.rb