Class: Optimus::Implementations::Standard::Interface

Inherits:
Optimus::Interface show all
Extended by:
Forwardable
Defined in:
lib/optimus/implementations/standard.rb

Instance Attribute Summary collapse

Attributes inherited from Optimus::Interface

#implementation

Instance Method Summary collapse

Methods inherited from Optimus::Interface

#inspect

Constructor Details

#initialize(implementation) ⇒ Interface

Returns a new instance of Interface.



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/optimus/implementations/standard.rb', line 101

def initialize (implementation)
  super(implementation)

  @options = {}

  @data = {
    :parameters => {},
    :arguments  => []
  }

  @internal = {}
end

Instance Attribute Details

#argumentsObject (readonly) Also known as: args

Returns the value of attribute arguments.



90
91
92
# File 'lib/optimus/implementations/standard.rb', line 90

def arguments
  @arguments
end

#dataObject (readonly)

Returns the value of attribute data.



90
91
92
# File 'lib/optimus/implementations/standard.rb', line 90

def data
  @data
end

#optionsObject (readonly)

Returns the value of attribute options.



90
91
92
# File 'lib/optimus/implementations/standard.rb', line 90

def options
  @options
end

#parametersObject (readonly) Also known as: params

Returns the value of attribute parameters.



90
91
92
# File 'lib/optimus/implementations/standard.rb', line 90

def parameters
  @parameters
end

Instance Method Details

#get(name) ⇒ Object



134
135
136
# File 'lib/optimus/implementations/standard.rb', line 134

def get (name)
  @options[name]
end

#merge(hash) ⇒ Object



114
115
116
117
118
# File 'lib/optimus/implementations/standard.rb', line 114

def merge (hash)
  result = self.clone
  result.merge! hash
  result
end

#normalizeObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/optimus/implementations/standard.rb', line 138

def normalize
  @parameters = {}
  @arguments  = @data[:arguments]

  @options.each_value {|option|
    if option.default
      @parameters[option.long]  = option.default
      @parameters[option.short] = option.default
    end
  }

  @data[:parameters].each {|key, value|
    if value[:type] == :long
      if @options[key]
        @parameters[@options[key].long]  = value[:value]
        @parameters[@options[key].short] = value[:value]
      end
    else
      @options.each_value {|option|
        if option.short == key
          @parameters[option.long]  = value[:value]
          @parameters[option.short] = value[:value]
        end
      }
    end
  }

  @options.each_value {|option|
    case option.type
      when :numeric
        value = @parameters[option.long]
        value = (value.to_i == value.to_f) ? value.to_i : value.to_f

      when :boolean
        value = @parameters[option.long] == 'true'

      when :date
        value = Date.parse(@parameters[option.long])

      when :array
        value = @parameters[option.long].split(/,/)

      when :hash
        value = Hash[@parameters[option.long].split(/,/).split(/=/)]
    end

    @parameters[option.long]  = value
    @parameters[option.short] = value
  }
end

#set(arguments) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/optimus/implementations/standard.rb', line 120

def set (arguments)
  if !arguments[:long]
    raise 'You have to pass at least :long.'
  end

  arguments[:long]  = arguments[:long].to_sym
  arguments[:short] = arguments[:short].to_sym if arguments[:short]

  option    = Option.new(arguments)
  option.name = arguments[:long]

  @options[option.name] = option
end