lib/xmlss/element/cell.rb in xmlss-1.0.0.rc.1 vs lib/xmlss/element/cell.rb in xmlss-1.0.0.rc.2
- old
+ new
@@ -5,12 +5,10 @@
class Cell
def self.writer; :cell; end
attr_accessor :index, :style_id, :formula, :href, :merge_across, :merge_down
- alias_method :style_i_d, :style_id
- alias_method :h_ref, :href
attr_accessor :data
include Enumeration
enum :type, {
@@ -19,22 +17,18 @@
:boolean => "Boolean",
:string => "String",
:error => "Error"
}
- def initialize(*args, &build)
+ def initialize(*args)
attrs = args.last.kind_of?(::Hash) ? args.pop : {}
- self.data = args.last.nil? ? (attrs[:data] || "") : args.last
- self.type = attrs[:type] unless attrs[:type].nil?
+ self.data = [args.last, attrs.delete(:data), ''].reject{|v| v.nil?}.first
+ self.merge_across = attrs.delete(:merge_across) || 0
+ self.merge_down = attrs.delete(:merge_down) || 0
- self.index = attrs[:index]
- self.style_id = attrs[:style_id]
- self.formula = attrs[:formula]
- self.href = attrs[:href]
- self.merge_across = attrs[:merge_across] || 0
- self.merge_down = attrs[:merge_down] || 0
+ attrs.keys.each { |k| self.send("#{k}=", attrs[k]) }
end
def data=(v)
self.type = data_type(v)
@data = v
@@ -47,30 +41,41 @@
else
self.data.to_s
end
end
- [:index, :merge_across, :merge_down].each do |meth|
- define_method("#{meth}=") do |value|
- if value && !value.kind_of?(::Fixnum)
- raise ArgumentError, "must specify #{meth} as a Fixnum"
- end
- instance_variable_set("@#{meth}", value && value <= 0 ? nil : value)
+ def index=(value)
+ if value && !value.kind_of?(::Fixnum)
+ raise ArgumentError, "must specify `index` as a Fixnum"
end
+ @index = (value && value <= 0 ? nil : value)
end
+ def merge_across=(value)
+ if value && !value.kind_of?(::Fixnum)
+ raise ArgumentError, "must specify `merge_across` as a Fixnum"
+ end
+ @merge_across = (value && value <= 0 ? nil : value)
+ end
+
+ def merge_down=(value)
+ if value && !value.kind_of?(::Fixnum)
+ raise ArgumentError, "must specify `merge_down` as a Fixnum"
+ end
+ @merge_down = (value && value <= 0 ? nil : value)
+ end
+
private
def data_type(v)
- case v
- when ::Numeric
+ if v.kind_of?(::String) || v.kind_of?(::Symbol)
+ :string
+ elsif v.kind_of?(::Numeric)
:number
- when ::Date, ::Time
+ elsif v.kind_of?(::Date) || v.kind_of?(::Time)
:date_time
- when ::TrueClass, ::FalseClass
+ elsif v.kind_of?(::TrueClass) || v.kind_of?(::FalseClass)
:boolean
- when ::String, ::Symbol
- :string
else
:string
end
end