Class: Lolita::Builder::Custom

Inherits:
Object
  • Object
show all
Defined in:
lib/lolita/builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element, attributes_as_hash) ⇒ Custom

Returns a new instance of Custom.



63
64
65
66
67
68
69
70
# File 'lib/lolita/builder.rb', line 63

def initialize(element, attributes_as_hash)
  @element = element
  @options = {}
  @build_attributes = {}
  @conditions = {}
  set_attributes(attributes_as_hash)
  set_default_attribute_values
end

Instance Attribute Details

#build_attributesObject

Returns the value of attribute build_attributes.



61
62
63
# File 'lib/lolita/builder.rb', line 61

def build_attributes
  @build_attributes
end

#optionsObject

Returns the value of attribute options.



61
62
63
# File 'lib/lolita/builder.rb', line 61

def options
  @options
end

Class Method Details

.create(element, *args) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/lolita/builder.rb', line 30

def create(element, *args)
  possible_builder = extract_args(*args)
  if possible_builder.is_a?(Hash)
    Lolita::Builder::Custom.new(element, possible_builder)
  else
    possible_builder
  end
end

.extract_args(*args) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lolita/builder.rb', line 39

def extract_args(*args)
  if args && args.any?
    options = args.extract_options! || {}
    if args[0] && args[0].is_a?(self)
      args[0]
    elsif args[0].is_a?(String) || args[0].is_a?(Symbol)
      options[:name] = args[0]
      if args[1]
        options[:state] = args[1]
      end
      options
    elsif options
      options
    else
       raise ArgumentError, "Don't know how to make builder from #{options}."
    end
  else
    return {}
  end
end

Instance Method Details

#buildObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/lolita/builder.rb', line 82

def build
    
  path = if conditions?
    switch_path do |name,state|
      if conditions_met?
        [fixed_name(name).to_sym,state.to_sym]
      else
        [self.name, self.state]
      end
    end
  else
    [self.name,self.state]
  end
  result = path + [merged_options]
  return result
end

#nameObject



99
100
101
102
103
104
105
106
# File 'lib/lolita/builder.rb', line 99

def name
  result = if build_attributes[:name].to_s.size > 0
    fixed_name(build_attributes[:name])
  else
    fixed_name(@name,build_attributes[:name])
  end
  result.to_sym
end

#stateObject



108
109
110
111
112
113
114
115
# File 'lib/lolita/builder.rb', line 108

def state
  result = if build_attributes[:state] && build_attributes[:state].to_s.size > 0
    build_attributes[:state]
  else
    @state
  end
  result.to_sym
end

#with(*values) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/lolita/builder.rb', line 72

def with(*values)
  new_values = self.class.extract_args(*values)
  if new_values.is_a?(Hash)
    @build_attributes = new_values
  else
    raise ArgumentError, "Can't build with other builder, use build on that builder!"
  end
  self
end