Class: Flatter::Mapping

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload
Defined in:
lib/flatter/mapping.rb

Defined Under Namespace

Modules: Scribe Classes: Factory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapper, name, target_attribute, **options) ⇒ Mapping

Returns a new instance of Mapping.



12
13
14
15
16
17
# File 'lib/flatter/mapping.rb', line 12

def initialize(mapper, name, target_attribute, **options)
  @mapper           = mapper
  @name             = name.to_s
  @target_attribute = target_attribute
  @options          = options
end

Instance Attribute Details

#mapperObject (readonly)

Returns the value of attribute mapper.



8
9
10
# File 'lib/flatter/mapping.rb', line 8

def mapper
  @mapper
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/flatter/mapping.rb', line 8

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/flatter/mapping.rb', line 8

def options
  @options
end

#target_attributeObject (readonly)

Returns the value of attribute target_attribute.



8
9
10
# File 'lib/flatter/mapping.rb', line 8

def target_attribute
  @target_attribute
end

Instance Method Details

#readObject



19
20
21
# File 'lib/flatter/mapping.rb', line 19

def read
  read!
end

#read!Object



23
24
25
# File 'lib/flatter/mapping.rb', line 23

def read!
  target.public_send(target_attribute)
end

#read_as_paramsObject



35
36
37
# File 'lib/flatter/mapping.rb', line 35

def read_as_params
  {name => read}
end

#write(value) ⇒ Object



27
28
29
# File 'lib/flatter/mapping.rb', line 27

def write(value)
  write!(value)
end

#write!(value) ⇒ Object



31
32
33
# File 'lib/flatter/mapping.rb', line 31

def write!(value)
  target.public_send("#{target_attribute}=", value)
end

#write_from_params(params) ⇒ Object



39
40
41
# File 'lib/flatter/mapping.rb', line 39

def write_from_params(params)
  write(params[name]) if params.key?(name)
end