Class: DataBindings::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/data_bindings/generator.rb

Overview

This is the class the handles registering readers, writers, adapters and types.

Direct Known Subclasses

DefaultGenerator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGenerator

Returns a new instance of Generator.



8
9
10
# File 'lib/data_bindings/generator.rb', line 8

def initialize
  reset!
end

Instance Attribute Details

#strictObject 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

Parameters:

  • name (Symbol)

    The name of the type to create a constructor for



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

Parameters:

  • name (Symbol)

    The name of the adapter

Returns:

  • (Object)

    The 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

Parameters:

  • name (Symbol)

    The name of the type

Returns:

  • (Proc)

    The body of the type



22
23
24
# File 'lib/data_bindings/generator.rb', line 22

def get_type(name)
  @types[name]
end

#native_constructorsObject



96
97
98
# File 'lib/data_bindings/generator.rb', line 96

def native_constructors
  @native_constructors ||= {}
end

#reader(name) {|*Object| ... } ⇒ Object

Defines a reader

Parameters:

  • name (Symbol)

    The name of the reader

Yields:

  • (*Object)

    All arguments passed to the method used to invoke this 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

Parameters:

  • name (Symbol)

    The name of the adapter

  • The (Object)

    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

Parameters:

  • name (Symbol)

    The name of the type

See Also:



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.

Parameters:

  • method_name (Symbol)

    The method name to be invoked on the writer

  • data (String)

    The data to be written



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

Parameters:

  • name (Symbol)

    The name of the writer to test

Returns:

  • (Boolean)


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

Parameters:

  • name (Symbol)

    The name of the writer

Yields:

  • (*Object)

    All arguments passed to the method used to invoke this writer



53
54
55
# File 'lib/data_bindings/generator.rb', line 53

def writer(name, &blk)
  @writer_module.define_singleton_method(name, &blk)
end