Class: ActiveDelegate::Attribute::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/active_delegate/attribute/object.rb

Overview

Calculates attribute methods, aliases and options

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute_name, association_class, options = {}) ⇒ Object

Returns a new instance of Object.



12
13
14
15
16
# File 'lib/active_delegate/attribute/object.rb', line 12

def initialize(attribute_name, association_class, options = {})
  @attribute_name    = attribute_name
  @association_class = association_class
  @options           = options
end

Instance Attribute Details

#association_classObject (readonly)

Returns the value of attribute association_class.



10
11
12
# File 'lib/active_delegate/attribute/object.rb', line 10

def association_class
  @association_class
end

#attribute_nameObject (readonly)

Returns the value of attribute attribute_name.



10
11
12
# File 'lib/active_delegate/attribute/object.rb', line 10

def attribute_name
  @attribute_name
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/active_delegate/attribute/object.rb', line 10

def options
  @options
end

Instance Method Details

#accessible_attributesObject



94
95
96
# File 'lib/active_delegate/attribute/object.rb', line 94

def accessible_attributes
  @accessible_attributes ||= [unprefixed].map { |a| add_prefix(a) }
end

#aliasedObject



70
71
72
# File 'lib/active_delegate/attribute/object.rb', line 70

def aliased
  options[:alias] || prefixed
end

#aliased?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/active_delegate/attribute/object.rb', line 74

def aliased?
  prefixed != aliased
end

#aliasesObject



78
79
80
81
82
# File 'lib/active_delegate/attribute/object.rb', line 78

def aliases
  return {} unless aliased?

  Hash[delegatable_methods.map { |m| generate_alias(m) }]
end

#defaultObject



46
47
48
# File 'lib/active_delegate/attribute/object.rb', line 46

def default
  options.fetch :default, association_class.column_defaults[unprefixed.to_s]
end

#define?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/active_delegate/attribute/object.rb', line 18

def define?
  dirty? && (in_option?(:define) || options[:define])
end

#delegatable_attributesObject



98
99
100
# File 'lib/active_delegate/attribute/object.rb', line 98

def delegatable_attributes
  @delegatable_attributes ||= [unprefixed, *localized].map { |a| add_prefix(a) }
end

#delegatable_methodsObject



102
103
104
105
106
107
108
109
110
# File 'lib/active_delegate/attribute/object.rb', line 102

def delegatable_methods
  @delegatable_methods ||= [unprefixed, *localized].flat_map do |method_name|
    methods = Methods.new(
      method_name, association_class, writer: writer?, dirty: dirty?
    )

    methods.delegatable
  end
end

#dirty?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/active_delegate/attribute/object.rb', line 30

def dirty?
  writer? && (in_option?(:dirty) || options[:dirty])
end

#finder?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/active_delegate/attribute/object.rb', line 34

def finder?
  in_option?(:finder) || options[:finder]
end

#localize?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/active_delegate/attribute/object.rb', line 22

def localize?
  in_option?(:localized) || options[:localized]
end

#localizedObject



84
85
86
87
88
# File 'lib/active_delegate/attribute/object.rb', line 84

def localized
  return [] unless localize?

  @localized ||= Localize.new(unprefixed, association_class).attributes
end

#localized?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/active_delegate/attribute/object.rb', line 90

def localized?
  localized.any?
end

#prefixObject



42
43
44
# File 'lib/active_delegate/attribute/object.rb', line 42

def prefix
  options[:prefix]
end

#prefixedObject



62
63
64
# File 'lib/active_delegate/attribute/object.rb', line 62

def prefixed
  add_prefix(attribute_name)
end

#prefixed?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/active_delegate/attribute/object.rb', line 66

def prefixed?
  unprefixed != prefixed
end

#read_typeObject



50
51
52
# File 'lib/active_delegate/attribute/object.rb', line 50

def read_type
  options.fetch :cast_type, write_type
end

#scope?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/active_delegate/attribute/object.rb', line 38

def scope?
  in_option?(:scope) || options[:scope]
end

#unprefixedObject



58
59
60
# File 'lib/active_delegate/attribute/object.rb', line 58

def unprefixed
  remove_prefix(attribute_name)
end

#write_typeObject



54
55
56
# File 'lib/active_delegate/attribute/object.rb', line 54

def write_type
  association_class.type_for_attribute(unprefixed.to_s)
end

#writer?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/active_delegate/attribute/object.rb', line 26

def writer?
  in_option?(:writer) || options[:writer]
end