Module: FFI::DRY::StructHelper::ClassMethods
- Defined in:
- lib/ffi/dry.rb
Overview
Contains dsl_layout and some support methods that an ‘includee’ of DryStructHelper will have available as class methods.
Instance Method Summary collapse
- #_class_meths_from_dsl_metadata(meta) ⇒ Object
- #define_field_accessor(name, &block) ⇒ Object
-
#dsl_layout(&block) ⇒ Object
This passes a block to an instance of DSLStructLayoutBuilder, allowing for a more declarative syntax with additional metadata to be included.
-
#dsl_metadata ⇒ Object
returns the structure metadata for this class based on the dsl_layout definitions.
Instance Method Details
#_class_meths_from_dsl_metadata(meta) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/ffi/dry.rb', line 208 def () (@dsl_metadata = ).each do |spec| name = spec[:name] ftype = spec[:type] if p=spec[:p_struct] and p.kind_of?(Class) define_field_accessor(:"#{name}") do p.new(self[name]) unless self[name].null? end else define_field_accessor(:"#{name}") { self[name] } end define_field_accessor(:"#{name}=") {|val| self[name]=val } end end |
#define_field_accessor(name, &block) ⇒ Object
178 179 180 181 182 183 184 185 |
# File 'lib/ffi/dry.rb', line 178 def define_field_accessor name, &block if instance_methods.include?("#{name}") warn "WARNING: The name '#{name}' is in use for class #{self} "+ "Skipping automatic method creation in dsl_layout block." else define_method name, &block end end |
#dsl_layout(&block) ⇒ Object
This passes a block to an instance of DSLStructLayoutBuilder, allowing for a more declarative syntax with additional metadata to be included.
dsl_layout() a replacement to layout() and stores the dsl_metadata gathered about structure members locally and automatically creates accessor methods for each field in the structure.
NOTE if a structure field name conflicts with another instance method already defined in the class, the relevant accessor method is not created and a warning is issued. This does not apply to methods defined after the dsl_layout block is called. In other words this does not affect the overriding of accessor methods in any way.
199 200 201 202 203 204 205 206 |
# File 'lib/ffi/dry.rb', line 199 def dsl_layout &block builder = DSLStructLayoutBuilder.new(self) builder.instance_eval(&block) @layout = self.layout(*(builder.__layout_args)) @size = @layout.size ( builder. ) return @layout end |
#dsl_metadata ⇒ Object
returns the structure metadata for this class based on the dsl_layout definitions
174 175 176 |
# File 'lib/ffi/dry.rb', line 174 def @dsl_metadata end |