Class: DataBindings::Generator
- Inherits:
-
Object
- Object
- DataBindings::Generator
- Defined in:
- lib/data_bindings/generator.rb
Overview
This is the class the handles registering readers, writers, adapters and types.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#strict ⇒ Object
(also: #strict?)
Enable/disable strict mode.
Instance Method Summary collapse
- #binding_class(cls) ⇒ Object
- #class_for(name, superclass = Object, &blk) ⇒ Object
-
#for_native(name, &blk) ⇒ Object
Defines a native constructor.
-
#get_adapter(name) ⇒ Object
Retrieves an adapter.
-
#get_type(name) ⇒ Proc
Retrieves an object type.
-
#initialize ⇒ Generator
constructor
A new instance of Generator.
- #native_constructors ⇒ Object
-
#reader(name) {|*Object| ... } ⇒ Object
Defines a reader.
-
#register(name, cls) ⇒ Object
Registers an adapter.
-
#reset! ⇒ Object
Resets the generator to a blank state.
-
#type(name, &blk) ⇒ Object
Defines an object type.
-
#write(method_name, obj, *args, &blk) ⇒ Object
Passes off writing of an object through a specific writer.
-
#write_targets(name) ⇒ Boolean
Tests if a specific type of writer is supported.
-
#writer(name) {|*Object| ... } ⇒ Object
Defines a writer.
Constructor Details
#initialize ⇒ Generator
Returns a new instance of Generator.
8 9 10 |
# File 'lib/data_bindings/generator.rb', line 8 def initialize reset! end |
Instance Attribute Details
#strict ⇒ Object Also known as: strict?
Enable/disable strict mode
5 6 7 |
# File 'lib/data_bindings/generator.rb', line 5 def strict @strict end |
Instance Method Details
#binding_class(cls) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/data_bindings/generator.rb', line 26 def binding_class(cls) mod = @writer_module @binding_classes[cls] ||= begin Class.new(cls) do include mod end end end |
#class_for(name, superclass = Object, &blk) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/data_bindings/generator.rb', line 100 def class_for(name, superclass = Object, &blk) cls = Class.new(superclass, &blk) for_native(name) { |props| inst = cls.allocate props.each do |k, v| inst.instance_variable_set(:"@#{k}", v) end inst.send(:initialize) inst } end |
#for_native(name, &blk) ⇒ Object
Defines a native constructor
92 93 94 |
# File 'lib/data_bindings/generator.rb', line 92 def for_native(name, &blk) native_constructors[name] = blk end |
#get_adapter(name) ⇒ Object
Retrieves an adapter
38 39 40 |
# File 'lib/data_bindings/generator.rb', line 38 def get_adapter(name) @adapter_classes[name] or raise UnknownAdapterError, "Could not find adapter #{name.inspect}" end |
#get_type(name) ⇒ Proc
Retrieves an object type
22 23 24 |
# File 'lib/data_bindings/generator.rb', line 22 def get_type(name) @types[name] end |
#native_constructors ⇒ Object
96 97 98 |
# File 'lib/data_bindings/generator.rb', line 96 def native_constructors @native_constructors ||= {} end |
#reader(name) {|*Object| ... } ⇒ Object
Defines a reader
45 46 47 48 |
# File 'lib/data_bindings/generator.rb', line 45 def reader(name, &blk) @reader_module.define_singleton_method(name, &blk) build! end |
#register(name, cls) ⇒ Object
Registers an adapter
74 75 76 77 |
# File 'lib/data_bindings/generator.rb', line 74 def register(name, cls) @adapters[name] = cls build! end |
#reset! ⇒ Object
Resets the generator to a blank state
80 81 82 83 84 85 86 87 88 |
# File 'lib/data_bindings/generator.rb', line 80 def reset! @reader_module = Module.new { extend Readers } @writer_module = Module.new { extend Writers; include WritingInterceptor } @strict = false @types = {} @adapters = {} @adapter_classes = {} @binding_classes = {} end |
#type(name, &blk) ⇒ Object
Defines an object type
15 16 17 |
# File 'lib/data_bindings/generator.rb', line 15 def type(name, &blk) @types[name] = blk end |
#write(method_name, obj, *args, &blk) ⇒ Object
Passes off writing of an object through a specific writer.
60 61 62 |
# File 'lib/data_bindings/generator.rb', line 60 def write(method_name, obj, *args, &blk) @writer_module.send(method_name, obj, *args, &blk) end |
#write_targets(name) ⇒ Boolean
Tests if a specific type of writer is supported
67 68 69 |
# File 'lib/data_bindings/generator.rb', line 67 def write_targets(name) target, format = name.to_s.split(/_/, 2) end |
#writer(name) {|*Object| ... } ⇒ Object
Defines a writer
53 54 55 |
# File 'lib/data_bindings/generator.rb', line 53 def writer(name, &blk) @writer_module.define_singleton_method(name, &blk) end |