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.



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

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



34
35
36
# File 'lib/betterdocs/dsl/result/property.rb', line 34

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

#add_to_collector(collector) ⇒ Object



60
61
62
# File 'lib/betterdocs/dsl/result/property.rb', line 60

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

#assign(result, object) ⇒ Object



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

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

#compute_value(object) ⇒ Object



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

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

#sanitizerObject



56
57
58
# File 'lib/betterdocs/dsl/result/property.rb', line 56

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

#sub_representer?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/betterdocs/dsl/result/property.rb', line 30

def sub_representer?
  represent_with
end