Class: FunWithJsonApi::Attribute
- Inherits:
-
Object
- Object
- FunWithJsonApi::Attribute
- Defined in:
- lib/fun_with_json_api/attribute.rb
Direct Known Subclasses
FunWithJsonApi::Attributes::BooleanAttribute, FunWithJsonApi::Attributes::DateAttribute, FunWithJsonApi::Attributes::DatetimeAttribute, FunWithJsonApi::Attributes::DecimalAttribute, FunWithJsonApi::Attributes::FloatAttribute, FunWithJsonApi::Attributes::IntegerAttribute, FunWithJsonApi::Attributes::Relationship, FunWithJsonApi::Attributes::RelationshipCollection, FunWithJsonApi::Attributes::StringAttribute, FunWithJsonApi::Attributes::UuidV4Attribute
Instance Attribute Summary collapse
-
#as ⇒ Object
readonly
Returns the value of attribute as.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #decode(value) ⇒ Object (also: #call)
-
#initialize(name, options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
- #param_value ⇒ Object
- #sanitize_attribute_method ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Attribute
Returns a new instance of Attribute.
17 18 19 20 21 22 23 |
# File 'lib/fun_with_json_api/attribute.rb', line 17 def initialize(name, = {}) raise ArgumentError, 'name cannot be blank!' unless name.present? @name = name.to_sym @as = .fetch(:as, name).to_sym @options = end |
Instance Attribute Details
#as ⇒ Object (readonly)
Returns the value of attribute as.
4 5 6 |
# File 'lib/fun_with_json_api/attribute.rb', line 4 def as @as end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/fun_with_json_api/attribute.rb', line 3 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/fun_with_json_api/attribute.rb', line 5 def @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, = {}) format = .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, ) 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_value ⇒ Object
34 35 36 |
# File 'lib/fun_with_json_api/attribute.rb', line 34 def param_value as end |
#sanitize_attribute_method ⇒ Object
30 31 32 |
# File 'lib/fun_with_json_api/attribute.rb', line 30 def sanitize_attribute_method :"parse_#{param_value}" end |