Sha256: 47a4ae770b9d25f1c8299eeb27cadda20e3c3bcd19d8c64590ec410c5180f1d5

Contents?: true

Size: 1.85 KB

Versions: 4

Compression:

Stored size: 1.85 KB

Contents

class Rack::CoreData::DataModel
  class Attribute
    attr_reader :name, :type, :identifier, :version_hash_modifier, :default_value, :minimum_value, :maximum_value, :regular_expression

    def initialize(attribute)
      raise ArgumentError unless ::Nokogiri::XML::Element === attribute

      @name = attribute['name']
      @type = attribute['attributeType']
      @identifier = attribute['elementID']
      @version_hash_modifier = attribute['versionHashModifier']

      @default_value = default_value_from_string(attribute['defaultValueString'])
      @minimum_value = range_value_from_string(attribute['minValueString'])
      @maximum_value = range_value_from_string(attribute['maxValueString'])

      @regular_expression = Regexp.new(attributes['regularExpressionString']) rescue nil

      @optional = attribute['optional'] == "YES"
      @transient = attribute['transient'] == "YES"
      @indexed = attribute['indexed'] == "YES"
      @syncable = attribute['syncable'] == "YES"
    end

    def to_s
      [@name, @type].to_s
    end

    [:optional, :transient, :indexed, :syncable].each do |symbol|
      define_method("#{symbol}?") {!!instance_variable_get(("@#{symbol}").intern)}
    end

    private

    def default_value_from_string(string)
      return nil unless string
      
      return case @type
             when "Integer 16", "Integer 32", "Integer 64"
               string.to_i
             when "Float", "Decimal"
               string.to_f
             when "Boolean"
               string == "YES"
             else
               string
             end
    end

    def range_value_from_string(string)
      return nil unless string
      
      return case @type
             when "Float", "Decimal"
               string.to_f
             when "Date"
               string
             else
               string.to_i
             end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rack-core-data-0.3.1 ./lib/rack/core-data/data_model/attribute.rb
rack-core-data-0.3.0 ./lib/rack/core-data/data_model/attribute.rb
rack-core-data-0.2.0 ./lib/rack/core-data/data_model/attribute.rb
rack-core-data-0.1.0 ./lib/rack/core-data/data_model/attribute.rb