Module: Card::Set
- Defined in:
- lib/card/set.rb
Defined Under Namespace
Modules: Format
Constant Summary collapse
- @@modules =
{ :base=>[], :base_format=>{}, :nonbase=>{}, :nonbase_format=>{} }
Class Method Summary collapse
- .clean_empty_module_from_hash(hash) ⇒ Object
- .clean_empty_modules ⇒ Object
-
.extended(mod) ⇒ Object
each set file calls ‘extend Card::Set` when loaded.
- .process_base_module_list(list, klass) ⇒ Object
-
.process_base_modules ⇒ Object
“base modules” are modules that are permanently included on the Card or Format class “nonbase modules” are included dynamically on singleton_classes.
- .register_set(set_module) ⇒ Object
- .write_tmp_file(set_pattern, anchor, from_file, seq) ⇒ Object
Instance Method Summary collapse
- #all_set? ⇒ Boolean
- #card_accessor(*args) ⇒ Object
- #card_reader(*args) ⇒ Object
- #card_writer(*args) ⇒ Object
- #event(event, opts = {}, &final) ⇒ Object
- #format(format = nil, &block) ⇒ Object
- #register_set_format(format_class, mod) ⇒ Object
- #shortname ⇒ Object
- #view(*args, &block) ⇒ Object
Class Method Details
.clean_empty_module_from_hash(hash) ⇒ Object
252 253 254 255 256 257 |
# File 'lib/card/set.rb', line 252 def clean_empty_module_from_hash hash hash.each do |mod_name, modlist| modlist.delete_if { |x| x.instance_methods.empty? } hash.delete mod_name if modlist.empty? end end |
.clean_empty_modules ⇒ Object
245 246 247 248 249 250 |
# File 'lib/card/set.rb', line 245 def clean_empty_modules clean_empty_module_from_hash modules[ :nonbase ] modules[ :nonbase_format ].values.each do |hash| clean_empty_module_from_hash hash end end |
.extended(mod) ⇒ Object
each set file calls ‘extend Card::Set` when loaded
185 186 187 |
# File 'lib/card/set.rb', line 185 def extended mod register_set mod end |
.process_base_module_list(list, klass) ⇒ Object
234 235 236 237 238 239 240 241 242 243 |
# File 'lib/card/set.rb', line 234 def process_base_module_list list, klass list.each do |mod| if mod.instance_methods.any? klass.send :include, mod end if class_methods = mod.const_get_if_defined( :ClassMethods ) klass.send :extend, class_methods end end end |
.process_base_modules ⇒ Object
“base modules” are modules that are permanently included on the Card or Format class “nonbase modules” are included dynamically on singleton_classes
225 226 227 228 229 230 231 232 |
# File 'lib/card/set.rb', line 225 def process_base_modules process_base_module_list modules[:base], Card modules[:base_format].each do |format_class, modules_list| process_base_module_list modules_list, format_class end modules.delete :base modules.delete :base_format end |
.register_set(set_module) ⇒ Object
189 190 191 192 193 194 195 196 |
# File 'lib/card/set.rb', line 189 def register_set set_module if set_module.all_set? modules[ :base ] << set_module else modules[ :nonbase ][ set_module.shortname ] ||= [] modules[ :nonbase ][ set_module.shortname ] << set_module end end |
.write_tmp_file(set_pattern, anchor, from_file, seq) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/card/set.rb', line 198 def write_tmp_file set_pattern, anchor, from_file, seq # FIXME - this does not properly handle anchorless sets # There are special hacks for *all, but others (like *rstar) will not be found by # include_set_modules, which will look for Card::Set::Rstar, not Card::Set::Rstar::Blah to_file = "#{Wagn.paths['tmp/set'].first}/#{set_pattern}/#{seq}-#{anchor}.rb" file_content = "# -*- encoding : utf-8 -*-\nclass Card; module Set; module \#{set_pattern.camelize}; module \#{anchor.camelize}\nextend Card::Set\n# ~~~~~~~~~~~ above autogenerated; below pulled from \#{from_file} ~~~~~~~~~~~\n\n\#{ File.read from_file }\n\n# ~~~~~~~~~~~ below autogenerated; above pulled from \#{from_file} ~~~~~~~~~~~\nend;end;end;end\n" File.write to_file, file_content to_file end |
Instance Method Details
#all_set? ⇒ Boolean
283 284 285 |
# File 'lib/card/set.rb', line 283 def all_set? name =~ /^Card::Set::All::/ end |
#card_accessor(*args) ⇒ Object
155 156 157 158 |
# File 'lib/card/set.rb', line 155 def card_accessor *args = args. add_traits args, .merge( :reader=>true, :writer=>true ) end |
#card_reader(*args) ⇒ Object
160 161 162 163 |
# File 'lib/card/set.rb', line 160 def card_reader *args = args. add_traits args, .merge( :reader=>true ) end |
#card_writer(*args) ⇒ Object
165 166 167 168 |
# File 'lib/card/set.rb', line 165 def card_writer *args = args. add_traits args, .merge( :writer=>true ) end |
#event(event, opts = {}, &final) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/card/set.rb', line 127 def event event, opts={}, &final opts[:on] = [:create, :update ] if opts[:on] == :save Card.define_callbacks event class_eval do final_method = "#{event}_without_callbacks" #should be private? define_method final_method, &final define_method event do run_callbacks event do Wagn.with_logging self.name, :event, event, opts do send final_method end end end end set_event_callbacks event, opts end |
#format(format = nil, &block) ⇒ Object
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/card/set.rb', line 109 def format format=nil, &block klass = Card::Format.format_class_name format # format class name, eg. HtmlFormat mod = const_get_or_set klass do # called on current set module, eg Card::Set::Type::Pointer m = Module.new # yielding set format module, eg Card::Set::Type::Pointer::HtmlFormat register_set_format Card.const_get(klass), m m.extend Card::Set::Format m end mod.class_eval &block end |
#register_set_format(format_class, mod) ⇒ Object
262 263 264 265 266 267 268 269 270 271 |
# File 'lib/card/set.rb', line 262 def register_set_format format_class, mod if self.all_set? modules[ :base_format ][ format_class ] ||= [] modules[ :base_format ][ format_class ] << mod else format_hash = modules[ :nonbase_format ][ format_class ] ||= {} format_hash[ shortname ] ||= [] format_hash[ shortname ] << mod end end |
#shortname ⇒ Object
273 274 275 276 277 278 279 280 281 |
# File 'lib/card/set.rb', line 273 def shortname parts = name.split '::' first = 2 # shortname eliminates Card::Set set_class = Card::SetPattern.find parts[first].underscore last = first + ( set_class.anchorless? ? 0 : 1 ) #FIXME - handle multi-anchor sets like type_plus_right parts[first..last].join '::' end |
#view(*args, &block) ⇒ Object
120 121 122 123 124 |
# File 'lib/card/set.rb', line 120 def view *args, &block format do view *args, &block end end |