Class: FunWithJsonApi::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/fun_with_json_api/attribute.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Attribute

Returns a new instance of Attribute.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
# File 'lib/fun_with_json_api/attribute.rb', line 17

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

Instance Attribute Details

#asObject (readonly)

Returns the value of attribute as.



4
5
6
# File 'lib/fun_with_json_api/attribute.rb', line 4

def as
  @as
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/fun_with_json_api/attribute.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/fun_with_json_api/attribute.rb', line 5

def options
  @options
end

Class Method Details

.create(name, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/fun_with_json_api/attribute.rb', line 7

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

Instance Method Details

#decode(value) ⇒ Object Also known as: call



25
26
27
# File 'lib/fun_with_json_api/attribute.rb', line 25

def decode(value)
  value
end

#param_valueObject



34
35
36
# File 'lib/fun_with_json_api/attribute.rb', line 34

def param_value
  as
end

#sanitize_attribute_methodObject



30
31
32
# File 'lib/fun_with_json_api/attribute.rb', line 30

def sanitize_attribute_method
  :"parse_#{param_value}"
end