Class: A4Tools::ObjectBuilder

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.description_textObject (readonly)

Returns the value of attribute description_text.



4
5
6
# File 'lib/object_builder/object_builder.rb', line 4

def description_text
  @description_text
end

.generate_blockObject (readonly)

Returns the value of attribute generate_block.



4
5
6
# File 'lib/object_builder/object_builder.rb', line 4

def generate_block
  @generate_block
end

.idObject

Returns the value of attribute id.



5
6
7
# File 'lib/object_builder/object_builder.rb', line 5

def id
  @id
end

.is_cachedObject (readonly)

Returns the value of attribute is_cached.



4
5
6
# File 'lib/object_builder/object_builder.rb', line 4

def is_cached
  @is_cached
end

Class Method Details

.[](identifier) ⇒ Object



83
84
85
# File 'lib/object_builder/object_builder.rb', line 83

def [](identifier)
  @definitions[identifier.to_sym]
end

.[]=(identifier, defn) ⇒ Object



87
88
89
# File 'lib/object_builder/object_builder.rb', line 87

def []=(identifier, defn)
  @definitions[identifier.to_sym] = defn
end

.buildersObject



64
65
66
# File 'lib/object_builder/object_builder.rb', line 64

def builders
  @definitions.values
end

.builders_for_name(name) ⇒ Object



68
69
70
71
72
# File 'lib/object_builder/object_builder.rb', line 68

def builders_for_name(name)
  @definitions.values.select do |defn|
    defn.name_matches?(name)
  end
end

.cache(is_cached) ⇒ Object



105
106
107
# File 'lib/object_builder/object_builder.rb', line 105

def cache(is_cached)
  @is_cached = is_cached
end

.define_with_directory(directory) ⇒ Object



32
33
34
# File 'lib/object_builder/object_builder.rb', line 32

def define_with_directory(directory)
  Dir[File.join(directory, "*")].map { |file| define_with_filename(file) }
end

.define_with_filename(filename) ⇒ Object



36
37
38
39
40
41
# File 'lib/object_builder/object_builder.rb', line 36

def define_with_filename(filename)
  contents = IO.read(filename)
  id = File.basename(filename).gsub(/\..*$/, "").to_sym
  return define_with_script(contents, id) if filename.end_with? ".rb"
  define_with_json(contents, id)
end

.define_with_json(string, id = nil, description = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/object_builder/object_builder.rb', line 50

def define_with_json(string, id=nil, description=nil)
  object = symbolify(JSON.parse(string))
  description ||= object[:___description] || "Supplied from JSON"
  definition = Class.new(A4Tools::ObjectBuilder) do
    generate { self.class.instance_variable_get("@obj") }
  end
  
  object.delete(:__description)
  definition.class_eval("description \"#{description}\"")
  definition.instance_variable_set("@obj", object)
  definition.id = id.to_sym
  definition.new
end

.define_with_script(script, id) ⇒ Object



43
44
45
46
47
48
# File 'lib/object_builder/object_builder.rb', line 43

def define_with_script(script, id)
  definition = Class.new(A4Tools::ObjectBuilder) {}
  definition.id = id
  definition.class_eval(script, id.to_s)
  definition.new
end

.description(desc) ⇒ Object



101
102
103
# File 'lib/object_builder/object_builder.rb', line 101

def description(desc)
  @description_text = desc
end

.generate(&block) ⇒ Object



109
110
111
# File 'lib/object_builder/object_builder.rb', line 109

def generate(&block)
  @generate_block = block
end

.identifier(id) ⇒ Object

DSL stuff for defining items



97
98
99
# File 'lib/object_builder/object_builder.rb', line 97

def identifier(id)
  @id = id
end

.identifiers_like(id) ⇒ Object



91
92
93
# File 'lib/object_builder/object_builder.rb', line 91

def identifiers_like(id)
  @definitions.keys.select { |key| key.to_s.start_with? id.to_s }
end

.load_object(obj, id, description) ⇒ Object



27
28
29
30
# File 'lib/object_builder/object_builder.rb', line 27

def load_object(obj, id, description)
  obj = JSON.generate(obj) unless obj.is_a? String
  @definitions[id.to_sym] = define_with_json(obj, id.to_sym, description)
end

.load_objectsObject



7
8
9
10
# File 'lib/object_builder/object_builder.rb', line 7

def load_objects
  @definitions = {}
  load_path(File.join(File.dirname(__FILE__), "definitions"))
end

.load_path(path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/object_builder/object_builder.rb', line 12

def load_path(path)
  if File.directory?(path) then
    define_with_directory(path).each do |defn|
      @definitions[defn.identifier] = defn
    end
    true
  elsif File.file?(path) then
    defn = define_with_filename(path)
    @definitions[defn.identifier] = defn
    true
  else
    false
  end
end

.supported_classes(truncate = true) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/object_builder/object_builder.rb', line 74

def supported_classes(truncate=true)
  r = {}
  @definitions.each do |id, defn|
    key = truncate ? talk.truncated_name(defn.name) : defn.name
    r[key] = true
  end
  r.keys
end

Instance Method Details

#block_call(&block) ⇒ Object



114
115
116
117
# File 'lib/object_builder/object_builder.rb', line 114

def block_call(&block)
  self.class.send(:define_method, :__block_call, &block)
  self.class.send(:instance_method, :__block_call).bind(self).call
end

#descriptionObject



132
133
134
# File 'lib/object_builder/object_builder.rb', line 132

def description
  self.class.description_text
end

#identifierObject



119
120
121
# File 'lib/object_builder/object_builder.rb', line 119

def identifier
  self.class.id
end

#nameObject



127
128
129
130
# File 'lib/object_builder/object_builder.rb', line 127

def name
  value if @cached.nil?
  @cached[:__class] || ""
end

#name_matches?(other) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/object_builder/object_builder.rb', line 123

def name_matches?(other)
  name.end_with? other.to_s
end

#valueObject



136
137
138
139
140
141
# File 'lib/object_builder/object_builder.rb', line 136

def value
  return @cached unless @cached.nil? or not self.class.is_cached
  v = block_call(&self.class.generate_block)
  v.delete(:__description)
  @cached = v
end