Skip to content

Commit

Permalink
Switch to composition
Browse files Browse the repository at this point in the history
  • Loading branch information
kyledrake committed Jan 22, 2012
1 parent fa52994 commit 3ca5c9a
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 43 deletions.
105 changes: 63 additions & 42 deletions lib/geoloqi/model.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Geoloqi
module Model

module Model
class Property
attr_accessor :name, :type
def initialize(name, type)
Expand All @@ -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
3 changes: 2 additions & 1 deletion lib/geoloqi/models/layer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Geoloqi
class Layer < Model::Base
class Layer
include Model
property :name, :string
property :key, :string
end
Expand Down
35 changes: 35 additions & 0 deletions spec/geoloqi/model_spec.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions spec/geoloqi/models/layer_spec.rb
Original file line number Diff line number Diff line change
@@ -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.