Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanstout committed Feb 1, 2014
1 parent abe9ea3 commit 922c2eb
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 54 deletions.
57 changes: 3 additions & 54 deletions lib/volt/models/model.rb
Expand Up @@ -2,6 +2,7 @@
require 'volt/models/array_model'
require 'volt/models/model_helpers'
require 'volt/reactive/object_tracking'
require 'volt/models/model_hash_behaviour'

class NilMethodCall < NoMethodError
def true?
Expand All @@ -18,22 +19,11 @@ class Model
include ModelWrapper
include ObjectTracking
include ModelHelpers
include ModelHashBehaviour

attr_accessor :attributes
attr_reader :parent, :path, :persistor, :options

def nil?
attributes.nil?
end

def false?
attributes.false?
end

def true?
attributes.true?
end

def initialize(attributes={}, options={})
@options = options
@parent = options[:parent]
Expand Down Expand Up @@ -72,32 +62,6 @@ def event_removed(event, no_more_events)
@persistor.event_removed(event, no_more_events) if @persistor
end


tag_method(:delete) do
destructive!
end
def delete(*args)
__clear_element(args[0])
attributes.delete(*args)
trigger_by_attribute!('changed', args[0])

@persistor.removed(args[0]) if @persistor
end

tag_method(:clear) do
destructive!
end
def clear
attributes.each_pair do |key,value|
__clear_element(key)
end

attributes.clear
trigger!('changed')

@persistor.removed(nil) if @persistor
end

tag_all_methods do
pass_reactive! do |method_name|
method_name[0] == '_' && method_name[-1] == '='
Expand Down Expand Up @@ -240,22 +204,7 @@ def <<(value)

def inspect
"<#{self.class.to_s} #{attributes.inspect}>"
end

def [](val)
raise "Models do not support hash style lookup. Hashes inserted into other models are converted to models, see https://github.com/voltrb/volt#automatic-model-conversion"
end

# Convert the model to a hash all of the way down.
def to_h
hash = {}
attributes.each_pair do |key, value|
hash[key] = deep_unwrap(value)
end

return hash
end

end

private
# Clear the previous value and assign a new one
Expand Down
66 changes: 66 additions & 0 deletions lib/volt/models/model_hash_behaviour.rb
@@ -0,0 +1,66 @@
# Contains all of the methods on a model that make it behave like a hash.
# Moving this into a module cleans up the main Model class for things that
# make it behave like a model.
module ModelHashBehaviour
def self.included(base)
# In modules, since we need to tag on the main class, we setup the
# tags with included.
base.tag_method(:delete) do
destructive!
end

base.tag_method(:clear) do
destructive!
end
end


def nil?
attributes.nil?
end

def false?
attributes.false?
end

def true?
attributes.true?
end

def delete(*args)
__clear_element(args[0])
attributes.delete(*args)
trigger_by_attribute!('changed', args[0])

@persistor.removed(args[0]) if @persistor
end


def clear
attributes.each_pair do |key,value|
__clear_element(key)
end

attributes.clear
trigger!('changed')

@persistor.removed(nil) if @persistor
end


# Convert the model to a hash all of the way down.
def to_h
hash = {}
attributes.each_pair do |key, value|
hash[key] = deep_unwrap(value)
end

return hash
end


def [](val)
raise "Models do not support hash style lookup. Hashes inserted into other models are converted to models, see https://github.com/voltrb/volt#automatic-model-conversion"
end

end

0 comments on commit 922c2eb

Please sign in to comment.