Skip to content

Commit

Permalink
default_field command means when you assign to a field that's a struc…
Browse files Browse the repository at this point in the history
…ture,

the assignment proxies to that field, which is way more useful than I'm
making it sound
  • Loading branch information
test2049 committed Nov 26, 2008
1 parent 238b2a9 commit 537d5da
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
29 changes: 29 additions & 0 deletions ruckus/structure/defaults.rb
@@ -1,5 +1,9 @@
module Ruckus
module StructureDefaultValues
def self.included(klass)
klass.extend(ClassMethods)
end

def template_entry_added_hook(obj)
obj.instance_variables.grep(/^@with_.*/).each do |v|
v =~ /@with_(.*)/
Expand All @@ -8,5 +12,30 @@ def template_entry_added_hook(obj)
end
super
end

def method_missing_hook(meth, args)
m = meth.to_s
setter = (m[-1].chr == "=") ? true : false
m = m[0..-2] if setter
if setter and (field = self[m.intern])
if(field.kind_of? Structure)
if((deft = field.instance_variable_get :@default_field))
field.send(deft.to_s + "=", args[0])
return false
else
puts "WARNING: attempt to set structure field with no default_field declared"
end
end
end
true
end

module ClassMethods
def default_field(f)
self.initializers << lambda do
@default_field = f
end
end
end
end
end
33 changes: 33 additions & 0 deletions ruckus/structure/searchmods.rb
@@ -0,0 +1,33 @@
module Ruckus
module StructureSearchModules
def self.included(klass)
klass.class_eval {
class_inheritable_array :search_modules
write_inheritable_array :search_modules, []
}

klass.extend(ClassMethods)
end

module ClassMethods
def derive_search_module
if self.search_modules.empty?
return Ruckus
else
mod = Module.new
self.search_modules.each do |m|
mod.module_eval "include #{ m.to_s.class_name }"
end
mod.module_eval "include Ruckus"
return mod
end
end

def search_module(*args)
args.each do |m|
self.search_modules << m
end
end
end
end
end

0 comments on commit 537d5da

Please sign in to comment.