Sha256: e764b3f17f9fff7d1f240ced00a69f032e9a5a94a85f3a4e8c8fef763b212e8e

Contents?: true

Size: 1.79 KB

Versions: 5

Compression:

Stored size: 1.79 KB

Contents

require "declarative/schema"

module Representable
  autoload :Decorator, "representation/decorator"
  autoload :Definition, "representation/definition"

  module Declarative
    def representation_wrap=(name)
      heritage.record(:representation_wrap=, name)

      definitions.wrap = name
    end

    def collection(name, options={}, &block)
      property(name, options.merge(collection: true), &block)
    end

    def hash(name=nil, options={}, &block)
      return super() unless name  # allow Object.hash.

      options[:hash] = true
      property(name, options, &block)
    end

    # Allows you to nest a block of properties in a separate section while still mapping
    # them to the original object.
    def nested(name, options={}, &block)
      options = options.merge(
        getter:   ->(_opts) { self },
        setter:   ->(opts) { },
        instance: ->(_opts) { self },
      )

      if block
        options[:_nested_builder] = Decorator.nested_builder
        options[:_base]           = Decorator.default_nested_class
      end

      property(name, options, &block)
    end


    include ::Declarative::Schema::DSL # ::property
    include ::Declarative::Schema::Feature
    include ::Declarative::Heritage::DSL

    def default_nested_class
      Module.new # FIXME: make that unnecessary in Declarative
    end


    NestedBuilder = ->(options) do
      Module.new do
        include Representable # FIXME: do we really need this?
        feature(*options[:_features])
        include(*options[:_base]) # base when :inherit, or in decorator.

        module_eval(&options[:_block])
      end
    end

    def nested_builder
      NestedBuilder
    end

    def definitions
      @definitions ||= Config.new(Representable::Definition)
    end

    alias_method :representable_attrs, :definitions
  end
end

Version data entries

5 entries across 5 versions & 3 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/representable-3.2.0/lib/representable/declarative.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/representable-3.2.0/lib/representable/declarative.rb
representable-3.2.0 lib/representable/declarative.rb
representable-3.1.1 lib/representable/declarative.rb
representable-3.1.0 lib/representable/declarative.rb