Sha256: b294a426328bed0e93952d4c89df6228bc5c81fa54794fd4cf2b8d073008ef89

Contents?: true

Size: 947 Bytes

Versions: 6

Compression:

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

    def call(value)
      value
    end

    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

6 entries across 6 versions & 1 rubygems

Version Path
fun_with_json_api-0.0.6.pre.alpha.1 lib/fun_with_json_api/attribute.rb
fun_with_json_api-0.0.6.1 lib/fun_with_json_api/attribute.rb
fun_with_json_api-0.0.6 lib/fun_with_json_api/attribute.rb
fun_with_json_api-0.0.5 lib/fun_with_json_api/attribute.rb
fun_with_json_api-0.0.4 lib/fun_with_json_api/attribute.rb
fun_with_json_api-0.0.3 lib/fun_with_json_api/attribute.rb