Module: Fauxsql

Extended by:
ActiveSupport::Concern
Defined in:
lib/fauxsql.rb,
lib/fauxsql/dsl.rb,
lib/fauxsql/map_wrapper.rb,
lib/fauxsql/list_wrapper.rb,
lib/fauxsql/attribute_map.rb,
lib/fauxsql/attribute_list.rb,
lib/fauxsql/manymany_wrapper.rb,
lib/fauxsql/attribute_wrapper.rb,
lib/fauxsql/attribute_manymany.rb,
lib/fauxsql/dereferenced_attribute.rb

Defined Under Namespace

Modules: DSL Classes: AttributeList, AttributeManymany, AttributeMap, AttributeWrapper, Attributes, DereferencedAttribute, ListWrapper, ManymanyWrapper, MapWrapper, Options

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dereference_fauxsql_attribute(attribute) ⇒ Object

When setting values, all attributes pass through this method. This way we can control how certain classes are serialized by Fauxsql See #resolve_fauxsql_attribute to see how attributes are read.



77
78
79
80
81
82
83
# File 'lib/fauxsql.rb', line 77

def self.dereference_fauxsql_attribute(attribute)
  if attribute.is_a?(DataMapper::Resource)
    DereferencedAttribute.get(attribute)
  else
    attribute
  end
end

.dereference_fauxsql_key(key) ⇒ Object

AttributeMap is an Hash that dereferences and resolves fauxsql attributes when setting/reading members in the Hash



4
5
6
7
8
9
10
# File 'lib/fauxsql/attribute_map.rb', line 4

def self.dereference_fauxsql_key(key)
  real_key   = Fauxsql.dereference_fauxsql_attribute(key)
  if (real_key.is_a? Fauxsql::DereferencedAttribute)
    real_key = real_key.hash
  end
  real_key
end

.dirty!(record) ⇒ Object



96
97
98
99
100
101
# File 'lib/fauxsql.rb', line 96

def self.dirty!(record)
  record.attribute_set(:fauxsql_attributes, record.fauxsql_attributes.dup)
  value = yield
  record.attribute_set(:fauxsql_attributes, record.fauxsql_attributes)
  value
end

.resolve_fauxsql_attribute(attribute) ⇒ Object

When reading values, all attributes pass through this method. This way we can control how certain classes are deserialized by Fauxsql See #dereference_fauxsql_attribute to see how attributes are stored.



88
89
90
91
92
93
94
# File 'lib/fauxsql.rb', line 88

def self.resolve_fauxsql_attribute(attribute)
  if attribute.is_a?(DereferencedAttribute)
    attribute.resolve
  else
    attribute
  end
end

Instance Method Details

#fauxsql_nested_classes(attribute_name) ⇒ Object



103
104
105
106
# File 'lib/fauxsql.rb', line 103

def fauxsql_nested_classes(attribute_name)
  # Find a list of the nested classes that an attribute has and return them as a list
  self.class.fauxsql_options[attribute_name][:nest]   # provides a list of classes  
end

#get_fauxsql_attribute(attribute_name) ⇒ Object

Getter method for attributes defined as:

attribute :attribute_name


35
36
37
38
39
40
41
# File 'lib/fauxsql.rb', line 35

def get_fauxsql_attribute(attribute_name)
  attribute = fauxsql_attributes[attribute_name]
  value = Fauxsql.resolve_fauxsql_attribute(attribute)

  options = fauxsql_options[attribute_name]
  options and options[:type] ? value.send(options[:type]) : value
end

#get_fauxsql_list(attribute_name) ⇒ Object

Gets a reference to an AttributeList object. AttributeList quacks like a Ruby Array. Except it uses Fauxsql’s dereference and resolve strategy to store members.



56
57
58
59
# File 'lib/fauxsql.rb', line 56

def get_fauxsql_list(attribute_name)
  list = fauxsql_attributes[attribute_name] || AttributeList.new
  ListWrapper.new(list, self, attribute_name, fauxsql_options[attribute_name])
end

#get_fauxsql_manymany(attribute_name, classes, options) ⇒ Object



69
70
71
72
# File 'lib/fauxsql.rb', line 69

def get_fauxsql_manymany(attribute_name, classes, options)
  manymany = fauxsql_attributes[attribute_name] || AttributeManymany.new
  ManymanyWrapper.new(manymany, self, attribute_name, classes, options)
end

#get_fauxsql_map(attribute_name) ⇒ Object

Gets a reference to an AttributeMap object. AttributeMap quacks like a Ruby Hash. Except it uses Fauxsql’s dereference and resolve strategy to store keys and values.



64
65
66
67
# File 'lib/fauxsql.rb', line 64

def get_fauxsql_map(attribute_name)
  map = fauxsql_attributes[attribute_name] || AttributeMap.new
  MapWrapper.new(map, self,  attribute_name, fauxsql_options[attribute_name])
end

#set_fauxsql_attribute(attribute_name, value) ⇒ Object

Setter method for attributes defined as:

attribute :attribute_name


45
46
47
48
49
50
51
# File 'lib/fauxsql.rb', line 45

def set_fauxsql_attribute(attribute_name, value)
  options = fauxsql_options[attribute_name]
  value = value.send(options[:type]) if options and options[:type]
  
  attribute = Fauxsql.dereference_fauxsql_attribute(value)
  Fauxsql.dirty!(self){ fauxsql_attributes[attribute_name] = attribute }    
end