Class: Betterdocs::Dsl::Result::Property

Inherits:
Betterdocs::Dsl::Representer show all
Extended by:
Tins::DSLAccessor
Includes:
Common, Naming
Defined in:
lib/betterdocs/dsl/result/property.rb

Direct Known Subclasses

CollectionProperty

Instance Attribute Summary

Attributes included from Naming

#options

Attributes inherited from Betterdocs::Dsl::Representer

#name, #representer

Instance Method Summary collapse

Methods included from Naming

#below_path, #full_name, #nesting_name, #path, #public_name

Methods included from Common

#set_context

Methods inherited from Betterdocs::Dsl::Representer

#assign?

Constructor Details

#initialize(representer, name, options, &block) ⇒ Property

Returns a new instance of Property.



23
24
25
26
27
28
29
30
# File 'lib/betterdocs/dsl/result/property.rb', line 23

def initialize(representer, name, options, &block)
  super
  types Betterdocs::Dsl::JsonTypeMapper.map_types(types)
  if sr = sub_representer?
    sr < Betterdocs::ResultRepresenter or
      raise TypeError, "#{sr.inspect} is not a Betterdocs::Result subclass"
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Betterdocs::Dsl::Common

Instance Method Details

#actual_property_nameObject



36
37
38
# File 'lib/betterdocs/dsl/result/property.rb', line 36

def actual_property_name
  (options[:as] || name).to_s
end

#add_to_collector(collector) ⇒ Object



62
63
64
# File 'lib/betterdocs/dsl/result/property.rb', line 62

def add_to_collector(collector)
  collector.properties[name] = self
end

#assign(result, object) ⇒ Object



40
41
42
43
44
# File 'lib/betterdocs/dsl/result/property.rb', line 40

def assign(result, object)
  assign?(object) or return
  result[actual_property_name] = compute_value(object)
  self
end

#compute_value(object) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/betterdocs/dsl/result/property.rb', line 46

def compute_value(object)
  value = object.__send__(name)
  value.nil? and return
  if represent_with
    represent_with.hashify(value)
  elsif ActiveSupport::TimeWithZone === value
    value.extend Betterdocs::JsonTimeWithZone
  else
    sanitizer.sanitize(value)
  end
end

#sanitizerObject



58
59
60
# File 'lib/betterdocs/dsl/result/property.rb', line 58

def sanitizer
  Betterdocs::Sanitizer.new(&sanitize)
end

#sub_representer?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/betterdocs/dsl/result/property.rb', line 32

def sub_representer?
  represent_with
end