Class: Waw::ResourceCollection

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

Overview

Localized resources

Defined Under Namespace

Classes: DSL

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = "unnamed") ⇒ ResourceCollection

Creates a resource collection



32
33
34
35
# File 'lib/waw/resource_collection.rb', line 32

def initialize(name = "unnamed")
  @resources = {}
  @__name = name
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Logs a friendly message and returns nil



75
76
77
78
79
80
81
82
83
84
# File 'lib/waw/resource_collection.rb', line 75

def method_missing(name, *args)
  if args.size==1
    args[0]
  elsif args.size==0
    Waw.logger.warn("No such resource #{name} on #{@__name}, (#{caller[0]})")
    nil
  else
    super
  end
end

Instance Attribute Details

#__nameObject (readonly)

Resource name



29
30
31
# File 'lib/waw/resource_collection.rb', line 29

def __name
  @__name
end

Class Method Details

.parse_resource_file(f, name = File.basename(f, '.rs')) ⇒ Object

Parses a resource file



94
95
96
# File 'lib/waw/resource_collection.rb', line 94

def self.parse_resource_file(f, name=File.basename(f, '.rs'))
  parse_resources File.read(f), name
end

.parse_resources(str, name = "unnamed") ⇒ Object

Parses some resource string



87
88
89
90
91
# File 'lib/waw/resource_collection.rb', line 87

def self.parse_resources(str, name = "unnamed")
  r = ResourceCollection.new(name)
  DSL.new(r).instance_eval str
  r
end

Instance Method Details

#[](name) ⇒ Object

Returns the resource installed under name



44
45
46
# File 'lib/waw/resource_collection.rb', line 44

def [](name)
  @resources[name]
end

#eachObject Also known as: each_pair

Yields the block with each resource key,value pair



38
39
40
# File 'lib/waw/resource_collection.rb', line 38

def each
  @resources.each_pair {|k, v| yield(k, v)}
end

#has_resource?(name) ⇒ Boolean

Checks if a resource exists

Returns:



49
50
51
# File 'lib/waw/resource_collection.rb', line 49

def has_resource?(name)
  @resources.has_key?(name)
end