Permalink
Browse files

Switch to composition

  • Loading branch information...
kyledrake committed Jan 22, 2012
1 parent fa52994 commit 3ca5c9ada374398a94d4187c3efcc7d1fe659a54
Showing with 107 additions and 43 deletions.
  1. +63 −42 lib/geoloqi/model.rb
  2. +2 −1 lib/geoloqi/models/layer.rb
  3. +35 −0 spec/geoloqi/model_spec.rb
  4. +7 −0 spec/geoloqi/models/layer_spec.rb
View
@@ -1,6 +1,6 @@
module Geoloqi
- module Model
+ module Model
class Property
attr_accessor :name, :type
def initialize(name, type)
@@ -9,62 +9,83 @@ def initialize(name, type)
end
end
- class Base
- @@properties = []
-
- class << self
- def property(name, type)
- @@properties << Property.new(name, type)
- define_method "#{name}=" do |value|
- @unsaved_attributes ||= []
- @unsaved_attributes << name
- attribute_set name, value
- end
- attr_reader name
- end
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
- def properties
- @@properties
- end
+ module ClassMethods
- def property_keys
- properties.collect {|a| a.name}
+ def property(name, type)
+ @_properties ||= []
+ @_properties << Property.new(name, type)
+ define_method "#{name}=" do |value|
+ @_unsaved_attributes ||= []
+ @_unsaved_attributes << name
+ attribute_set name, value
end
+ attr_reader name
end
- def attribute_set(name, value)
- raise ArgumentError, "property \"#{name}\" does not exist for this class" unless self.class.property_keys.include? name
- instance_variable_set "@#{name}".to_sym, value
+ def properties
+ @_properties
end
- def attribute_get(name)
- instance_variable_get name
+ def property_keys
+ properties.collect {|a| a.name}
end
- def attributes_get
- attributes = {}
- self.class.property_keys.each {|key| attributes[key] = send(key)}
- attributes
- end
+ end
- def attributes(attributes=nil)
- return attributes_get if attributes.nil?
- send :attributes=, attributes
+ def save
+ if @_new_record
+ puts "CREATING RECORD"
+ puts "TODO: Create record"
end
+ puts "SAVING #{attributes_get.inspect}"
+ true
+ end
- def attributes=(attributes={})
- attributes.each {|key, value| attribute_set key, value}
- end
+ def attribute_set(name, value)
+ raise ArgumentError, "property \"#{name}\" does not exist for this class" unless self.class.property_keys.include? name
+ instance_variable_set "@#{name}".to_sym, value
+ end
- def initialize(attributes={})
- attributes.each {|k,v| send("#{k}=", v) }
- end
+ def attribute_get(name)
+ instance_variable_get name
+ end
- alias_method :to_hash, :attributes
+ def attributes_get
+ attributes = {}
+ self.class.property_keys.each {|key| attributes[key] = send(key)}
+ attributes
+ end
- def to_json
- attributes.to_json
- end
+ def attributes(attributes=nil)
+ return attributes_get if attributes.nil?
+ send :attributes=, attributes
+ end
+
+ def attributes=(attributes={})
+ attributes.each {|key, value| attribute_set key, value}
+ end
+
+ def unsaved_attributes
+ @_unsaved_attributes ||= []
+ end
+
+ def unsaved_attributes?
+ !unsaved_attributes.empty?
+ end
+
+ def initialize(attributes={})
+ @_new_record = true
+ attributes.each {|k,v| send("#{k}=", v) }
+ end
+
+ alias_method :to_hash, :attributes
+
+ def to_json
+ attributes.to_json
end
end
end
@@ -1,5 +1,6 @@
module Geoloqi
- class Layer < Model::Base
+ class Layer
+ include Model
property :name, :string
property :key, :string
end
View
@@ -0,0 +1,35 @@
+require File.join '.', File.dirname(__FILE__), '..', 'env.rb'
+
+class Widget
+ include Geoloqi::Model
+ property :name, :string
+end
+
+describe Widget do
+ it 'adds property to class' do
+ Widget.properties.first.name.must_equal :name
+ Widget.properties.first.type.must_equal :string
+ end
+
+ it 'instantiates with property' do
+ widget = Widget.new :name => 'Captain'
+ widget.name.must_equal 'Captain'
+ widget.to_hash.must_equal :name => 'Captain'
+ end
+
+ it 'instantiates with hash with nil name' do
+ widget = Widget.new
+ widget.to_hash.must_equal :name => nil
+ end
+
+ it 'adds attribute, registers updated attributes as dirty' do
+ widget = Widget.new
+ widget.unsaved_attributes.empty?.must_equal true
+ widget.unsaved_attributes?.must_equal false
+
+ widget.name = 'Captain'
+ widget.name.must_equal 'Captain'
+ widget.to_hash[:name].must_equal 'Captain'
+ end
+
+end
@@ -0,0 +1,7 @@
+require File.join '.', File.dirname(__FILE__), '..', '..', 'env.rb'
+
+describe Geoloqi::Layer do
+ it 'creates a new layer' do
+ puts "Not yet"
+ end
+end

0 comments on commit 3ca5c9a

Please sign in to comment.