class BinData::DSLMixin::DSLBigAndLittleEndianHandler
Handles the :big_and_little endian option. This option creates two subclasses, each handling :big or :little endian.
Public Class Methods
class_with_endian(class_name, endian)
click to toggle source
# File lib/bindata/dsl.rb, line 332 def class_with_endian(class_name, endian) hints = { :endian => endian, :search_prefix => class_name.dsl_parser.search_prefix, } RegisteredClasses.lookup(class_name, hints) end
create_subclasses_with_endian(bnl_class)
click to toggle source
# File lib/bindata/dsl.rb, line 278 def create_subclasses_with_endian(bnl_class) instance_eval "class ::#{bnl_class}Be < ::#{bnl_class}; endian :big; end" instance_eval "class ::#{bnl_class}Le < ::#{bnl_class}; endian :little; end" end
delegate_field_creation(bnl_class)
click to toggle source
# File lib/bindata/dsl.rb, line 299 def delegate_field_creation(bnl_class) endian_classes = { :big => class_with_endian(bnl_class, :big), :little => class_with_endian(bnl_class, :little), } parser = bnl_class.dsl_parser parser.define_singleton_method(:parse_and_append_field) do |*args, &block| endian_classes[:big].send(*args, &block) endian_classes[:little].send(*args, &block) end end
fixup_subclass_hierarchy(bnl_class)
click to toggle source
# File lib/bindata/dsl.rb, line 312 def fixup_subclass_hierarchy(bnl_class) parent = bnl_class.superclass if obj_attribute(parent, :endian) == :big_and_little be_subclass = class_with_endian(bnl_class, :big) be_parent = class_with_endian(parent, :big) be_fields = obj_attribute(be_parent, :fields) le_subclass = class_with_endian(bnl_class, :little) le_parent = class_with_endian(parent, :little) le_fields = obj_attribute(le_parent, :fields) be_subclass.dsl_parser.define_singleton_method(:parent_fields) do be_fields end le_subclass.dsl_parser.define_singleton_method(:parent_fields) do le_fields end end end
handle(bnl_class)
click to toggle source
# File lib/bindata/dsl.rb, line 266 def handle(bnl_class) make_class_abstract(bnl_class) create_subclasses_with_endian(bnl_class) override_new_in_class(bnl_class) delegate_field_creation(bnl_class) fixup_subclass_hierarchy(bnl_class) end
make_class_abstract(bnl_class)
click to toggle source
# File lib/bindata/dsl.rb, line 274 def make_class_abstract(bnl_class) bnl_class.send(:unregister_self) end
obj_attribute(obj, attr)
click to toggle source
# File lib/bindata/dsl.rb, line 340 def obj_attribute(obj, attr) obj.dsl_parser.send(attr) end
override_new_in_class(bnl_class)
click to toggle source
Calls superclass method
# File lib/bindata/dsl.rb, line 283 def override_new_in_class(bnl_class) endian_classes = { :big => class_with_endian(bnl_class, :big), :little => class_with_endian(bnl_class, :little), } bnl_class.define_singleton_method(:new) do |*args| if self == bnl_class _, options, _ = arg_processor.separate_args(self, args) delegate = endian_classes[options[:endian]] return delegate.new(*args) if delegate end super(*args) end end