Class: BCDD::Data::Properties

Inherits:
Object
  • Object
show all
Defined in:
lib/bcdd/ext/data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProperties

Returns a new instance of Properties.



29
30
31
32
# File 'lib/bcdd/ext/data.rb', line 29

def initialize
  @spec = {}
  @contract = false
end

Instance Attribute Details

#contractObject (readonly)

Returns the value of attribute contract.



27
28
29
# File 'lib/bcdd/ext/data.rb', line 27

def contract
  @contract
end

#specObject (readonly)

Returns the value of attribute spec.



27
28
29
# File 'lib/bcdd/ext/data.rb', line 27

def spec
  @spec
end

Instance Method Details

#attribute(name, **options) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bcdd/ext/data.rb', line 34

def attribute(name, **options)
  name.is_a?(Symbol) or raise ArgumentError, "#{name.inspect} must be a Symbol"

  value_option = options[:value]

  spec[name] =
    if value_option
      (value_option.is_a?(Value::Object) ? value_option : Value[value_option])::Properties
    else
      Value::Properties.new(options).freeze
    end
end

#contract?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/bcdd/ext/data.rb', line 55

def contract?
  contract
end

#freezeObject



47
48
49
50
51
52
53
# File 'lib/bcdd/ext/data.rb', line 47

def freeze
  @contract = spec.any? { |_, properties| properties.contract? }

  spec.freeze

  super
end

#map(input) ⇒ Object



59
60
61
62
63
# File 'lib/bcdd/ext/data.rb', line 59

def map(input)
  spec.each_with_object({}) do |(name, properties), output|
    output[name] = properties.map(input[name])
  end
end