Class: Rabbit::Properties

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rabbit/properties.rb

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Properties

Returns a new instance of Properties.


23
24
25
# File 'lib/rabbit/properties.rb', line 23

def initialize(data)
  @data = normalize(data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rabbit/properties.rb', line 67

def method_missing(name, *args, &block)
  case args.size
  when 0
    key = name.to_s
    if key.end_with?("?")
      key = key[0..-2]
      is_predict = true
    else
      is_predict = false
    end
    key = normalize_key(key)
    return super unless @data.key?(key)
    value = @data[key]
    if is_predict
      true_value?(value)
    else
      value
    end
  when 1
    key = name.to_s
    return super unless key.end_with?("=")
    key = key[0..-2]
    @data[normalize_key(key)] = args[0]
  else
    super
  end
end

Instance Method Details

#[](key) ⇒ Object


27
28
29
# File 'lib/rabbit/properties.rb', line 27

def [](key)
  @data[normalize_key(key)]
end

#[]=(key, value) ⇒ Object


31
32
33
# File 'lib/rabbit/properties.rb', line 31

def []=(key, value)
  @data[normalize_key(key)] = value
end

#each(&block) ⇒ Object


35
36
37
# File 'lib/rabbit/properties.rb', line 35

def each(&block)
  @data.each(&block)
end

#get_boolean(key, default = nil) ⇒ Object


39
40
41
42
43
# File 'lib/rabbit/properties.rb', line 39

def get_boolean(key, default=nil)
  value = self[key]
  return default if value.nil?
  true_value?(value)
end

#get_float(key, default = nil) ⇒ Object


49
50
51
# File 'lib/rabbit/properties.rb', line 49

def get_float(key, default=nil)
  float_value(self[key] || default)
end

#get_integer(key, default = nil) ⇒ Object


45
46
47
# File 'lib/rabbit/properties.rb', line 45

def get_integer(key, default=nil)
  integer_value(self[key] || default)
end

#get_relative_size(key, filename, default = nil) ⇒ Object


57
58
59
60
61
# File 'lib/rabbit/properties.rb', line 57

def get_relative_size(key, filename, default=nil)
  relative_size_value(self[key] || default,
                      filename,
                      key)
end

#get_size(key, filename, default = nil) ⇒ Object


53
54
55
# File 'lib/rabbit/properties.rb', line 53

def get_size(key, filename, default=nil)
  size_value(self[key] || default, filename, key)
end

#respond_to_missing?(name, include_private) ⇒ Boolean

Returns:

  • (Boolean)

63
64
65
# File 'lib/rabbit/properties.rb', line 63

def respond_to_missing?(name, include_private)
  @data.key?(normalize_key(name.to_s.gsub(/\?\z/, "")))
end