Class: Threatinator::PropertyDefiner::Property

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ Property

Returns a new instance of Property.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/threatinator/property_definer.rb', line 42

def initialize(name, opts = {})
  @name = name.to_sym
  @type = opts.delete(:type) || Object
  if @validator = opts.delete(:validate) 
    unless @validator.kind_of?(Proc)
      raise ArgumentError.new(":validate must be a proc")
    end
  end
  @default_value = opts.delete(:default)
  @opts = opts
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



41
42
43
# File 'lib/threatinator/property_definer.rb', line 41

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



41
42
43
# File 'lib/threatinator/property_definer.rb', line 41

def opts
  @opts
end

Instance Method Details

#default_valueObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/threatinator/property_definer.rb', line 54

def default_value
  case @default_value
  when Proc
    return @default_value.call()
  when nil
    return nil
  else 
    return @default_value
  end
end

#valid?(obj, val) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
# File 'lib/threatinator/property_definer.rb', line 65

def valid?(obj, val)
  return false unless val.kind_of?(@type)
  unless @validator.nil?
    return false unless @validator.call(obj, val)
  end
  true
end

#validate!(obj, val) ⇒ Object



73
74
75
76
77
# File 'lib/threatinator/property_definer.rb', line 73

def validate!(obj, val)
  unless valid?(obj, val)
    raise Threatinator::Exceptions::InvalidAttributeError.new(self.name, val)
  end
end