Sha256: cda677907642b3951516ca1f32ce3ad4695fa125c6424a461a735b8e3d98e6c8

Contents?: true

Size: 1.05 KB

Versions: 12

Compression:

Stored size: 1.05 KB

Contents

module EventbriteSDK
  class Resource
    class SchemaDefinition
      def initialize(resource_name)
        @resource_name = resource_name
        @read_only_keys = Set.new
        @attrs = {}
      end

      %i(boolean currency datetime integer string).each do |method|
        define_method(method) do |value, *opts|
          options = opts.first

          @read_only_keys << value if options && options[:read_only]
          @attrs[value] = method
        end
      end

      def writeable?(key)
        whitelisted_attribute?(key) && !read_only?(key)
      end

      def type(key)
        attrs[key]
      end

      def defined_keys
        attrs.keys
      end

      private

      attr_reader :read_only_keys, :resource_name, :attrs

      def read_only?(key)
        read_only_keys.member?(key)
      end

      def whitelisted_attribute?(key)
        if attrs.has_key?(key)
          true
        else
          raise InvalidAttribute.new(
            "attribute `#{key}` not present in #{resource_name}"
          )
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
eventbrite_sdk-3.1.1 lib/eventbrite_sdk/resource/schema_definition.rb
eventbrite_sdk-3.1.0 lib/eventbrite_sdk/resource/schema_definition.rb
eventbrite_sdk-3.0.11 lib/eventbrite_sdk/resource/schema_definition.rb
eventbrite_sdk-3.0.10 lib/eventbrite_sdk/resource/schema_definition.rb
eventbrite_sdk-3.0.9 lib/eventbrite_sdk/resource/schema_definition.rb
eventbrite_sdk-3.0.8 lib/eventbrite_sdk/resource/schema_definition.rb
eventbrite_sdk-3.0.7 lib/eventbrite_sdk/resource/schema_definition.rb
eventbrite_sdk-3.0.6 lib/eventbrite_sdk/resource/schema_definition.rb
eventbrite_sdk-3.0.5 lib/eventbrite_sdk/resource/schema_definition.rb
eventbrite_sdk-3.0.4 lib/eventbrite_sdk/resource/schema_definition.rb
eventbrite_sdk-3.0.3 lib/eventbrite_sdk/resource/schema_definition.rb
eventbrite_sdk-3.0.2 lib/eventbrite_sdk/resource/schema_definition.rb