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.



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

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rabbit/properties.rb', line 80

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



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

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

#[]=(key, value) ⇒ Object



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

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

#drawsObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rabbit/properties.rb', line 64

def draws
  targets = @data.select do |key, _|
    /\Adraw\d+\z/.match?(key)
  end
  targets = targets.sort_by do |key, _|
    Integer(key[/\d+/], 10)
  end
  targets.collect do |_, value|
    YAMLLoader.load(value)
  end
end

#each(&block) ⇒ Object



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

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

#get_boolean(key, default = nil) ⇒ Object



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

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

#get_float(key, default = nil) ⇒ Object



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

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

#get_integer(key, default = nil) ⇒ Object



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

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

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



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

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

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



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

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

#respond_to_missing?(name, include_private) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/rabbit/properties.rb', line 76

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