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

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

Constant Summary collapse

@@default_type =
:string

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Builder

#build, #builder, #builder=, #builder_default_name, #builder_default_options, #builder_default_state

Constructor Details

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

Returns a new instance of Base.



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

def initialize dbi,name,*args, &block
  @dbi=dbi
  self.name = name
  options = args ? args.extract_options! : {}
  type = args[0]

  self.type = type || @@default_type

  self.set_attributes(options)
  if block_given?
    self.instance_eval(&block)
  end
  set_default_values
  validate
end

Instance Attribute Details

#dbiObject (readonly)

Returns the value of attribute dbi.



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

def dbi
  @dbi
end

#dbi_fieldObject

Returns the value of attribute dbi_field.



35
36
37
# File 'lib/lolita/configuration/field.rb', line 35

def dbi_field
  @dbi_field
end

#nested_inObject

Returns the value of attribute nested_in.



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

def nested_in
  @nested_in
end

Instance Method Details

#find_dbi_fieldObject



126
127
128
129
130
# File 'lib/lolita/configuration/field.rb', line 126

def find_dbi_field
  @dbi_field ||= self.dbi.fields.detect{|field|
    field.name.to_s == @name.to_s || (field.association && field.association.name.to_s == @name.to_s)
  }
end

#match_state_of?(record) ⇒ Boolean

Returns:



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/lolita/configuration/field.rb', line 73

def match_state_of?(record)
  if @on
    if @on.respond_to?(:call)
      @on.call(record)
    elsif @on.respond_to?(:detect)
      !!@on.detect{|state| record_state_matches_with(record,state)}
    else
      record_state_matches_with(record,@on)
    end
  else
    true
  end
end

#name=(value) ⇒ Object



69
70
71
# File 'lib/lolita/configuration/field.rb', line 69

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

#nested?Boolean

Returns:



101
102
103
# File 'lib/lolita/configuration/field.rb', line 101

def nested?
  !self.nested_in.nil?
end

#nested_in?(dbi_or_class) ⇒ Boolean

Returns:



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

def nested_in?(dbi_or_class)
  if dbi_or_class.respond_to?(:klass)
    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_state_matches_with(record, state) ⇒ Object



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

def record_state_matches_with(record,state)
  @dbi.switch_record_state(record).state == state
end

#set_attributes(attributes) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/lolita/configuration/field.rb', line 113

def set_attributes(attributes)
  excluded_keys = [:name,:type]
  attributes.each{|attr,value|
    unless excluded_keys.include?(attr.to_sym)
      if attr == :dbi_field
        self.dbi_field = value
      else
        self.send(:"#{attr}=",value)
      end
    end
  }
end

#title(new_title = nil) ⇒ Object



53
54
55
56
57
58
# File 'lib/lolita/configuration/field.rb', line 53

def title(new_title = nil)
  if new_title
    @title = new_title
  end
  Lolita::Utils.dynamic_string(@title, :default => @dbi.klass.human_attribute_name(@name))
end

#type(value = nil) ⇒ Object



60
61
62
63
# File 'lib/lolita/configuration/field.rb', line 60

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

#type=(value) ⇒ Object



65
66
67
# File 'lib/lolita/configuration/field.rb', line 65

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