Sha256: 8bd96f4f054f399e4556b4ef94275a403ab3ab58b641adccff0c44e2f63bd9f9

Contents?: true

Size: 1.07 KB

Versions: 4

Compression:

Stored size: 1.07 KB

Contents

module Spigot
  module Map
    class Resource
      attr_reader :definitions

      def initialize(name, &block)
        @name = name.to_s.underscore.to_sym
        @definitions = []
        @options = Spigot::Map::Option.new
        instance_eval(&block) if block_given?
      end

      def append(definition)
        @definitions << definition
      end

      def associations
        definitions.map do |definition|
          value = definition.instance_variable_get(:@value)
          definition if value.is_a?(Class)
        end.compact
      end

      def options(&block)
        @options = Spigot::Map::Option.new(&block)
      end

      def to_hash
        resource = {}
        @definitions.each { |rule| resource.merge!(rule.to_hash) }
        { @name => resource }
      end

      # Spigot::Map::Resource.new(:user){ username :login }
      # Spigot::Map::Resource.new(:user){ username = :login }
      def method_missing(name, *args, &block)
        name = name.to_s.gsub('=', '').to_sym
        Spigot::Map::Definition.define(self, name, *args, &block)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spigot-0.3.3 lib/spigot/map/resource.rb
spigot-0.3.2 lib/spigot/map/resource.rb
spigot-0.3.1 lib/spigot/map/resource.rb
spigot-0.3.0 lib/spigot/map/resource.rb