lib/gorillib/builder.rb in gorillib-0.4.0pre vs lib/gorillib/builder.rb in gorillib-0.4.1pre

- old
+ new

@@ -1,17 +1,14 @@ require 'gorillib/string/simple_inflector' require 'gorillib/model' +require 'gorillib/collection/model_collection' module Gorillib module Builder extend Gorillib::Concern include Gorillib::Model - def initialize(attrs={}, &block) - receive!(attrs, &block) - end - def receive!(*args, &block) super(*args) if block_given? (block.arity == 1) ? block.call(self) : self.instance_eval(&block) end @@ -101,24 +98,21 @@ end module ClassMethods include Gorillib::Model::ClassMethods - def field(field_name, type, options={}) - super(field_name, type, {:field_type => ::Gorillib::Builder::GetsetField}.merge(options)) + def magic(field_name, type, options={}) + field(field_name, type, {:field_type => ::Gorillib::Builder::GetsetField}.merge(options)) end def member(field_name, type, options={}) field(field_name, type, {:field_type => ::Gorillib::Builder::MemberField}.merge(options)) end def collection(field_name, item_type, options={}) - field(field_name, Gorillib::Collection, { + field(field_name, Gorillib::ModelCollection, { :item_type => item_type, :field_type => ::Gorillib::Builder::CollectionField}.merge(options)) end - def simple_field(field_name, type, options={}) - field(field_name, type, {:field_type => ::Gorillib::Model::Field}.merge(options)) - end protected def define_attribute_getset(field) field_name = field.name; type = field.type @@ -176,28 +170,28 @@ module FancyBuilder extend Gorillib::Concern include Gorillib::Builder included do |base| - base.field :name, Symbol + base.magic :name, Symbol end module ClassMethods include Gorillib::Builder::ClassMethods def belongs_to(field_name, type, options={}) - field = field(field_name, type, {:field_type => ::Gorillib::Builder::MemberField }.merge(options)) + field = member(field_name, type) define_meta_module_method "#{field.name}_name" do val = getset_member(field) or return nil val.name end field end def option(field_name, options={}) type = options.delete(:type){ Whatever } - field(field_name, type, {:field_type => ::Gorillib::Builder::GetsetField }.merge(options)) + magic(field_name, type) end def collects(type, clxn_name) type_handle = type.handle define_meta_module_method type_handle do |item_name, attrs={}, options={}, &block| @@ -241,10 +235,10 @@ @singular_name ||= Gorillib::Inflector.singularize(name.to_s).to_sym end def inscribe_methods(model) item_type = self.item_type - self.default = ->{ Gorillib::Collection.new(item_type) } + self.default = ->{ Gorillib::ModelCollection.new(nil, item_type) } raise "Plural and singular names must differ: #{self.plural_name}" if (singular_name == plural_name) # @visibilities[:writer] = false model.__send__(:define_attribute_reader, self.name, self.type, visibility(:reader)) model.__send__(:define_attribute_tester, self.name, self.type, visibility(:tester))