fauxsql 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ source :gemcutter
2
+
3
+ git "git://github.com/rails/rails.git"
4
+ gem "rake"
5
+ gem "jeweler"
6
+ gem "shoulda"
7
+ gem "activesupport", "~> 3.0.0.beta1", :require => "active_support"
8
+ gem "data_objects", "~> 0.10.1"
9
+ gem "do_sqlite3"
10
+ gem "dm-do-adapter", :git => "git://github.com/datamapper/dm-do-adapter.git"
11
+ gem "dm-sqlite-adapter", :git => "git://github.com/datamapper/dm-sqlite-adapter.git"
12
+ gem "dm-aggregates", :git => "git://github.com/datamapper/dm-aggregates.git"
13
+ gem 'dm-migrations', :git => 'git://github.com/datamapper/dm-migrations.git'
14
+ gem 'dm-timestamps', :git => 'git://github.com/datamapper/dm-timestamps.git'
15
+ gem 'dm-transactions', :git => 'git://github.com/datamapper/dm-transactions.git'
16
+ gem 'dm-types', :git => 'git://github.com/datamapper/dm-types.git'
17
+ gem 'dm-validations', :git => 'git://github.com/datamapper/dm-validations.git'
18
+ gem 'dm-aggregates', :git => 'git://github.com/datamapper/dm-aggregates.git'
19
+ gem 'dm-constraints', :git => 'git://github.com/datamapper/dm-constraints.git'
20
+ gem 'dm-observer', :git => 'git://github.com/datamapper/dm-observer.git'
21
+ gem "dm-core", :git => "git://github.com/collin/dm-core.git", "branch" => "next"
22
+ gem "datamapper"
data/Rakefile CHANGED
@@ -41,8 +41,6 @@ rescue LoadError
41
41
  end
42
42
  end
43
43
 
44
- task :test => :check_dependencies
45
-
46
44
  task :default => :test
47
45
 
48
46
  require 'rake/rdoctask'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
data/fauxsql.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{fauxsql}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Collin Miller"]
@@ -19,17 +19,20 @@ Gem::Specification.new do |s|
19
19
  s.files = [
20
20
  ".document",
21
21
  ".gitignore",
22
+ "Gemfile",
22
23
  "LICENSE",
23
24
  "README.rdoc",
24
25
  "Rakefile",
25
26
  "VERSION",
26
27
  "fauxsql.gemspec",
27
28
  "lib/fauxsql.rb",
28
- "lib/fauxsql/attribute.rb",
29
29
  "lib/fauxsql/attribute_list.rb",
30
30
  "lib/fauxsql/attribute_map.rb",
31
+ "lib/fauxsql/attribute_wrapper.rb",
31
32
  "lib/fauxsql/dereferenced_attribute.rb",
32
33
  "lib/fauxsql/dsl.rb",
34
+ "lib/fauxsql/list_wrapper.rb",
35
+ "lib/fauxsql/map_wrapper.rb",
33
36
  "test/helper.rb",
34
37
  "test/test_fauxsql.rb"
35
38
  ]
data/lib/fauxsql.rb CHANGED
@@ -6,9 +6,11 @@ require 'pathname'
6
6
  # Internal Libs
7
7
  root = Pathname.new(__FILE__).dirname.expand_path
8
8
  require root+'fauxsql/dereferenced_attribute'
9
- require root+'fauxsql/attribute'
10
9
  require root+'fauxsql/attribute_list'
11
10
  require root+'fauxsql/attribute_map'
11
+ require root+'fauxsql/attribute_wrapper'
12
+ require root+'fauxsql/map_wrapper'
13
+ require root+'fauxsql/list_wrapper'
12
14
  require root+'fauxsql/dsl'
13
15
  module Fauxsql
14
16
  extend ActiveSupport::Concern
@@ -36,14 +38,16 @@ module Fauxsql
36
38
  # a Ruby Array. Except it uses Fauxsql's dereference and resolve strategy to
37
39
  # store members.
38
40
  def get_fauxsql_list(list_name)
39
- fauxsql_attributes[list_name] or AttributeList.new(self, list_name)
41
+ list = fauxsql_attributes[list_name] || AttributeList.new
42
+ ListWrapper.new(list, self, list_name)
40
43
  end
41
44
 
42
45
  # Gets a reference to an AttributeMap object. AttributeMap quacks like
43
46
  # a Ruby Hash. Except it uses Fauxsql's dereference and resolve strategy to
44
47
  # store keys and values.
45
48
  def get_fauxsql_map(map_name)
46
- fauxsql_attributes[map_name] or AttributeMap.new(self, map_name)
49
+ map = fauxsql_attributes[map_name] || AttributeMap.new
50
+ MapWrapper.new(map, self, map_name)
47
51
  end
48
52
 
49
53
  # When setting values, all attributes pass through this method.
@@ -51,7 +55,7 @@ module Fauxsql
51
55
  # See #resolve_fauxsql_attribute to see how attributes are read.
52
56
  def self.dereference_fauxsql_attribute(attribute)
53
57
  if attribute.is_a?(DataMapper::Resource)
54
- DereferencedAttribute.new(attribute)
58
+ DereferencedAttribute.get(attribute)
55
59
  else
56
60
  attribute
57
61
  end
@@ -2,8 +2,6 @@ module Fauxsql
2
2
  # AttributeList is an Array that dereferences and resolves fauxsql attributes
3
3
  # when setting/reading members in the Array
4
4
  class AttributeList < Array
5
- include Attribute
6
-
7
5
  def <<(attribute)
8
6
  super Fauxsql.dereference_fauxsql_attribute(attribute)
9
7
  end
@@ -2,8 +2,6 @@ module Fauxsql
2
2
  # AttributeMap is an Hash that dereferences and resolves fauxsql attributes
3
3
  # when setting/reading members in the Hash
4
4
  class AttributeMap < Hash
5
- include Attribute
6
-
7
5
  # We dereference and resolve the key because in Ruby _any_ object
8
6
  # can be a hash key. Even a DataMapper record.
9
7
  def []= key, value
@@ -17,16 +15,37 @@ module Fauxsql
17
15
  Fauxsql.resolve_fauxsql_attribute super(real_key.hash)
18
16
  end
19
17
 
18
+ def each(&block)
19
+ super do |key, value|
20
+ yield(resolve_key(key), resolve_value(value))
21
+ end
22
+ end
23
+
20
24
  # VERY VERY SPECIFIC to the marshal dump format.
21
25
  # Probably brittle.
22
26
  def keys
23
27
  super.map do |key|
24
- if key.match(/^.+Fauxsql::DereferencedAttribute.+@klass.+@lookup_key.+$/)
25
- Fauxsql::DereferencedAttribute.load(key)
26
- else
27
- key
28
- end
28
+ resolve_key(key)
29
+ end
30
+ end
31
+
32
+ def resolve_key(key)
33
+ if key.respond_to?(:match) && key.match(/^.+Fauxsql::DereferencedAttribute.+@klass.+@lookup_key.+$/)
34
+ Fauxsql.resolve_fauxsql_attribute Fauxsql::DereferencedAttribute.load(key)
35
+ else
36
+ key
29
37
  end
30
38
  end
39
+
40
+ def resolve_value(value)
41
+ Fauxsql.resolve_fauxsql_attribute value
42
+ end
43
+
44
+ def eql?(other)
45
+ return false
46
+ raise [self, other.inspect].inspect
47
+ end
48
+
49
+
31
50
  end
32
51
  end
@@ -0,0 +1,20 @@
1
+ module Fauxsql
2
+ class AttributeWrapper
3
+ extend ActiveSupport::Concern
4
+ attr_reader:attribute
5
+ attr_reader:record
6
+ attr_reader:name
7
+
8
+ def initialize(attribute, record, name)
9
+ @attribute, @record, @name = attribute, record, name
10
+ @record.fauxsql_attributes[name] ||= attribute
11
+ end
12
+
13
+ def dirty!
14
+ record.attribute_set(:fauxsql_attributes, record.fauxsql_attributes.dup)
15
+ value = yield
16
+ record.attribute_set(:fauxsql_attributes, record.fauxsql_attributes)
17
+ value
18
+ end
19
+ end
20
+ end
@@ -4,6 +4,7 @@ module Fauxsql
4
4
  # DataMapper::Resource object is given. This way only the class and the
5
5
  # primary key are stored.
6
6
  class DereferencedAttribute
7
+ @@identity_map = {}
7
8
  def initialize(attribute)
8
9
  @klass = attribute.class
9
10
  @lookup_key = attribute.key
@@ -18,6 +19,10 @@ module Fauxsql
18
19
  end
19
20
  alias hash dump
20
21
 
22
+ def self.get(attribute)
23
+ @@identity_map[attribute] ||= new(attribute)
24
+ end
25
+
21
26
  def self.load(dump)
22
27
  Marshal.load(dump)
23
28
  end
@@ -0,0 +1,12 @@
1
+ require "active_support/dependencies"
2
+ require "active_support/core_ext/module/delegation"
3
+ module Fauxsql
4
+ class ListWrapper < AttributeWrapper
5
+ alias list attribute
6
+ delegate :[], :==, :<<, :first, :last, :equals, :map_resolved, :to => :list
7
+
8
+ def <<(item)
9
+ dirty! { list << item }
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require "active_support/dependencies"
2
+ require "active_support/core_ext/module/delegation"
3
+ module Fauxsql
4
+ class MapWrapper < AttributeWrapper
5
+ alias map attribute
6
+ delegate :[], :each, :keys, :resolve_key, :resolve_value, :to => :map
7
+
8
+ def []=(key, value)
9
+ dirty! { map[key] = value }
10
+ end
11
+ end
12
+ end
data/test/test_fauxsql.rb CHANGED
@@ -55,7 +55,7 @@ class TestFauxsql < Test::Unit::TestCase
55
55
  @faux.things << :hello
56
56
  @faux.things << :goodbye
57
57
  reload
58
- assert_equal [:hello, :goodbye], @faux.things
58
+ assert @faux.things == [:hello, :goodbye]
59
59
  end
60
60
 
61
61
  should "persist maps" do
@@ -119,9 +119,37 @@ class TestFauxsql < Test::Unit::TestCase
119
119
  simple1 = SimpleKey.create
120
120
  simple2 = SimpleKey.create
121
121
  @faux.dictionary[simple1] = simple2
122
- assert_equal Fauxsql::DereferencedAttribute, @faux.dictionary.keys.first.class
122
+ assert_equal SimpleKey, @faux.dictionary.keys.first.class
123
123
  reload
124
124
  assert_equal simple2, @faux.dictionary[simple1]
125
125
  end
126
+
127
+ should "give records as keys/values when calling #each" do
128
+ simple1 = SimpleKey.create
129
+ simple2 = SimpleKey.create
130
+ @faux.dictionary[simple1] = simple2
131
+ reload
132
+ @faux.dictionary.each do |key, value|
133
+ assert_equal simple1, key
134
+ assert_equal simple2, value
135
+ end
136
+ end
137
+
138
+ should "not choke on normal values in hash when calling #each" do
139
+ simple1 = SimpleKey.create
140
+ @faux.dictionary[simple1] = 33
141
+ reload
142
+ @faux.dictionary.each{|key, value| }
143
+ assert true, "choked on normal values in #each"
144
+ end
145
+
146
+ should "persist changes to maps" do
147
+ simple1 = SimpleKey.create
148
+ @faux.dictionary[simple1] = 33
149
+ reload
150
+ @faux.dictionary[simple1] = 50
151
+ reload
152
+ assert_equal 50, @faux.dictionary[simple1]
153
+ end
126
154
  end
127
155
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Collin Miller
@@ -79,17 +79,20 @@ extra_rdoc_files:
79
79
  files:
80
80
  - .document
81
81
  - .gitignore
82
+ - Gemfile
82
83
  - LICENSE
83
84
  - README.rdoc
84
85
  - Rakefile
85
86
  - VERSION
86
87
  - fauxsql.gemspec
87
88
  - lib/fauxsql.rb
88
- - lib/fauxsql/attribute.rb
89
89
  - lib/fauxsql/attribute_list.rb
90
90
  - lib/fauxsql/attribute_map.rb
91
+ - lib/fauxsql/attribute_wrapper.rb
91
92
  - lib/fauxsql/dereferenced_attribute.rb
92
93
  - lib/fauxsql/dsl.rb
94
+ - lib/fauxsql/list_wrapper.rb
95
+ - lib/fauxsql/map_wrapper.rb
93
96
  - test/helper.rb
94
97
  - test/test_fauxsql.rb
95
98
  has_rdoc: true
@@ -1,10 +0,0 @@
1
- module Fauxsql
2
- # Attribute is a mixin for AttributeList and AttributeMap classes.
3
- # It should be mixed into classes that inherit standard Ruby data structures
4
- module Attribute
5
- def initialize(record, name)
6
- super() # explicitly super without arguments to preserve base behavior
7
- record.fauxsql_attributes[name] = self
8
- end
9
- end
10
- end