Class: SimpleMapper::Attribute

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

Direct Known Subclasses

Pattern

Defined Under Namespace

Modules: Collection Classes: Pattern

Constant Summary collapse

Options =
[
  :default,
  :key,
  :type,
  :mapper,
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Attribute.



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

def initialize(name, options = {})
  self.key = self.name = name
  process_options(options)
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



9
10
11
# File 'lib/simple_mapper/attribute.rb', line 9

def default
  @default
end

#keyObject

Returns the value of attribute key.



9
10
11
# File 'lib/simple_mapper/attribute.rb', line 9

def key
  @key
end

#mapperObject

Returns the value of attribute mapper.



9
10
11
# File 'lib/simple_mapper/attribute.rb', line 9

def mapper
  @mapper
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/simple_mapper/attribute.rb', line 9

def name
  @name
end

Instance Method Details

#change_tracking_for(object) ⇒ Object



80
81
82
# File 'lib/simple_mapper/attribute.rb', line 80

def change_tracking_for(object)
  object.simple_mapper_changes
end

#changed!(object, flag = true) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/simple_mapper/attribute.rb', line 84

def changed!(object, flag=true)
  if flag
    change_tracking_for(object)[name] = true
  else
    change_tracking_for(object).delete(name)
    false
  end
end

#changed?(object) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
# File 'lib/simple_mapper/attribute.rb', line 93

def changed?(object)
  if mapper and val = value(object)
    val.changed?
  else
    (change_tracking_for(object)[name] && true) || false
  end
end

#converterObject



58
59
60
61
62
63
# File 'lib/simple_mapper/attribute.rb', line 58

def converter
  return nil unless type
  converter = type.respond_to?(:encode) || type.respond_to?(:decode) ? type : (t = SimpleMapper::Attributes.type_for(type) and t[:converter])
  raise SimpleMapper::InvalidTypeException unless converter
  converter
end

#default_value(object) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/simple_mapper/attribute.rb', line 101

def default_value(object)
  case d = self.default
    when :from_type
      converter.default
    when Symbol
      object.send(d)
    else
      nil
  end
end

#encode(value) ⇒ Object



65
66
67
68
# File 'lib/simple_mapper/attribute.rb', line 65

def encode(value)
  return value unless c = converter
  c.encode(value)
end

#freeze_for(object) ⇒ Object



112
113
114
115
116
# File 'lib/simple_mapper/attribute.rb', line 112

def freeze_for(object)
  if mapper and val = value(object)
    val.freeze
  end
end

#process_options(options = {}) ⇒ Object



24
25
26
27
28
# File 'lib/simple_mapper/attribute.rb', line 24

def process_options(options = {})
  Options.each do |option|
    self.send(:"#{option.to_s}=", options[option]) if options[option]
  end
end

#source_value(object) ⇒ Object



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

def source_value(object)
  source = object.simple_mapper_source
  source.has_key?(key) ? source[key] : source[key.to_s]
end

#to_simple(object, container, options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/simple_mapper/attribute.rb', line 70

def to_simple(object, container, options = {})
  raw_value = self.value(object)
  if mapper
    value = raw_value.nil? ? nil : raw_value.to_simple(options)
  else
    value = encode(raw_value)
  end
  container[options[:string_keys] ? key.to_s : key] = value unless value.nil? and options[:defined]
end

#transformed_source_value(object) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/simple_mapper/attribute.rb', line 35

def transformed_source_value(object)
  val = source_value(object)
  val = default_value(object) if val.nil?
  if type = self.type
    if type.respond_to?(:decode)
      type.decode(val)
    else
      type = SimpleMapper::Attributes.type_for(type)
      if expected = type[:expected_type] and val.instance_of? expected
        val
      else
        type[:converter].decode(val)
      end
    end
  else
    val
  end
end

#typeObject



15
16
17
# File 'lib/simple_mapper/attribute.rb', line 15

def type
  @type || mapper
end

#type=(new_type) ⇒ Object



11
12
13
# File 'lib/simple_mapper/attribute.rb', line 11

def type=(new_type)
  @type = new_type
end

#value(object) ⇒ Object



54
55
56
# File 'lib/simple_mapper/attribute.rb', line 54

def value(object)
  object.send(name)
end