Sha256: ba6ce84718b3bceaf0348d41eb1744e8eb6ac7ebd74f9d8c2cc5f7dbbdfef24b

Contents?: true

Size: 1021 Bytes

Versions: 4

Compression:

Stored size: 1021 Bytes

Contents

module FunWithJsonApi
  class Attribute
    attr_reader :name
    attr_reader :as
    attr_reader :options

    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.to_sym
      @as = options.fetch(:as, name).to_sym
      @options = options
    end

    def decode(value)
      value
    end
    alias call decode

    def sanitize_attribute_method
      :"parse_#{param_value}"
    end

    def param_value
      as
    end
  end
end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fun_with_json_api-0.0.14 lib/fun_with_json_api/attribute.rb
fun_with_json_api-0.0.13 lib/fun_with_json_api/attribute.rb
fun_with_json_api-0.0.11.3 lib/fun_with_json_api/attribute.rb
fun_with_json_api-0.0.11.2 lib/fun_with_json_api/attribute.rb