lib/gorillib/builder.rb in gorillib-0.4.1pre vs lib/gorillib/builder.rb in gorillib-0.4.2pre
- old
+ new
@@ -5,16 +5,16 @@
module Gorillib
module Builder
extend Gorillib::Concern
include Gorillib::Model
+ # @return [Object, nil] the return value of the block, or nil if no block given
def receive!(*args, &block)
super(*args)
if block_given?
(block.arity == 1) ? block.call(self) : self.instance_eval(&block)
end
- self
end
def getset(field, *args, &block)
ArgumentError.check_arity!(args, 0..1)
if args.empty?
@@ -33,12 +33,12 @@
write_attribute(field.name, val)
else
val = read_attribute(field.name)
if val.present?
# existing item: update it with args and block
- val.receive!(*args, &block) if args.present?
- elsif attrs.blank?
+ val.receive!(*args, &block) if args.present? or block_given?
+ elsif attrs.blank? and not block_given?
# missing item (read): return nil
return nil
else
# missing item (write): construct item and add to collection
options = args.extract_options!.merge(:owner => self)
@@ -70,11 +70,13 @@
def get_collection_item(plural_name, item_key)
collection_of(plural_name)[item_key]
end
def set_collection_item(plural_name, item_key, item)
- collection_of(plural_name)[item_key] = item
+ collection = collection_of(plural_name)
+ collection[item_key] = item
+ collection[item_key]
end
def has_collection_item?(plural_name, item_key)
collection_of(plural_name).include?(item_key)
end
@@ -82,17 +84,15 @@
def key_method
:name
end
def to_key
- self.read_attribute(key_method)
+ self.send(key_method)
end
- def inspect_helper(detailed, attrs)
- attrs.delete(:owner)
- # detailed ? str : ([str[0..-2], " #{to_key}>"].join)
- str = super(detailed, attrs)
+ def to_inspectable
+ super.tap{|attrs| attrs.delete(:owner) }
end
def collection_of(plural_name)
self.read_attribute(plural_name)
end
@@ -104,14 +104,15 @@
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
+
+ # FIXME: this interface is borked -- it should not take the item_type in the second slot.
def collection(field_name, item_type, options={})
- field(field_name, Gorillib::ModelCollection, {
- :item_type => item_type,
- :field_type => ::Gorillib::Builder::CollectionField}.merge(options))
+ super(field_name, Gorillib::ModelCollection, {
+ :item_type => item_type, :field_type => ::Gorillib::Builder::GetsetCollectionField }.merge(options))
end
protected
def define_attribute_getset(field)
@@ -139,24 +140,10 @@
getset_collection_item(field, *args, &block)
rescue StandardError => err ; err.polish("#{self.class}.#{field_name} c[#{item_type}] on #{args}'") rescue nil ; raise ; end
end
end
- def define_collection_receiver(field)
- plural_name = field.name; item_type = field.item_type; field_type = field.type
- define_meta_module_method("receive_#{plural_name}", true) do |coll, &block|
- begin
- if coll.is_a?(field_type)
- write_attribute(plural_name, coll)
- else
- read_attribute(plural_name).receive!(coll, &block)
- end
- self
- rescue StandardError => err ; err.polish("#{self.class} #{plural_name} c[#{item_type}] on #{args}'") rescue nil ; raise ; end
- end
- end
-
def define_collection_tester(field)
plural_name = field.plural_name
define_meta_module_method("has_#{field.singular_name}?", field.visibility(:collection_tester)) do |item_key|
begin
collection_of(plural_name).include?(item_key)
@@ -221,25 +208,22 @@
model.__send__(:define_attribute_tester, self.name, self.type, visibility(:tester))
model.__send__(:define_attribute_receiver, self.name, self.type, visibility(:receiver))
end
end
- class CollectionField < Gorillib::Model::Field
+ class GetsetCollectionField < ::Gorillib::Model::SimpleCollectionField
field :singular_name, Symbol, :default => ->{ Gorillib::Inflector.singularize(name.to_s).to_sym }
- field :item_type, Class, :default => Whatever
self.visibilities = visibilities.merge(:writer => false, :tester => false,
:collection_getset => :public, :collection_tester => true)
alias_method :plural_name, :name
def singular_name
@singular_name ||= Gorillib::Inflector.singularize(name.to_s).to_sym
end
def inscribe_methods(model)
- item_type = self.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))
@@ -247,8 +231,9 @@
model.__send__(:define_collection_receiver, self)
model.__send__(:define_collection_getset, self)
model.__send__(:define_collection_tester, self)
end
end
+ CollectionField = GetsetCollectionField
end
end