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
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/ffi/dry.rb', line 190 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
160 161 162 163 164 165 166 167 |
# File 'lib/ffi/dry.rb', line 160 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.
181 182 183 184 185 186 187 188 |
# File 'lib/ffi/dry.rb', line 181 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
156 157 158 |
# File 'lib/ffi/dry.rb', line 156 def @dsl_metadata end |