require 'active_mocker/mock_requires' Object.send(:remove_const, "<%= class_name %>") if ActiveMocker.class_exists?("<%= class_name %>") class <%= class_name %> < ::ActiveHash::Base include ActiveMocker::ActiveHash::ARApi include ActiveMocker::MockInstanceMethods extend ActiveMocker::MockClassMethods def initialize(attributes={}, &block) @attributes = HashWithIndifferentAccess.new(<%= default_attributes %>) @associations = HashWithIndifferentAccess.new(<%= associations %>) super(attributes, &block) end def self.column_names <%= attribute_names %> end def self.attribute_names @attribute_names = <%= attribute_names %> end ################################## # Attributes getter/setters # ################################## <% attributes.each do |meth| %> def <%= meth.name %> @attributes['<%= meth.name %>'] end def <%= meth.name %>=(val) type = (types[:<%= meth.name %>] ||= Virtus::Attribute.build(<%= meth.type %>)) @attributes['<%= meth.name %>'] = type.coerce(val) end <% end %> ################################## # Association getter/setters # ################################## def self.association_names @association_names = <%= association_names.map { |a| a.to_sym } %> end <% single_associations.each do |meth| %> def <%= meth %> associations['<%= meth %>'] end def <%= meth %>=(val) associations['<%= meth %>'] = val end <% end -%> <% collection_associations.each do |meth| %> def <%= meth %> associations['<%= meth %>'] ||= ActiveMocker::Collection::Association.new end def <%= meth %>=(val) associations['<%= meth %>'] = ActiveMocker::Collection::Association.new(val) end <% end -%> ################################## # Model Methods getter/setters # ################################## def self.model_instance_methods @model_instance_methods ||= <%= model_instance_methods %> end def self.model_class_methods @model_class_methods ||= <%= model_class_methods %> end def self.clear_mock @model_class_methods, @model_instance_methods = nil, nil delete_all end <% instance_methods.each do |method| %> def <%= method.method %>(<%= method.params %>) block = model_instance_methods['<%= method.method %>'] self.class.is_implemented(block, '#<%= method.method %>') instance_exec(*[<%= method.params_pass %>], &block) end <% end -%> <% class_methods.each do |method| %> def self.<%= method.method %>(<%= method.params %>) block = model_class_methods['<%= method.method %>'] is_implemented(block, '::<%= method.method %>') instance_exec(*[<%= method.params_pass %>], &block) end <% end -%> end