class EasySettings::Struct

Constants

UnknownPropertyError

Public Class Methods

import(hash) click to toggle source
# File lib/easy-settings/struct.rb, line 8
def self.import(hash)
  new.tap do |struct|
    hash.each do |k, v|
      case v
      when Hash
        v = import(v)
      when Array
        v = v.collect{ |i| i.is_a?(Hash) ? import(i) : i }
      end
      struct[k] = v
    end
  end
end
new(properties = {}) click to toggle source
# File lib/easy-settings/struct.rb, line 22
def initialize(properties = {})
  @properties = properties
end

Public Instance Methods

[](property) click to toggle source
# File lib/easy-settings/struct.rb, line 26
def [](property)
  @properties.fetch(property.to_s){ raise_unknown_property(property) }
end
[]=(property, value) click to toggle source
# File lib/easy-settings/struct.rb, line 30
def []=(property, value)
  @properties[property.to_s] = value
end
method_missing(method, *args) click to toggle source
# File lib/easy-settings/struct.rb, line 46
def method_missing(method, *args)
  method = method.to_s
  return @properties.fetch(method) unless method.end_with?("=")
  property = method[0..-2]
  @properties[property] = args[0]
rescue KeyError => e
  raise_unknown_property(method)
end
raise_unknown_property(property) click to toggle source
# File lib/easy-settings/struct.rb, line 55
def raise_unknown_property(property)
  raise UnknownPropertyError, "unknown property #{property.inspect} (#{@properties.keys.map(&:inspect).join(", ")})"
end
respond_to?(property, include_private = false) click to toggle source
# File lib/easy-settings/struct.rb, line 42
def respond_to?(property, include_private = false)
  @properties.has_key?(property.to_s)
end
to_h() click to toggle source
# File lib/easy-settings/struct.rb, line 38
def to_h
  @properties.transform_values{ |v| v.is_a?(EasySettings::Struct) ? v.to_h : v }
end
try(property) click to toggle source
# File lib/easy-settings/struct.rb, line 34
def try(property)
  @properties[property.to_s]
end