Module: SimpleMapper::Attributes

Defined in:
lib/simple_mapper/attributes.rb

Defined Under Namespace

Modules: ClassMethods, Types Classes: Manager

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#simple_mapper_sourceObject (readonly)

Returns the value of attribute simple_mapper_source.



128
129
130
# File 'lib/simple_mapper/attributes.rb', line 128

def simple_mapper_source
  @simple_mapper_source
end

Class Method Details

.included(klass) ⇒ Object



17
18
19
# File 'lib/simple_mapper/attributes.rb', line 17

def self.included(klass)
  klass.extend ClassMethods
end

Instance Method Details

#all_changed!Object



95
96
97
# File 'lib/simple_mapper/attributes.rb', line 95

def all_changed!
  simple_mapper_changes.all_changed!
end

#all_changed?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/simple_mapper/attributes.rb', line 99

def all_changed?
  simple_mapper_changes.all
end

#attribute_changed!(attr, flag = true) ⇒ Object



87
88
89
# File 'lib/simple_mapper/attributes.rb', line 87

def attribute_changed!(attr, flag=true)
  attribute_object_for(attr).changed!(self, flag)
end

#attribute_changed?(attr) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/simple_mapper/attributes.rb', line 91

def attribute_changed?(attr)
  attribute_object_for(attr).changed?(self)
end

#attribute_object_for(attr) ⇒ Object



39
40
41
# File 'lib/simple_mapper/attributes.rb', line 39

def attribute_object_for(attr)
  self.class.simple_mapper.attributes[attr]
end

#changed?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/simple_mapper/attributes.rb', line 115

def changed?
  changed_attributes.size > 0
end

#changed_attributesObject



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/simple_mapper/attributes.rb', line 103

def changed_attributes
  attribs = self.class.simple_mapper.attributes
  if simple_mapper_changes.all
    attribs.keys
  else
    attribs.inject([]) do |list, keyval|
      list << keyval[0] if keyval[1].changed?(self)
      list
    end
  end
end

#freezeObject



119
120
121
122
# File 'lib/simple_mapper/attributes.rb', line 119

def freeze
  self.class.simple_mapper.attributes.values.each {|attribute| attribute.freeze_for(self)}
  simple_mapper_changes.freeze
end

#frozen?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/simple_mapper/attributes.rb', line 124

def frozen?
  simple_mapper_changes.frozen?
end

#get_attribute_default(attr) ⇒ Object



79
80
81
# File 'lib/simple_mapper/attributes.rb', line 79

def get_attribute_default(attr)
  attribute_object_for(attr).default_value(self)
end

#initialize(values = {}) ⇒ Object



130
131
132
133
134
# File 'lib/simple_mapper/attributes.rb', line 130

def initialize(values = {})
  @simple_mapper_source = values
  @simple_mapper_init = {}
  @simple_mapper_changes = SimpleMapper::ChangeHash.new
end

#key_for(attr) ⇒ Object



43
44
45
# File 'lib/simple_mapper/attributes.rb', line 43

def key_for(attr)
  attribute_object_for(attr).key
end

#read_attribute(attr) ⇒ Object



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

def read_attribute(attr)
  if @simple_mapper_init[attr]
    instance_variable_get(:"@#{attr}")
  else
    result = instance_variable_set(:"@#{attr}", transform_source_attribute(attr))
    @simple_mapper_init[attr] = true
    result
  end
end

#read_source_attribute(attr) ⇒ Object



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

def read_source_attribute(attr)
  attribute_object_for(attr).source_value(self)
end

#reset_attribute(attr) ⇒ Object



47
48
49
50
51
# File 'lib/simple_mapper/attributes.rb', line 47

def reset_attribute(attr)
  @simple_mapper_init.delete attr
  attribute_changed!(attr, false)
  remove_instance_variable(:"@#{attr}")
end

#simple_mapper_changesObject



83
84
85
# File 'lib/simple_mapper/attributes.rb', line 83

def simple_mapper_changes
  @simple_mapper_changes ||= SimpleMapper::ChangeHash.new
end

#to_simple(options = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/simple_mapper/attributes.rb', line 136

def to_simple(options = {})
  clean_opt = options.clone
  # if all_changed? true, we disregard changed flag entirely, so the entire graph
  # appears to be changed in result set.  We
  # also propagate the :all flag, which tells
  # objects that support it to include information
  # about members that were removed.
  if all_changed?
    clean_opt.delete(:changed)
    clean_opt[:all] = true
  end
  changes = (clean_opt[:changed] && true) || false
  self.class.simple_mapper.attributes.values.inject({}) do |container, attrib|
    attrib.to_simple(self, container, clean_opt) if !changes or attrib.changed?(self)
    container
  end
end

#transform_source_attribute(attr) ⇒ Object



61
62
63
# File 'lib/simple_mapper/attributes.rb', line 61

def transform_source_attribute(attr)
  attribute_object_for(attr).transformed_source_value(self)
end

#write_attribute(attr, value) ⇒ Object

Raises:

  • (RuntimeError)


53
54
55
56
57
58
59
# File 'lib/simple_mapper/attributes.rb', line 53

def write_attribute(attr, value)
  raise(RuntimeError, "can't modify frozen object") if frozen?
  instance_variable_set(:"@#{attr}", value)
  @simple_mapper_init[attr] = true
  attribute_changed! attr
  value
end