Class: Granite::Form::Model::Attributes::Reflections::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/granite/form/model/attributes/reflections/base.rb,
lib/granite/form/model/attributes/reflections/base/build_type_definition.rb

Direct Known Subclasses

Attribute, ReferenceOne

Defined Under Namespace

Classes: BuildTypeDefinition

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *args, &block) ⇒ Base

Returns a new instance of Base.



22
23
24
25
26
27
28
# File 'lib/granite/form/model/attributes/reflections/base.rb', line 22

def initialize(name, *args, &block)
  @name = name.to_s

  @options = args.extract_options!
  @options[:type] = args.first if args.first
  @options[:default] = block if block
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/granite/form/model/attributes/reflections/base.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/granite/form/model/attributes/reflections/base.rb', line 7

def options
  @options
end

Class Method Details

.attribute_classObject



17
18
19
# File 'lib/granite/form/model/attributes/reflections/base.rb', line 17

def attribute_class
  @attribute_class ||= "Granite::Form::Model::Attributes::#{name.demodulize}".constantize
end

.build(_target, generated_methods, name, *args, &block) ⇒ Object



10
11
12
13
# File 'lib/granite/form/model/attributes/reflections/base.rb', line 10

def build(_target, generated_methods, name, *args, &block)
  generate_methods name, generated_methods
  new(name, *args, &block)
end

.generate_methods(name, target) ⇒ Object



15
# File 'lib/granite/form/model/attributes/reflections/base.rb', line 15

def generate_methods(name, target) end

Instance Method Details

#build_attribute(owner, raw_value = Granite::Form::UNDEFINED) ⇒ Object



30
31
32
33
34
35
# File 'lib/granite/form/model/attributes/reflections/base.rb', line 30

def build_attribute(owner, raw_value = Granite::Form::UNDEFINED)
  type_definition = self.class::BuildTypeDefinition.new(owner, self).call
  attribute = self.class.attribute_class.new(type_definition)
  attribute.write_value(raw_value, origin: :persistence) unless raw_value == Granite::Form::UNDEFINED
  attribute
end

#enumObject



45
46
47
# File 'lib/granite/form/model/attributes/reflections/base.rb', line 45

def enum
  options[:enum] || options[:in]
end

#inspect_reflectionObject



53
54
55
# File 'lib/granite/form/model/attributes/reflections/base.rb', line 53

def inspect_reflection
  "#{name}: #{type}"
end

#keysObject



49
50
51
# File 'lib/granite/form/model/attributes/reflections/base.rb', line 49

def keys
  @keys ||= Array.wrap(options[:keys]).map(&:to_s)
end

#readonlyObject



41
42
43
# File 'lib/granite/form/model/attributes/reflections/base.rb', line 41

def readonly
  options[:readonly]
end

#typeObject



37
38
39
# File 'lib/granite/form/model/attributes/reflections/base.rb', line 37

def type
  options[:type]
end