Class: Lolita::Configuration::Field::Base

Inherits:
Object
  • Object
show all
Includes:
Builder
Defined in:
lib/lolita/configuration/field.rb

Direct Known Subclasses

Array, BigDecimal, Boolean, Datetime, Integer, String, Time

Constant Summary collapse

@@default_type =
"string"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Builder

#build, #builder_options, #default_builder, #get_builder

Constructor Details

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

Returns a new instance of Base.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lolita/configuration/field.rb', line 35

def initialize dbi, *args, &block
  @dbi=dbi
  before_init(*args)
  begin
    self.set_attributes(*args)
    if block_given?
      self.instance_eval(&block)
      process_type unless @type 
    end
    set_default_values
    validate
  rescue Exception=>e
    unless Lolita::Configuration::Field.temp_object?
      raise e
    end
  end
end

Instance Attribute Details

#dbiObject (readonly)

Returns the value of attribute dbi.



33
34
35
# File 'lib/lolita/configuration/field.rb', line 33

def dbi
  @dbi
end

#nested_inObject

Returns the value of attribute nested_in.



33
34
35
# File 'lib/lolita/configuration/field.rb', line 33

def nested_in
  @nested_in
end

Instance Method Details

#name=(value) ⇒ Object



83
84
85
# File 'lib/lolita/configuration/field.rb', line 83

def name=(value)
  @name=value ? value.to_sym : nil
end

#nested?Boolean

Returns:



99
100
101
# File 'lib/lolita/configuration/field.rb', line 99

def nested?
  !self.nested_in.nil?
end

#nested_in?(dbi_or_class) ⇒ Boolean

Returns:



103
104
105
106
107
108
109
# File 'lib/lolita/configuration/field.rb', line 103

def nested_in?(dbi_or_class)
  if dbi_or_class.is_a?(Lolita::DBI::Base)
    self.nested_in && self.nested_in.klass==dbi_or_class.klass
  else
    self.nested_in && self.nested_in.klass==dbi_or_class
  end
end

#record_valueObject

TODO is this useable



121
122
123
124
125
126
127
# File 'lib/lolita/configuration/field.rb', line 121

def record_value #TODO test
  if self.record
    self.record.send(self.name.to_sym)
  else
    nil
  end
end

#set_attributes(*args) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/lolita/configuration/field.rb', line 112

def set_attributes(*args)
  @given_attributes.each{|attr,value|
    if (attr.to_sym==:type && !@type) || attr.to_sym!=:type
      self.send(:"#{attr}=",value)
    end
  }
end

#type(value = nil) ⇒ Object



74
75
76
77
# File 'lib/lolita/configuration/field.rb', line 74

def type value=nil
  @type=value.to_s.underscore if value
  @type
end

#type=(value) ⇒ Object



79
80
81
# File 'lib/lolita/configuration/field.rb', line 79

def type=(value)
  @type=value ? value.to_s.underscore : nil
end

#type_nameObject



87
88
89
# File 'lib/lolita/configuration/field.rb', line 87

def type_name
  self.type.to_s.downcase
end

#value(value = nil, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/lolita/configuration/field.rb', line 53

def value value=nil, &block
  self.send(:value=,value,&block) if value || block_given?
  unless @value
    self.record_value
  else
    if @value.is_a?(Proc)
      @value.call(self)
    else
      @value
    end
  end
end

#value=(value = nil, &block) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/lolita/configuration/field.rb', line 66

def value=(value=nil,&block)
  if block_given?
    @value=block
  else
    @value=value
  end
end