Class: Serdee::Attribute

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

Defined Under Namespace

Classes: Config

Instance Attribute Summary collapse

Instance Method Summary collapse

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, **options, &block)
  @key = key
  { allow_blank: false }.merge(options).each do |option, setting|
    config.public_send(option, setting)
  end
  config.instance_eval(&block) if block_given?
end

Instance Attribute Details

#allow_blankObject

Returns the value of attribute allow_blank.



6
7
8
# File 'lib/serdee/attribute.rb', line 6

def allow_blank
  @allow_blank
end

#asObject



20
21
22
# File 'lib/serdee/attribute.rb', line 20

def as
  @as || key
end

#defaultObject

Returns the value of attribute default.



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

def default
  @default
end

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

#optionalObject

Returns the value of attribute optional.



6
7
8
# File 'lib/serdee/attribute.rb', line 6

def optional
  @optional
end

#serializerObject

Returns the value of attribute serializer.



6
7
8
# File 'lib/serdee/attribute.rb', line 6

def serializer
  @serializer
end

Instance Method Details

#configObject



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