Class: Serdee::Attribute
- Inherits:
-
Object
- Object
- Serdee::Attribute
- Defined in:
- lib/serdee/attribute.rb
Defined Under Namespace
Classes: Config
Instance Attribute Summary collapse
-
#allow_blank ⇒ Object
Returns the value of attribute allow_blank.
- #as ⇒ Object
-
#default ⇒ Object
Returns the value of attribute default.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#optional ⇒ Object
Returns the value of attribute optional.
-
#serializer ⇒ Object
Returns the value of attribute serializer.
Instance Method Summary collapse
- #config ⇒ Object
- #deserialize(value) ⇒ Object
- #extract(hash) ⇒ Object
- #extract_to(data, obj) ⇒ Object
-
#initialize(key, **options, &block) ⇒ Attribute
constructor
A new instance of Attribute.
- #insert(value, hash) ⇒ Object
- #insert_to(obj, hash) ⇒ Object
- #serialize(value) ⇒ Object
Constructor Details
#initialize(key, **options, &block) ⇒ Attribute
Returns a new instance of Attribute.
8 9 10 11 12 13 14 |
# File 'lib/serdee/attribute.rb', line 8 def initialize(key, **, &block) @key = key { allow_blank: false }.merge().each do |option, setting| config.public_send(option, setting) end config.instance_eval(&block) if block_given? end |
Instance Attribute Details
#allow_blank ⇒ Object
Returns the value of attribute allow_blank.
6 7 8 |
# File 'lib/serdee/attribute.rb', line 6 def allow_blank @allow_blank end |
#as ⇒ Object
20 21 22 |
# File 'lib/serdee/attribute.rb', line 20 def as @as || key end |
#default ⇒ Object
Returns the value of attribute default.
4 5 6 |
# File 'lib/serdee/attribute.rb', line 4 def default @default end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
4 5 6 |
# File 'lib/serdee/attribute.rb', line 4 def key @key end |
#optional ⇒ Object
Returns the value of attribute optional.
6 7 8 |
# File 'lib/serdee/attribute.rb', line 6 def optional @optional end |
#serializer ⇒ Object
Returns the value of attribute serializer.
6 7 8 |
# File 'lib/serdee/attribute.rb', line 6 def serializer @serializer end |
Instance Method Details
#config ⇒ Object
16 17 18 |
# File 'lib/serdee/attribute.rb', line 16 def config @config ||= Config.new(self) end |
#deserialize(value) ⇒ Object
52 53 54 |
# File 'lib/serdee/attribute.rb', line 52 def deserialize(value) value end |
#extract(hash) ⇒ Object
44 45 46 |
# File 'lib/serdee/attribute.rb', line 44 def extract(hash) deserialize(hash[as]) end |
#extract_to(data, obj) ⇒ Object
39 40 41 42 |
# File 'lib/serdee/attribute.rb', line 39 def extract_to(data, obj) obj.send("#{key}=", extract(data)) obj end |
#insert(value, hash) ⇒ Object
29 30 31 32 |
# File 'lib/serdee/attribute.rb', line 29 def insert(value, hash) new_value = serialize(value) hash[as] = new_value if new_value.present? || allow_blank end |
#insert_to(obj, hash) ⇒ Object
34 35 36 37 |
# File 'lib/serdee/attribute.rb', line 34 def insert_to(obj, hash) insert(obj.send(key), hash) hash end |
#serialize(value) ⇒ Object
48 49 50 |
# File 'lib/serdee/attribute.rb', line 48 def serialize(value) value end |