Class: Arrest::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/arrest/attributes/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, clazz, actions = nil) ⇒ Attribute

Returns a new instance of Attribute.



14
15
16
17
18
19
20
21
# File 'lib/arrest/attributes/attribute.rb', line 14

def initialize(name, clazz, actions = nil)
  @name = name.to_sym
  @actions = actions || [:create, :retrieve, :update, :delete]
  @clazz = clazz
  @dirty_sensitive = @clazz.ancestors.include?(Dirty)
  @dirty = false
  @json_name = Source.json_key_converter.key_to_json(name).to_sym
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



12
13
14
# File 'lib/arrest/attributes/attribute.rb', line 12

def actions
  @actions
end

#clazzObject

Returns the value of attribute clazz.



12
13
14
# File 'lib/arrest/attributes/attribute.rb', line 12

def clazz
  @clazz
end

#dirtyObject

Returns the value of attribute dirty.



12
13
14
# File 'lib/arrest/attributes/attribute.rb', line 12

def dirty
  @dirty
end

#json_nameObject

Returns the value of attribute json_name.



12
13
14
# File 'lib/arrest/attributes/attribute.rb', line 12

def json_name
  @json_name
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/arrest/attributes/attribute.rb', line 12

def name
  @name
end

Instance Method Details

#dirty?Boolean

Returns:



31
32
33
34
35
36
37
# File 'lib/arrest/attributes/attribute.rb', line 31

def dirty?
  if @dirty_sensitive
    @dirty
  else
    true # treat as 'always' dirty
  end
end

#from_hash(parent, value) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/arrest/attributes/attribute.rb', line 39

def from_hash(parent, value)
  return if value == nil

  if @clazz.respond_to?(:convert)
    return @clazz.convert(value)
  end

  converter = CONVERTER[@clazz]
  if converter == nil
    puts "No converter for: #{@clazz.name}"
    converter = IdentConv
  end
  converter.convert value
end

#mutable?Boolean

Returns:



27
28
29
# File 'lib/arrest/attributes/attribute.rb', line 27

def mutable?
  @actions.include?(:create) || @actions.include?(:update)
end

#read_only?Boolean

Returns:



23
24
25
# File 'lib/arrest/attributes/attribute.rb', line 23

def read_only?
  @actions == [:retrieve]
end

#to_hash(value) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/arrest/attributes/attribute.rb', line 55

def to_hash value
  return nil unless value != nil

  if @clazz.respond_to?(:mk_json)
    return @clazz.mk_json(value)
  end

  converter = CONVERTER[@clazz]
  if converter == nil
    puts "No converter for: #{@clazz.name}"
    converter = IdentConv
  end
  converter.mk_json value
end