Sha256: 84cf5864cb605da61ee9ec8098a89e29418126b8939f41710e1d9edc87e92b3a

Contents?: true

Size: 940 Bytes

Versions: 2

Compression:

Stored size: 940 Bytes

Contents

module FunWithJsonApi
  class Attribute
    attr_reader :name
    attr_reader :as

    def self.create(name, options = {})
      format = options.fetch(:format, 'string')
      attribute_class_name = "#{format.to_s.classify}Attribute"
      if FunWithJsonApi::Attributes.const_defined?(attribute_class_name)
        FunWithJsonApi::Attributes.const_get(attribute_class_name)
      else
        raise ArgumentError, "Unknown attribute type: #{format}"
      end.new(name, options)
    end

    def initialize(name, options = {})
      raise ArgumentError, 'name cannot be blank!' unless name.present?

      @name = name
      @as = options.fetch(:as, name)
    end

    def call(value)
      value
    end

    def sanitize_attribute_method
      :"parse_#{param_value}"
    end

    def param_value
      as.to_sym
    end
  end
end

# Load pre-defined Attributes
Dir["#{File.dirname(__FILE__)}/attributes/**/*.rb"].each { |f| require f }

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fun_with_json_api-0.0.2 lib/fun_with_json_api/attribute.rb
fun_with_json_api-0.0.1 lib/fun_with_json_api/attribute.rb